selenium不能启动firefox浏览器,怎么办?
Posted 一个tester的独白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selenium不能启动firefox浏览器,怎么办?相关的知识,希望对你有一定的参考价值。
一、Java(环境:eclipse Oxygen + JDK1.8 + selenium3.8.1 +Junit5 + firefox58.0_64位 + geckodriver V1.09.1_64位)
1.报错信息如下
The path to the driver executable must be set by the webdriver.gecko.driver system property;
for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
1 java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases 2 at com.google.common.base.Preconditions.checkState(Preconditions.java:754) 3 at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124) 4 at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41) 5 at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:141) 6 at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:339) 7 at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:158) 8 at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:120) 9 at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:98) …………
部分源码如下:
@Test public void setUp() throws Exception { try { driver = new FirefoxDriver(); driver.get("https://www.baidu.com/"); System.out.print(driver.getTitle()); driver.quit(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
2.解决方法如下:
根据报错提示意为,缺少驱动。
(1)根据提示链接:https://github.com/mozilla/geckodriver/releases
下载最新驱动
(2)解压下载文件,把解压后的文件放入firefox.exe的同目录,默认为 C:\\Program Files (x86)\\Mozilla Firefox(此处最好配置环境变量path和PATH,这样就不用加上geckdriver的路径了)
(3)在源码中的加入
System.setProperty("webdriver.firefox.marionette", "C:\\\\Program Files (x86)\\\\Mozilla Firefox\\\\geckodriver.exe");
如下:
1 @Test 2 public void setUp() throws Exception { 3 4 try { 5 System.setProperty("webdriver.firefox.marionette", 6 "C:\\\\Program Files (x86)\\\\Mozilla Firefox\\\\geckodriver.exe"); 7 driver = new FirefoxDriver(); 8 driver.get("https://www.baidu.com/"); 9 System.out.print(driver.getTitle()); 10 driver.quit(); 11 } catch (Exception e) { 12 // TODO Auto-generated catch block 13 e.printStackTrace(); 14 } 15 }
二、python(环境:python3.6.2 + pycharm2017,3,1 + selenium3.8.1 + geckodriver V1.09.0_64位 + firefox58.0_64位)
1.报错信息如下:
selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities
driver = webdriver.Firefox()
提示报错行:
此种情况表示火狐驱动有问题
2.解决方法如下
(1)验证驱动有没有放在firefox路径下,并在PATH和path下配置环境变量(举例:C:\\Program Files (x86)\\Mozilla Firefox)
(2)保证火狐浏览器的版本和驱动的版本要一致,geckodriver V1.09.0+firefox58.0都必须是同样的版本(同64或同32)
源代码如下
1 #!/usr/bin/env python 2 # _*_ coding:utf-8 _*_ 3 # 导入webdriver包 4 from selenium import webdriver 5 from time import sleep 6 7 driver = webdriver.Firefox() 8 driver.get("https://www.baidu.com/") 9 driver.find_element_by_xpath("//input[@id=\'kw\']").send_keys("selenium") 10 driver.find_element_by_xpath("//input[@id=\'su\']").click() 11 sleep(5) 12 driver.quit()
(3)成功运行界面
以上是关于selenium不能启动firefox浏览器,怎么办?的主要内容,如果未能解决你的问题,请参考以下文章
怎么通过selenium启动firefox时启动httpwatch等插件
用selenium时启动的firefox每次都提示导入收藏夹的框子