zoukankan      html  css  js  c++  java
  • 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)成功运行界面

     
  • 相关阅读:
    JAVA课程作业01
    《大道至简》第二章读后感
    《大道至简》第一章读后感
    制作Linux镜像,将腾讯云服务器系统制成镜像
    postman数据驱动
    Navicat Premium 连接Oracle数据库报错 instant Client LIght : unsupported server charcter ser ZHS16GBK
    查看python位数
    AutoItLibrary安装过程中遇到的坑
    hyrobot使用
    有这样一道智力题:“某商店规定:三个空汽水瓶
  • 原文地址:https://www.cnblogs.com/zhangyating/p/8023089.html
Copyright © 2011-2022 走看看