【问题标题】:Selenium 3.8.1, ChromeDriver 2.34 - org.openqa.selenium.WebDriverException: chrome not reachableSelenium 3.8.1、ChromeDriver 2.34 - org.openqa.selenium.WebDriverException:无法访问 chrome
【发布时间】:2017-12-21 15:58:11
【问题描述】:

在执行 Selenium-Jvm 时,Chrome 不会间歇性启动并抛出“chrome not reachable”错误。相同的脚本适用于 IE

下面是代码。我使用的是 Chrome 浏览器版本 63.0.3239.84(官方版本)(64 位)。

package sampleSelenium;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class RunScript {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        WebDriver driver;
        System.setProperty("webdriver.chrome.driver", "D:/browser_drivers/chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("disable-infobars");

        for (int i=0; i<10;i++)
        {
            try {

                driver = new ChromeDriver(options);

                driver.manage().deleteAllCookies();
                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                driver.navigate().to("https://XXXX.XXXX.com/");
                Dimension dimension = new Dimension(1920, 1080);
                driver.manage().window().setSize(dimension);

                String currentWindow = driver.getWindowHandle();
                driver.switchTo().window(currentWindow);

                WebElement elm = driver.findElement(By.cssSelector("#username"));
                System.out.println(elm.isDisplayed());
                elm = driver.findElement(By.cssSelector("#password"));
                System.out.println(elm.isDisplayed());
                elm = driver.findElement(By.cssSelector("#Login"));
                System.out.println(elm.isDisplayed());

                driver.findElement(By.cssSelector("#username")).sendKeys("XXX");
                driver.findElement(By.cssSelector("#password")).sendKeys("XXX");
                driver.findElement(By.cssSelector("#Login")).click();

                WebDriverWait wait = new WebDriverWait(driver, 2);
                System.out.println("waiting");
                try {
                    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("mb12")));

                }
                catch(Exception e) {
                    System.out.println(e);
                };

                driver.close();
                driver.quit();

            }
            catch(Exception e) {
                System.out.println(e);
            };


        }

    }

}


