无法打开Chrome浏览器,代码如下

时间:2016-05-03 07:51:44

标签: webdriver

public class Chrome 
{

WebDriver driver = new ChromeDriver();

@Before   
public void setup()   
{   
System.setProperty("webdriver.chrome.driver","C:\\Users\\test\\Desktop\\Downloads");   
driver.get("http://google.com");   
driver.manage().window().maximize();   
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);   
}     

@Test
public void test()

{
driver.findElement(By.xpath(".//*[@id='menu-item-21']/a")).click();    
}}
  

错误java.lang.IllegalStateException:驱动程序的路径   必须由webdriver.chrome.driver系统属性设置可执行文件;   有关更多信息,请参阅   https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver。最新的   版本可以从中下载   http://chromedriver.storage.googleapis.com/index.html

at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
    at org.openqa.selenium.chrome.ChromeDriverService.access$0(ChromeDriverService.java:1)
    at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
    at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116)
    at com.example.tests.Chrome.<init>(Chrome.java:15)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

4 个答案:

答案 0 :(得分:0)

请设置chrome驱动程序的完整路径,而不是chromedriver目录

System.setProperty("webdriver.chrome.driver","C:\\Users\\test\\Desktop\\Downloads\\chromedriver.exe");

答案 1 :(得分:0)

尝试在设置系统属性后初始化驱动程序。请在下面找到示例代码。

public class Chrome 
{
    WebDriver driver;

    @BeforeSuite  
    public void setup()   
    {   
         System.setProperty("webdriver.chrome.driver","C:\\Users\\test\\Desktop\\Downloads");
         // Initializing the chromdriver here
         driver = new ChromeDriver();
         driver.get("http://google.com");   
         driver.manage().window().maximize();   
         driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);   
    }     
}

希望这有帮助

答案 2 :(得分:0)

应在系统属性中设置chrome驱动程序路径。 公共课Chromes {     WebDriver驱动程序;

@BeforeSuite  
public void setup()   
{   
     System.setProperty("webdriver.chrome.driver","C:\\Users\\test\\Desktop\\Downloads\\chromedriver.exe");
     // Initializing the chromdriver here
     driver = new ChromeDriver();
     driver.get("http://google.com");   
     driver.manage().window().maximize();   
     driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);   
}     

}

答案 3 :(得分:-1)

再次尝试&amp;将文件从上面的路径复制到桌面上的任何文件夹,它工作。问题是存在.exe的文件夹或路径。

如果您遇到此类问题,请复制chromedriver.exe文件夹并将其放在其他文件夹中然后重试。

谢谢!