有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

简单selenium脚本中的java错误

在使用它当时执行的同一chromedriver路径之前的一天,我尝试过使用同一种脚本,但今天,当我尝试使用另一种脚本时,我得到了错误

package ScriptsSelenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class TextBox_EntData {
    public static void main(String[] args){
        WebDriver driver = new ChromeDriver(); 
        System.setProperty("webdriver.chrome.driver","C:\\Users\\Sneha\\Downloads\\chromedriver_win32\\chromedriver.exe");
        driver.get("http://www.gmail.com");
        driver.findElement(By.id("Email")).sendKeys("XXX@YYail.com");
        driver.findElement(By.id("Passwd")).sendKeys("XXXXXX");
        driver.findElement(By.id("signIn")).click();

这是我的错误

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
    at com.google.common.base.Preconditions.checkState(Preconditions.java:197)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:110)
    at org.openqa.selenium.chrome.ChromeDriverService.access$0(ChromeDriverService.java:1)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:118)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:291)
    at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:82)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:117)
    at ScriptsSelenium.TextBox_EntData.main(TextBox_EntData.java:10)

共 (2) 个答案

  1. # 1 楼答案

    代码中唯一需要更改的是chromedriver.exepath语句的执行。在启动新的chromedriver之前,需要调用它。 换衣服

    System.setProperty("webdriver.chrome.driver","C:\\Users\\Sneha\\Downloads\\chromedriver_win32\\chromedriver.exe");
    WebDriver driver = new ChromeDriver(); 
    
  2. # 2 楼答案

    检查一下你有没有铬驱动。exe文件,并使用以下Chrome配置

    DesiredCapabilitiesChromeOptions类基本上允许您为浏览器设置一些选项(比如启动最大化浏览器、设置chromedriver路径等等)

    System.setProperty("webdriver.chrome.driver", "res/chromedriver.exe");
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    ChromeOptions options = new ChromeOptions();
    //this statement will not show any warnings when you launch chrome
    options.addArguments("test-type");
    //start a maximized window 
    options.addArguments("start-maximized");
    //this allows you to use a user profile
    //options.addArguments("user-data-dir=D:/temp/");
    capabilities.setCapability("chrome.binary","res/chromedriver.exe");
    capabilities.setCapability(ChromeOptions.CAPABILITY,options);
    WebDriver driver = new ChromeDriver(capabilities);