1.安装火狐
selenium 3.7.0
Firefox 54.0
Python 3.6.2
geckodriver-v0.19.1-win64
一、过程中遇到的问题:
1、报错:selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities
之前用的Firefox 50.0 ,更新浏览器至 54.0版本后,此条报错消失
浏览器版本再高的情况下会出现selenium IDE不能录制
太多问题记不住,只顾着解决问题了,这个是坑了我半天时间
2、报错:selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
geckodriver未放在环境变量下
搭建过程中Python、Firefox、geckodriver都要添加到环境变量下,可直接把geckodriver放在Python的安装文件里(因为python已经添加到环境变量下)
3、报错:selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status: 0
这条同样是因为浏览器版本低,试了firefox53.0报此条错误,升级到firefox 54.0后报错消失
二、Firefox历史版本下载地址: http://ftp.mozilla.org/pub/firefox/releases/
Windows环境下以下载firefox54.0 64位为例:
1、上面的链接打开,找到下图的目录点进去
2、找到下面的目录点进去
3、页面最下面有个“Dir zh-CN”,就下载这个安装程序
4、下载下来直接安装,并把firefox.exe程序添加到环境变量下
三、geckodriver的下载地址:https://github.com/mozilla/geckodriver/releases
目前最新版本如下:
下载下来之后直接放在Python的安装文件里就可以了(为了geckodriver也在环境变量下)
安装chrome:
使用WebDriver在Chrome浏览器上进行测试时,需要从http://chromedriver.storage.googleapis.com/index.html网址中(PS:http://npm.taobao.org/mirrors/chromedriver/ 这个是淘宝的镜像)下载与本机chrome浏览器对应的驱动程序,驱动程序名为chromedriver;
chromedriver的版本需要和本机的chrome浏览器对应,才能正常使用;
第一步:下载对应版本的chromedriver驱动文件,具体版本请对照文章底部的对应关系表:
如本机的chrome浏览器版本为:版本 61.0.3163.100(正式版本) (64 位),对应的chromedriver版本为2.33.
第二步:下载后把文件解压,然后放到本机chrome浏览器文件路径里,如:
C:\Program Files (x86)\Google\Chrome\Application
第三步:操作完后,就可以使用以下代码直接调起浏览器了:
chromedriver = "C:/你的目录/chromedriver.exe" os.environ["webdriver.chrome.driver"] = chromedriver browser = webdriver.Chrome(chromedriver) # 设置浏览器需要打开的url browser.get('http://www.baidu.com')
附chromedriver与chrome的对应关系表:
chromedriver版本 | 支持的Chrome版本 |
---|---|
v2.40 | v66-68 |
v2.39 | v66-68 |
v2.38 | v65-67 |
v2.37 | v64-66 |
v2.36 | v63-65 |
v2.35 | v62-64 |
v2.34 | v61-63 |
v2.33 | v60-62 |
v2.32 | v59-61 |
v2.31 | v58-60 |
v2.30 | v58-60 |
v2.29 | v56-58 |
v2.28 | v55-57 |
v2.27 | v54-56 |
v2.26 | v53-55 |
v2.25 | v53-55 |
v2.24 | v52-54 |
v2.23 | v51-53 |
v2.22 | v49-52 |
v2.21 | v46-50 |
v2.20 | v43-48 |
v2.19 | v43-47 |
v2.18 | v43-46 |
v2.17 | v42-43 |
v2.13 | v42-45 |
v2.15 | v40-43 |
v2.14 | v39-42 |
v2.13 | v38-41 |
v2.12 | v36-40 |
v2.11 | v36-40 |
v2.10 | v33-36 |
v2.9 | v31-34 |
v2.8 | v30-33 |
v2.7 | v30-33 |
v2.6 | v29-32 |
v2.5 | v29-32 |
v2.4 | v29-32 |
映射表转自:
http://blog.csdn.net/huilan_same/article/details/51896672
chrome启动flash的配置:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chromeOpitons = Options() prefs= { "profile.managed_default_content_settings.images":1, "profile.content_settings.plugin_whitelist.adobe-flash-player":1, "profile.content_settings.exceptions.plugins.*,*.per_resource.adobe-flash-player":1, } chromeOpitons.add_experimental_option('prefs', prefs) driver = webdriver.Chrome('./chromedriver', chrome_options=chromeOpitons)
Firefox启动flash的配置:
from selenium.webdriver import FirefoxOptions opts = FirefoxOptions() opts.add_argument("--headless") option_profile = webdriver.FirefoxProfile() option_profile.set_preference("plugin.state.flash",2) browser = webdriver.Firefox(firefox_profile=option_profile,options=opts)