*********Error***********

    Starting ChromeDriver 2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1) on port 5871
    Only local connections are allowed.
    Dec 21, 2017 9:14:09 AM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: OSS
    true
    true
    true
    waiting
    org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.className: mb12 (tried for 2 second(s) with 500 MILLISECONDS interval)
    Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:14.666Z'
    System info: host: 'XXXXXXX', ip: 'XXXXXXX', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_151'
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    Capabilities {acceptSslCerts: true, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.34.522940 (1a76f96f66e3ca..., userDataDir: C:\Users\XXXX\AppData\Lo...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 63.0.3239.108, webStorageEnabled: true}
    Session ID: c7c48eb76525894292da06d1e5ed8f07
    Starting ChromeDriver 2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1) on port 31553
    Only local connections are allowed.
    org.openqa.selenium.WebDriverException: chrome not reachable
      (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 61.61 seconds
    Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:14.666Z'
    System info: host: 'XXXXXX', ip: 'XXXXXXX', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_151'
    Driver info: driver.version: ChromeDriver

我们参考了所有旧帖子,并尝试为 chrome 二进制文件和 chromedriver 提供绝对路径,但没有成功。还尝试为 localhost 和 127.0.0.1 设置 no_proxy

下面提供了更多附加日志。

下面 id 为 100 的日志不起作用,调试日志中缺少一些操作

11:41:47,744 DEBUG http-outgoing-100 << "HTTP/1.1 200 OK[\r][\n]"
11:41:47,744 DEBUG http-outgoing-100 << "Content-Length:230[\r][\n]"
11:41:47,744 DEBUG http-outgoing-100 << "Content-Type:application/json; charset=utf-8[\r][\n]"
11:41:47,744 DEBUG http-outgoing-100 << "Connection:close[\r][\n]"
11:41:47,744 DEBUG http-outgoing-100 << "[\r][\n]"
11:41:47,744 DEBUG http-outgoing-100 << "{"sessionId":"fdebdff9e23b0d3715fb3b903cdf082b","status":100,"value":{"message":"chrome not reachable\n  (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86_64)"}}"
11:41:47,744 DEBUG http-outgoing-100 << HTTP/1.1 200 OK
11:41:47,744 DEBUG http-outgoing-100 << Content-Length:230
11:41:47,744 DEBUG http-outgoing-100 << Content-Type:application/json; charset=utf-8
11:41:47,744 DEBUG http-outgoing-100 << Connection:close
11:41:47,744 DEBUG http-outgoing-100: Close connection
11:41:47,744 DEBUG Connection discarded
11:41:47,744 DEBUG Connection released: [id: 100][route: {}->http://localhost:48950][total kept alive: 0; route allocated: 0 of 2000; total allocated: 2 of 2000]

当 id 229 正确打开连接时。

11:41:26,756 DEBUG CookieSpec selected: default
11:41:26,756 DEBUG Auth cache not set in the context
11:41:26,756 DEBUG Connection request: [route: {}->http://localhost:39283][total kept alive: 0; route allocated: 0 of 2000; total allocated: 3 of 2000]
11:41:26,756 DEBUG Connection leased: [id: 229][route: {}->http://localhost:39283][total kept alive: 0; route allocated: 1 of 2000; total allocated: 4 of 2000]
11:41:26,756 DEBUG Opening connection {}->http://localhost:39283
11:41:26,756 DEBUG Connecting to localhost/127.0.0.1:39283
11:41:26,756 DEBUG Connection established 127.0.0.1:50151<->127.0.0.1:39283
11:41:26,756 DEBUG http-outgoing-229: set socket timeout to 10800000
11:41:26,756 DEBUG Executing request DELETE /session/bf4589ba8b779464252afb0a20e55e0c HTTP/1.1
11:41:26,756 DEBUG Target auth state: UNCHALLENGED
11:41:26,756 DEBUG Proxy auth state: UNCHALLENGED
11:41:26,756 DEBUG http-outgoing-229 >> DELETE /session/bf4589ba8b779464252afb0a20e55e0c HTTP/1.1
11:41:26,756 DEBUG http-outgoing-229 >> Host: localhost:39283
11:41:26,756 DEBUG http-outgoing-229 >> Connection: Keep-Alive
11:41:26,756 DEBUG http-outgoing-229 >> User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_131)
11:41:26,756 DEBUG http-outgoing-229 >> Accept-Encoding: gzip,deflate
11:41:26,756 DEBUG http-outgoing-229 >> "DELETE /session/bf4589ba8b779464252afb0a20e55e0c HTTP/1.1[\r][\n]"
11:41:26,756 DEBUG http-outgoing-229 >> "Host: localhost:39283[\r][\n]"
11:41:26,756 DEBUG http-outgoing-229 >> "Connection: Keep-Alive[\r][\n]"
11:41:26,756 DEBUG http-outgoing-229 >> "User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_131)[\r][\n]"
11:41:26,756 DEBUG http-outgoing-229 >> "Accept-Encoding: gzip,deflate[\r][\n]"
11:41:26,756 DEBUG http-outgoing-229 >> "[\r][\n]"
11:41:26,803 DEBUG http-outgoing-229 << "HTTP/1.1 200 OK[\r][\n]"
11:41:26,803 DEBUG http-outgoing-229 << "Content-Length:40[\r][\n]"
11:41:26,803 DEBUG http-outgoing-229 << "Content-Type:application/json; charset=utf-8[\r][\n]"
11:41:26,803 DEBUG http-outgoing-229 << "Connection:close[\r][\n]"
11:41:26,803 DEBUG http-outgoing-229 << "[\r][\n]"
11:41:26,803 DEBUG http-outgoing-229 << "{"sessionId":"","status":0,"value":null}"
11:41:26,803 DEBUG http-outgoing-229 << HTTP/1.1 200 OK
11:41:26,803 DEBUG http-outgoing-229 << Content-Length:40
11:41:26,803 DEBUG http-outgoing-229 << Content-Type:application/json; charset=utf-8
11:41:26,803 DEBUG http-outgoing-229 << Connection:close
11:41:26,803 DEBUG http-outgoing-229: Close connection
11:41:26,803 DEBUG Connection discarded
11:41:26,803 DEBUG Connection released: [id: 229][route: {}->http://localhost:39283][total kept alive: 0; route allocated: 0 of 2000; total allocated: 3 of 2000]
11:41:26,803 DEBUG Closing connections idle longer than 0 SECONDS

【问题讨论】:

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


    【解决方案1】:

    您使用的是哪个版本的 ChromeDriver?

    最新版本:ChromeDriver 2.34(2017/12/10 发布)

    支持 Chrome v61-63

    以前的版本

    ChromeDriver 2.33

    支持 Chrome v60-62

    此外,您可能需要更多选择。以下是我使用的:

    args.add("enable-automation");
    args.add("test-type=browser");
    args.add("disable-plugins");
    args.add("disable-infobars");
    

    我相信您至少还需要“启用自动化”

    【讨论】:

    • 我们使用的是以下版本:: chrome=63.0.3239.108 chromedriver=2.34.522940
    • @KingKamal,我在答案中添加了更多信息。所以在我查找论点时提交了我的答案。
    • @MivaScott 感谢您的建议,但这并没有解决问题,还有其他建议吗?
    【解决方案2】:

    您在单个 Error Stack Trace 中合并了 2 个不同的不同错误。

    根据第一组错误堆栈跟踪,错误说明如下:

    org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.className: mb12 (tried for 2 second(s) with 500 MILLISECONDS interval)
    

    根据错误日志,错误发生在以下行:

    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("mb12")));
    

    这里值得一提的是,只有当 class 属性为 unique 时,才应使用 className 作为定位器所需的WebElement。如果所需元素的 class 属性不是唯一的,则通过 visibilityOfElementLocated(By.className("mb12") Selenium 将尝试第一个的 visibility HTML DOM 中的匹配元素可能不是是您的desired element隐藏。因此,我们应该尝试使用 cssSelectorxpath 来代替定位器 className(如果没有 idnamelinkTextpartialLinkTexttagName)。

    解决办法:

    您的解决方案是使用以下任一方法:

    • cssSelector

      WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("tagName.class#id")));
      
    • xpath

      WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("//tagName[@class='class_values' and @id='id_value']")));
      

    根据第二组错误堆栈跟踪,错误再次说明如下:

    org.openqa.selenium.WebDriverException: chrome not reachable
      (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 61.61 seconds
    Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:14.666Z'
    System info: host: 'XXXXXX', ip: 'XXXXXXX', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_151'
    Driver info: driver.version: ChromeDriver
    

    可能的原因是您尝试循环 10 次迭代来启动 Chrome Browser。由于先前 Chrome Browser 会话的悬空实例无法启动新会话。

    【讨论】:

    • 我只关心第二个问题,您对解决这个问题有什么建议吗?
    • 尝试在循环中启动Web Browser 不是理想的Test Strategy。不惜一切代价初始化WebDriverWeb Browser 必须平滑无缝。删除循环,从悬空的WebDriverWeb Browser 实例中清理您的系统并执行您的测试。如果您在启动WebDriverWeb Browser 时遇到任何问题,请提出一个包含所有详细信息的新问题。 SO 志愿者很乐意为您提供帮助。
    • 嗨 DebanjanB,感谢您的评论。我们的原始框架使用 cucumber 和 testng,我们在其中并行启动 5 个线程。这就是我们发现“chrome 无法访问”问题的地方。为了简化错误重现,我们创建了上面循环运行的脚本。上面的脚本没有错。由于 driver.close 和 driver.quit 在循环结束时,也没有 chrome 或 chrome 驱动程序的悬空实例。如果您有任何其他解决方法或解决方案,请告诉我。
    【解决方案3】:

    问题仍未解决? 我使用 Ch v63.0.3239.132 和 Chromedriver 2.35

    根据您的示例,最简单的原型对我来说效果很好。 我使用绝对路径来加载驱动程序,如下所示:

    C:\\Users\\...\\IDEA Projects\\AST\\src\\main\\resources\\Drivers\\Chrome\\chromedriver.exe
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-27
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 2017-10-30
      相关资源
      最近更新 更多