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)成功運行界面

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM