Windows环境下使用python selenium 打开Firefox的问题总结

最近需要在windows平台下写爬虫,用selenium + PhantomJS 是甚好的选择,但由于前期调试阶段还是对网页可见比较好,所以就选用了selenium (3.0.1)+ firefox(49.0.2)的组合。

这里写图片描述

但在windows底下,安装了最新版的selenium 3.0.1,直接启动webdriver会报错

from selenium import webdriver
browser = webdriver.Firefox()
browser

抛出

WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

(゚Д゚≡゚Д゚)/

好吧,见招拆招,我就bing了一下(不得不吐槽技术方面的内容百度真是糟糕透了……可惜谷歌最近的翻墙又有点问题。。。)

伪 · 解决方案在此这真的是不完全解决方案,但完成了解决方案的一部分,就是下载一个 geckodriver 并将其路径设置到指定的环境变量底下。

接下来你会发现报另一个错

WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line

正确的打开方式

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
# firfoxdriver 为 你的firefox安装路径
firfoxdriver = 'D:\\KXF_software\\firefox\\firefox.exe'
binary = FirefoxBinary(firfoxdriver)
browser = webdriver.Firefox(firefox_binary=binary,executable_path='C:\\geckodriver\\geckodriver.exe')

终于可以打开浏览器啦~(=▽=)b

具体的原理暂时挖个坑,后续补上~

This release contains a number of fixes for regressions introduced in 0.22.0, where we shipped a significant refactoring to the way geckodriver internally dealt with JSON serialisation. Removed The POST /session/{session id}/element/{element id}/tap endpoint was removed, thanks to Kerem Kat. Changed webdriver crate upgraded to 0.38.0. Fixed desiredCapabilities and requiredCapabilities are again recognised on session creation A regression in 0.22.0 caused geckodriver to recognise desired and required instead of the correct desiredCapabilities and requiredCapabilities. This will have caused significant problems for users who relied on this legacy Selenium-style session creation pattern. Do however note that support for Selenium-styled new session requests is temporary and that this will be removed sometime before the 1.0 release. duration field made optional on pause actions A regression in 0.22.0 caused the pause action primitive to require a duration field. This has now been fixed so that pauses in action chains can be achieved with the default duration. Log level formatted to expected Marionette input A regression in 0.22.0 caused the log level to be improperly formatted when using Firefox pre-releases. This is now fixed so that the requested log level is correctly interpreted by Marionette. temporary field on addon installation made optional A regression in 0.22.0 caused the temporary field for POST /session/{session id}/moz/addon/install to be mandatory. This has now been fixed so that an addon is installed permanently by default. SHA1s in version information uses limited number of characters The SHA1 used in --version when building geckodriver from a git repository is now limited to 12 characters, as it is when building from an hg checkout. This ensures reproducible builds.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值