ChromeDriver cannot find Chrome binary问题解决

来源:互联网 发布:windows 10 安装.net 编辑:程序博客网 时间:2024/05/16 13:52

1、运行程序

 @Test
 public void testChrome() throws InterruptedException {
   System.setProperty("webdriver.chrome.driver","D:\\codeware\\eclipse\\eclipse_neon_workspace\\selenium\\resources\\chromedriver.exe");
   WebDriver dr = new ChromeDriver();
   dr.manage().window().maximize();
         dr.get("http://localhost:8080/jenkins/");
         Thread.sleep(2000);
     //  dr.manage().window().maximize();
         dr.findElement(By.id("j_username")).sendKeys("admin");
         dr.findElement(By.name("j_password")).sendKeys("352cba3875a2446abd5935b1a60d726f");
         dr.findElement(By.id("yui-gen1-button")).click();
  
 
 }


2、出现的问题

Starting ChromeDriver (v2.4.226107) on port 9496
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot find Chrome binary
  (Driver info: chromedriver=2.4.226107,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)



3、解决

添加:

  ChromeOptions options = new ChromeOptions();
   options.setBinary("C:/Program Files (x86)/Google/Chrome/chrome.exe");
   WebDriver dr = new ChromeDriver(options);


@Test
 public void testChrome() throws InterruptedException {
   System.setProperty("webdriver.chrome.driver","D:\\codeware\\eclipse\\eclipse_neon_workspace\\selenium\\resources\\chromedriver.exe");
   ChromeOptions options = new ChromeOptions();
   options.setBinary("C:/Program Files (x86)/Google/Chrome/chrome.exe");
   WebDriver dr = new ChromeDriver(options);
   dr.manage().window().maximize();
         dr.get("http://localhost:8080/jenkins/");
         Thread.sleep(2000);
     //  dr.manage().window().maximize();
         dr.findElement(By.id("j_username")).sendKeys("admin");
         dr.findElement(By.name("j_password")).sendKeys("352cba3875a2446abd5935b1a60d726f");
         dr.findElement(By.id("yui-gen1-button")).click();
  
 
 }


之后就能运行了