簡體   English   中英

Selenium 3.8.1,ChromeDriver 2.34-org.openqa.selenium.WebDriverException:無法訪問Chrome

[英]Selenium 3.8.1, ChromeDriver 2.34 - org.openqa.selenium.WebDriverException: chrome not reachable

在執行Selenium-Jvm時,Chrome不會間歇性啟動,並引發“無法訪問Chrome”錯誤。 相同的腳本適用於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提供絕對路徑,但是沒有運氣。 還嘗試為本地主機和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

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

我相信至少您還需要“啟用自動化”

您已在單個“ 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屬性對於所需的WebElement唯一的時 ,才應使用className作為定位符。 如果所需元素的class屬性不是通過visibilityOfElementLocated(By.className("mb12")唯一visibilityOfElementLocated(By.className("mb12") Selenium將嘗試獲取HTML DOM第一個匹配元素的可見性 ,該元素可能不是desired element或被隱藏 。對於定位器className我們應該嘗試使用cssSelectorxpath (在沒有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會話的實例懸而未決,因此無法啟動新的會話。

麻煩還沒解決? 我使用Ch v63.0.3239.132和Chromedriver 2.35

根據您的示例,最簡單的原型對我來說很好用。 我使用絕對路徑加載驅動程序,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM