首页 文章

如何在Chrome中运行Selenium WebDriver测试用例?

提问于
浏览
141

我试过这个

WebDriver driver = new ChromeDriver();

但我得到的错误是

测试失败:setUp(com.TEST):驱动程序可执行文件的路径必须由webdriver.chrome.driver系统属性设置;有关更多信息,请参阅此处的代码 . 最新版本可以从Link下载

如何让Chrome测试Selenium-WebDriver测试用例?

10 回答

  • 6

    您需要从以下位置下载可执行驱动程序:ChromeDriver Download

    然后,您需要做的就是在创建驱动程序对象之前使用以下内容(已按正确的顺序显示):

    System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
    WebDriver driver = new ChromeDriver();
    

    这是从ChromeDriver Documentation中最有用的指南中提取的 .

  • 21

    here下载镀铬驱动程序的更新版本

    public class Chrome {
    
      public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe");
            WebDriver driver = new ChromeDriver();              
            driver.get("http://www.google.com");
    
        }
    
    }
    

    单击here后,您还可以看到答案

  • 6

    您应该在文件夹中下载chromeDriver,并在PATH变量中添加此文件夹 . 您必须重新启动控制台才能使其正常工作 .

  • 7

    如果您在MacOS上使用自制程序,则可以使用以下命令:

    brew install chromedriver
    

    在没有其他配置的情况下它应该可以正常工作 .

  • 231

    您需要安装chrome驱动程序 . 您可以使用nugget安装此包,如下所示

  • -2

    查找 chromedriver here的最新版本 . 下载后,将其解压缩到python安装的根目录,例如 C:/Program Files/Python-3.5 ,并且's it. You don'甚至需要在任何地方指定路径和/或将 chromedriver 添加到您的路径等 . 我刚刚在一个干净的Python安装上做到了这一点 .

  • 6

    您可以使用以下代码使用Selenium网络驱动程序在Chrome中运行测试用例:

    import java.io.IOException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class ChromeTest {
    
        /**
         * @param args
         * @throws InterruptedException
         * @throws IOException 
         */
        public static void main(String[] args) throws InterruptedException, IOException {
            // Telling the system where to find the Chrome driver
            System.setProperty(
                    "webdriver.chrome.driver",
                    "E:/chromedriver_win32/chromedriver.exe");
    
            WebDriver webDriver = new ChromeDriver();
    
            // Open google.com
            webDriver.navigate().to("http://www.google.com");
    
            String html = webDriver.getPageSource();
    
            // Printing result here.
            System.out.println(html);
    
            webDriver.close();
            webDriver.quit();
        }
    }
    
  • 18

    下载最新版本的chrome驱动程序并使用此代码:

    System.setProperty("webdriver.chrome.driver", " path of chromedriver.exe");
    WebDriver driver= new ChromeDriver();
    driver.manage().window().maximize();
    Thread.sleep(10000);
    driver.get("http://stackoverflow.com");
    
  • 4

    以上所有答案都是正确的,以下是对问题和解决方案的深入探讨 .

    例如,selenium中的驱动程序构造函数

    WebDriver driver = new ChromeDriver();
    

    搜索驱动程序可执行文件,在这种情况下,chrome驱动程序搜索chrome驱动程序可执行文件,以防服务无法找到可执行文件时抛出异常

    这是异常的来源(注意检查状态方法)

    /**
       *
       * @param exeName Name of the executable file to look for in PATH
       * @param exeProperty Name of a system property that specifies the path to the executable file
       * @param exeDocs The link to the driver documentation page
       * @param exeDownload The link to the driver download page
       *
       * @return The driver executable as a {@link File} object
       * @throws IllegalStateException If the executable not found or cannot be executed
       */
      protected static File findExecutable(
          String exeName,
          String exeProperty,
          String exeDocs,
          String exeDownload) {
        String defaultPath = new ExecutableFinder().find(exeName);
        String exePath = System.getProperty(exeProperty, defaultPath);
        checkState(exePath != null,
            "The path to the driver executable must be set by the %s system property;"
                + " for more information, see %s. "
                + "The latest version can be downloaded from %s",
                exeProperty, exeDocs, exeDownload);
    
        File exe = new File(exePath);
        checkExecutable(exe);
        return exe;
      }
    

    以下是检查状态方法,它抛出异常

    /**
       * Ensures the truth of an expression involving the state of the calling instance, but not
       * involving any parameters to the calling method.
       *
       * <p>See {@link #checkState(boolean, String, Object...)} for details.
       */
      public static void checkState(
          boolean b,
          @Nullable String errorMessageTemplate,
          @Nullable Object p1,
          @Nullable Object p2,
          @Nullable Object p3) {
        if (!b) {
          throw new IllegalStateException(format(errorMessageTemplate, p1, p2, p3));
        }
      }
    

    SOLUTION :在创建驱动程序对象之前设置系统属性,如下所示

    System.setProperty("webdriver.gecko.driver", "path/to/chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    

    以下是驱动程序服务搜索驱动程序可执行文件的代码段(用于chrome和firefox):

    Chrome:

    @Override
        protected File findDefaultExecutable() {
          return findExecutable("chromedriver", CHROME_DRIVER_EXE_PROPERTY,
              "https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver",
              "http://chromedriver.storage.googleapis.com/index.html");
        }
    

    FireFox:

    @Override
     protected File findDefaultExecutable() {
          return findExecutable(
            "geckodriver", GECKO_DRIVER_EXE_PROPERTY,
            "https://github.com/mozilla/geckodriver",
            "https://github.com/mozilla/geckodriver/releases");
        }
    

    其中CHROME_DRIVER_EXE_PROPERTY =“webdriver.chrome.driver”和GECKO_DRIVER_EXE_PROPERTY =“webdriver.gecko.driver”

    类似于其他浏览器的情况,以下是可用浏览器实现列表的快照

    enter image description here

  • 0

    下载chromedriver的exe并将其提取到当前项目位置 . 在这里链接,我们可以在这里下载最新版本的chromedriver .

    https://sites.google.com/a/chromium.org/chromedriver/

    这里是启动浏览器的简单代码并导航到url .

    System.setProperty("webdriver.chrome.driver", "path of chromedriver.exe");
    
    WebDriver driver = new ChromeDriver();
    
    driver.get("https://any_url.com");
    

相关问题