简单的selenium脚本出错

时间:2015-04-27 05:43:23

标签: java google-chrome selenium selenium-webdriver selenium-chromedriver

我在使用之前一天使用相同的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 个答案:

答案 0 :(得分:1)

您需要在代码中更改的是chromedriver.exe路径语句执行。您需要在发起新chromedriver之前调用它。 只需改变

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

答案 1 :(得分:-1)

检查您是否在指定的路径上有chromedriver.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);