Firefox更新后无法使用Selenium打开浏览器
我在Ubuntu Desktop 16.04上使用Selenium WebDriver,并且无法打开浏览器。 Firefox更新后出现以下错误(在此之前,它一切正常):
Traceback (most recent call last):
File "test.py", line 6, in <module>
driver = webdriver.Firefox()
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 81, in __init__
self.binary, timeout)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 51, in __init__
self.binary.launch_browser(self.profile, timeout=timeout)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 68, in launch_browser
self._wait_until_connectable(timeout=timeout)
File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 98, in _wait_until_connectable
raise WebDriverException("The browser appears to have exited "
selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.
从Firefox版本47.0开始(一段时间后出现),由于此版本中引入了错误,因此必须使用新驱动程序(由mozilla而不是硒创建)连接到Firefox。 从Firefox 48.0版开始,旧的驱动程序将完全不再使用,只能使用Marionette,所以最好现在切换。 请参阅:适用于Firefox的Marionette Webdriver
下载驱动程序(在OSX只使用brew install geckodriver
),重命名可执行文件wires.exe
在Windows,或wires
上* nix系统,并确保可执行文件存在于系统路径,然后在你的程序中使用这个驱动程序,而旧驱动程序的使用如下:
当使用本地webdriver时:
蟒蛇:
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
driver = webdriver.Firefox(capabilities=firefox_capabilities)
红宝石:
driver = Selenium::WebDriver.for :firefox, marionette: true
使用Javascript:
var capabilities = Capabilities.firefox();
capabilities.set('marionette', true);
var driver = new webdriver.Builder().withCapabilities(capabilities).build();
Java的:
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
Webdriver driver = new FirefoxDriver(capabilities);
C#:
var driver = new FirefoxDriver(new FirefoxOptions());
使用硒网格时:
当使用硒网格时,驱动程序应该出现在网格中所有机器的路径上。
蟒蛇:
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
driver = webdriver.Firefox(capabilities=firefox_capabilities)
红宝石:
caps = Selenium::WebDriver::Remote::Capabilities.firefox marionette: true
driver = Selenium::WebDriver.for :firefox, desired_capabilities: caps
Java的:
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
// Set Marionette on so the Grid will use this instead of normal FirefoxDriver
capabilities.setCapability("marionette", true);
WebDriver driver = new RemoteWebDriver(capabilities);
C#:
DesiredCapabilities capabilities = DesiredCapabilities.Firefox();
// Set Marionette on so the Grid will use this instead of normal FirefoxDriver
capabilities.SetCapability("marionette", true);
var driver = new RemoteWebDriver(capabilities);
固定 :此时的解决方案是降级Firefox! 运行此命令可获取可用Firefox版本的列表。
apt-cache show firefox | grep Version
我的结果:
Version: 47.0+build3-0ubuntu0.16.04.1
Version: 45.0.2+build1-0ubuntu1
安装:
sudo apt-get install firefox=45.0.2+build1-0ubuntu1
要保留此版本并禁止更新:
sudo apt-mark hold firefox
如果你想要保留firefox版本并允许更新:
sudo apt-mark unhold firefox
sudo apt-get upgrade
解决方案 :将Firefox升级到47.0.1,将Selenium升级到2.53.1。
这种组合对我有用。
有关更多详细信息,请参阅https://stackoverflow.com/a/37728659/6469532
链接地址: http://www.djcxy.com/p/62753.html上一篇: Can't open browser with Selenium after Firefox update
下一篇: Python Selenium setting path to firefox profile (ubuntu)