前言
Selenium是用于Web应用程序测试的工具。Selenium可以直接运行在浏览器中,模拟用户在浏览器中的各种操作,包括但不限于点击、复制、填写等。
不同的浏览器与Selenium之间进行通信的载体不同,ChromeDriver就是Chrome浏览器与Selenium进行通信的载体。
本文主要介绍用Python调用Selenium及ChromeDriver,实现Chrome浏览器多开。
步骤
Step 1 配置清华镜像源
-
按下Win + x按键,输入cmd,打开命令行窗口
-
执行
conda config --set show_channel_urls yes生成.condarc文件 -
修改.
condarc文件的内容如下:show_channel_urls: true channels: - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/或者cmd输入:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
Step 2 新建一个conda虚拟环境
-
执行命令
conda create -n chrome python=3.6 activate chrome conda install selenium
Step 3 下载ChromeDriver
- Chrome浏览器地址栏输入
chrome://version/,查看当前浏览器的版本。 - 在地址栏输入
https://registry.npmmirror.com/binary.html?path=chromedriver/,找到对应版本的ChromeDriver并下载到本地,并解压出chromedriver.exe。
Step 4 脚本编写与运行
- 编写python脚本,代码如下:
// chrome-selenium.py
from selenium import webdriver
import time
def main():
driver = webdriver.Chrome(executable_path=r"D:\ChromeDriver\chromedriver.exe")
driver.maximize_window()
// driver.get("https://www.baidu.com/")
time.sleep(60*60*24) // 推迟执行的秒数
driver.quit()
if __name__ == '__main__':
main()
-
执行脚本
python chrome-selenium.py