Selenium 3.8.1,ChromeDriver 2.34 - org.openqa.selenium.WebDriverException:chrome无法访问

时间:2017-12-21 15:58:11

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

在执行Selenium-Jvm时,Chrome不会间歇性地启动并且无法访问" chrome无法访问#34;错误。相同的脚本适用于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设置no_proxy和127.0.0.1

下面提供了更多其他日志。

下面标识为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

3 个答案:

答案 0 :(得分:0)

您使用的是什么版本的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");

我相信至少你还需要&#34; enable-automation&#34;

答案 1 :(得分:0)

您在一个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")));

值得一提的是,只有在 className 属性为唯一<时,才应使用 class 作为定位器< / strong>表示所需的WebElement。如果 class visibilityOfElementLocated(By.className("mb12")所需元素的 Selenium 属性不唯一,则会尝试可见性 HTML DOM 中第一个匹配元素的strong>,可能是您的desired element隐藏。因此,我们应该尝试使用 className cssSelector (来自 xpath ,而不是定位器 id 缺少namelinkTextpartialLinkTexttagNamecssSelector)。

解决方案:

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

  • 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
    

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

Chrome Browser

可能的原因是您正在尝试循环 10 次迭代以启动 Chrome Browser 。由于之前 {{1}} 会话的悬空情况,无法启动新会话。

答案 2 :(得分:0)

麻烦还没有解决? 我使用Ch v63.0.3239.132和Chromedriver 2.35

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

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