Python3+selenium3环境搭建笔记

系统:win7 64位
浏览器:ie9 64位 chrome70 32位 firefox63 64位
python版本:3.6.5 Windows x86 executable installer
selenium版本:selenium3.141.0

1、安装指定版本selenium3
pip install selenium==3.141.0


2、驱动与浏览器版本对应关系
1)IE9+ 64位
驱动名称:IEDriverServer.exe 版本:v3.0 IEDriverServer的版本号和Selenium的版本号一定要一致
驱动下载地址:http://selenium-release.storage.googleapis.com/index.html
驱动放在目录下 path=C:WindowsSystem32

2)chrome70+ 32位
驱动名称: chromedriver.exe 版本:v2.42
驱动下载地址: http://chromedriver.storage.googleapis.com/index.html
驱动放在目录下: C:Program Files (x86)GoogleChromeApplication
环境变量配置:path=C:Program Files (x86)GoogleChromeApplication

配置文件地址:在浏览器中通过访问获得 chrome://version/ 个人资料项:C:Usersanthony.huangAppDataLocalGoogleChromeUser DataDefault

3)firefox65.0+
驱动名称:geckodriver.exe 版本:v0.23 geckodriver-v0.23.0-win64.zip
驱动下载地址:https://github.com/mozilla/geckodriver/releases
驱动放在目录下:C:Program Files (x86)Mozilla Firefox
浏览器下载地址:http://www.firefox.com.cn/
配置文件地址:在浏览器中通过访问获得 帮助->故障排除信息->应用程序概要:配置文件夹 C:Usersanthony.huangAppDataRoamingMozillaFirefoxProfilespd6s648z.default

也可以统一将驱动放到 D:PythonPython36-32Scripts目录下

3、python多版本共存
配置环境变量
PYTHON2_HOME=D:Python27
PYTHON3_HOME=D:Python36-32
path=%PYTHON2_HOME%;%PYTHON2_HOME%Scripts;%PYTHON3_HOME%;%PYTHON3_HOME%Scripts;


安装配置:python2.7.15
将 D:Python27 目录下的 python.exe 改为 python2.exe
将 D:Python27Scripts 目录下的 pip*.py 文件中python路径改为指定的路径

安装配置:python3.6.5
将 D:Python36-32 目录下的 python.exe 改为 python3.exe
将 D:Python36-32Scripts 目录下的 pip*.py 文件中python路径改为指定的路径

安装python3版本以上时,会在c:Windows目录下有个 py.exe文件
通过 py -2 或 py -3 来指定运行哪个python版本,默认执行哪个版本,跟环境变量中配置的参数顺序有关系


cmd中 输入python2进入python2.7环境,输入python3进入python3.6环境

如果在选择安装python时,不选择安装pip,可以使用下面的命令安装和升级pip
安装pip:pip-18.1.tar.gz
下载pip,解压后,进入pip目录执行安装命令可进行安装
pip包地址:https://pypi.org/project/pip/#files
python setup.py install

下载pip安装脚本 get-pip.py :https://bootstrap.pypa.io/

关联python2版本:python2 get-pip.py
关联python3版本:python3 get-pip.py

升级pip3
pip3 install --upgrade pip
python3 -m pip3 install -U pip

安装其他包,则需要先进入Python对应版本的Script目录下,使用pip命令去安装,或是安装第3方包

#安装selenium3
进入目录 D:PythonPython36-32Scripts

pip3 install -U selenium

pip3 install selenium3


验证环境是否搭建成功
进入python3环境
cmd->python3
from selenium import webdriver

IE浏览器:IEDriverServer
当出现下面错误时:Protected Mode settings are not the same for all zones
将IE浏览器中,各站点的安全模式去掉,不勾选
ie_driver = webdriver.Ie()
ie_driver.get("https://www.baidu.com")
ie_driver.quit()

Google浏览器:chromedriver
当出现下面错误时:failed with error EGL_NOT_INITIALIZED 或 No available renderers 重启google浏览器或电脑再试试
chrome_driver = webdriver.Chrome()
chrome_driver.get("https://www.baidu.com")
chrome_driver.quit()

Firfox浏览器:geckodriver
firefox_driver = webdriver.Firefox()
firefox_driver.get("https://www.baidu.com")
firefox_driver.quit()


升级pip
python -m pip install --upgrade pip
python -m pip install --upgrade pip

原文地址:https://www.cnblogs.com/NiceTime/p/10023908.html