python+selenium 启动谷歌和火狐下面的flash

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.40v66-68
v2.39v66-68
v2.38v65-67
v2.37v64-66
v2.36v63-65
v2.35v62-64
v2.34v61-63
v2.33v60-62
v2.32v59-61
v2.31v58-60
v2.30v58-60
v2.29v56-58
v2.28v55-57
v2.27v54-56
v2.26v53-55
v2.25v53-55
v2.24v52-54
v2.23v51-53
v2.22v49-52
v2.21v46-50
v2.20v43-48
v2.19v43-47
v2.18v43-46
v2.17v42-43
v2.13v42-45
v2.15v40-43
v2.14v39-42
v2.13v38-41
v2.12v36-40
v2.11v36-40
v2.10v33-36
v2.9v31-34
v2.8v30-33
v2.7v30-33
v2.6v29-32
v2.5v29-32
v2.4v29-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)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值