Selenium UnreachableBrowserException - Java

2019-06-10 06:21发布

System.setProperty("webdriver.chrome.driver","D:/chromedriver.exe");
        WebDriver driver = new ChromeDriver();   
        driver.navigate().to("https://link");
        driver.findElement(By.cssSelector("#username")).sendKeys("id");
        driver.findElement(By.cssSelector("#password")).sendKeys("pass");
        driver.findElement(By.cssSelector("#clientName")).sendKeys("name");
        driver.findElement(By.cssSelector("#submitButton")).click();
        System.out.println("Okay !");

I set property for Chrome Driver. When I run it gives an error. (Below) I searched a lot but didn't found any solution.

Starting ChromeDriver 2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab) on port 10589
Only local connections are allowed.
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T15:15:03.216Z'
System info: host: 'DESKTOP-9HVORCR', ip: '192.168.1.24', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_171'
Driver info: driver.version: ChromeDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:564)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:207)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:130)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
    at zaaa.main.main(main.java:11)
Caused by: java.lang.IllegalArgumentException: Unexpected char 0x131 at 23 in User-Agent value: selenium/3.12.0 (java wındows)
    at okhttp3.Headers$Builder.checkNameAndValue(Headers.java:338)
    at okhttp3.Headers$Builder.add(Headers.java:288)
    at okhttp3.Request$Builder.addHeader(Request.java:177)
    at org.openqa.selenium.remote.internal.OkHttpClient.execute(OkHttpClient.java:85)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:101)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    ... 6 more

I have tried these:

  1. 32/64 bit drivers.
  2. Run it as administrator.
  3. Creating a Try/Catch block.
  4. Other drivers. (Like Operadriver.exe) (Result: Same error)

5条回答
叛逆
2楼-- · 2019-06-10 06:39

Add these lines before instantiating "WebDriver driver = new ChromeDriver();"

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--no-sandbox");

Then add the argument where you have declared Webdriver as shown below:-

WebDriver driver = new ChromeDriver(chromeOptions);

I hope it will help you.

查看更多
Root(大扎)
3楼-- · 2019-06-10 06:47

Here you are using
selenium version : 3.12.0
chromedriver version : 2.40

When i tried to run with these configurations (they are latest at present) I am able to launch the application without any exception and in the logs getting :

Starting ChromeDriver 2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab) on port 35584
Only local connections are allowed.
Jun 21, 2018 2:42:51 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Okay !

which is same as your starting logs. My chrome browser version is :

Version 67.0.3396.87 (Official Build) (64-bit)

I think you should check your .m2 folder and delete

.m2\repository\org\seleniumhq\selenium

folder from there and then again try to download the dependencies.

Also update to latest chrome browser version.

Hope it helps.

查看更多
霸刀☆藐视天下
4楼-- · 2019-06-10 06:48

This is a problem of Turkish Windows. As it is indicated in exception message the 'ı' character in user agent value "selenium/3.12.0 (java wındows)" is the cause of the exception. The problem is the string "WINDOWS" is changed to lower case and it results "wındows" because of TR locale. I found that it is generated in class org.openqa.selenium.remote.http.HttpClientand added Locale.US like this.

String USER_AGENT = String.format(
      "selenium/%s (java %s)",
      new BuildInfo().getReleaseLabel(),
      (Platform.getCurrent().family() == null ?
          Platform.getCurrent().toString().toLowerCase(Locale.US) :
          Platform.getCurrent().family().toString().toLowerCase(Locale.US)));

I compiled the library with my changes and it works now. I also opened a pull request on github.

You can also find my edited fork here.

查看更多
闹够了就滚
5楼-- · 2019-06-10 06:49
Locale.setDefault(new Locale("en", "EN"));

you can also try this. it worked for me.

查看更多
smile是对你的礼貌
6楼-- · 2019-06-10 06:50

I think your windows is not english. I am having the same problem. When I tried the same in an english version windows 10 the code works without any problem.

查看更多
登录 后发表回答