org.openqa.selenium.WebDriverException: disconnected: received Inspector.detached event error during

Posted

技术标签:

【中文标题】org.openqa.selenium.WebDriverException: disconnected: received Inspector.detached event error during test execution using Selenium and Chromedriver【英文标题】: 【发布时间】:2020-04-09 15:39:17 【问题描述】:

我知道 Selenium 是一种自动化测试工具,但我正在尝试将它用于 RPA(与我的工作环境有关)

我已经下载了 java 代码,它完全按照我想要的方式运行。它从 Excel 表中获取数据,将数据转换为 Java 对象,然后将数据输入到 Web 应用程序中。它在前 20-25 个条目中运行良好,然后开始变慢,最终导致应用程序崩溃。我有几千行要输入。

我尝试使用禁用缓存启动 chrome webdriver,但没有解决问题。

这是禁用缓存的代码。

System.setProperty("webdriver.chrome.driver","C:\\\\DRIVERS\\\\chromedriver.exe");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability("applicationCacheEnabled", false);
WebDriver webDriver = new ChromeDriver(cap);
WebDriverWait wait = new WebDriverWait(webDriver, 20);
javascriptExecutor js = (JavascriptExecutor) webDriver;

我正在使用 for each 循环来键入数据。

/* Add vehicle data */
            for(VehicleData vehicleData : testDataList) 

                Thread.sleep(2500);

                /* Add new inspection */

                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/div[2]/div/mat-tab-group/div/mat-tab-body[1]/div/div[2]/div/div/button")).click();

                js.executeScript("window.scrollBy(0,-1000)");

                DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
                LocalDateTime now = LocalDateTime.now();
                String inspectionDate = dtf.format(now).toString();

                Thread.sleep(5000);

                webDriver.findElement(By.name("InspectionDate")).click();
                webDriver.findElement(By.name("InspectionDate")).sendKeys(inspectionDate);
                webDriver.findElement(By.name("InspectionDate")).sendKeys(Keys.ESCAPE);

                Thread.sleep(1000);

                webDriver.findElement(By.id("cmbCity")).sendKeys(vehicleData.getCityMaster());
                webDriver.findElement(By.id("cmbCity")).sendKeys(Keys.ARROW_DOWN);
                webDriver.findElement(By.id("cmbCity")).sendKeys(Keys.ENTER);

                Thread.sleep(1000);

                webDriver.findElement(By.id("cmbvehicleTypeNew")).sendKeys(surveyType);
                webDriver.findElement(By.id("cmbvehicleTypeNew")).sendKeys(Keys.ARROW_DOWN);
                webDriver.findElement(By.id("cmbvehicleTypeNew")).sendKeys(Keys.ENTER);

                Thread.sleep(1000);

                webDriver.findElement(By.id("cmbWheel")).sendKeys("4");
                webDriver.findElement(By.id("cmbWheel")).sendKeys(Keys.ARROW_DOWN);
                webDriver.findElement(By.id("cmbWheel")).sendKeys(Keys.ENTER);

                Thread.sleep(1000);

                webDriver.findElement(By.id("maker")).sendKeys(vehicleData.getMake());
                webDriver.findElement(By.id("maker")).sendKeys(Keys.ARROW_DOWN);
                webDriver.findElement(By.id("maker")).sendKeys(Keys.ENTER);

                Thread.sleep(1000);

                webDriver.findElement(By.id("model")).sendKeys(vehicleData.getModelMaster());
                webDriver.findElement(By.id("model")).sendKeys(Keys.ARROW_DOWN);
                webDriver.findElement(By.id("model")).sendKeys(Keys.ENTER);

                Thread.sleep(1000);

                webDriver.findElement(By.id("cmbchassis")).click();
                webDriver.findElement(By.id("cmbchassis")).sendKeys("2s-2d");

                Thread.sleep(1000);

                webDriver.findElement(By.id("cmbchassis")).sendKeys(Keys.ARROW_DOWN);
                webDriver.findElement(By.id("cmbchassis")).sendKeys(Keys.ENTER);

                Thread.sleep(3000);

                js.executeScript("window.scrollBy(0,1000)");

                /* Enter Front Left Data */
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[1]/td[2]/div[1]/mat-form-field/div/div[1]/div/input"))).sendKeys(vehicleData.getSizeFL());
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[1]/td[2]/div[1]/mat-form-field/div/div[1]/div/input"))).sendKeys(Keys.ARROW_DOWN);
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[1]/td[2]/div[1]/mat-form-field/div/div[1]/div/input"))).sendKeys(Keys.ENTER);

                Thread.sleep(1000);

                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[1]/td[2]/div[2]/div[1]/mat-form-field/div/div[1]/div/input")).sendKeys(vehicleData.getBrandFL());
                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[1]/td[2]/div[2]/div[1]/mat-form-field/div/div[1]/div/input")).sendKeys(Keys.ARROW_DOWN);
                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[1]/td[2]/div[2]/div[1]/mat-form-field/div/div[1]/div/input")).sendKeys(Keys.ENTER);

                Thread.sleep(1000);

                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[1]/td[2]/div[2]/div[2]/mat-form-field/div/div[1]/div/input")).sendKeys(vehicleData.getPatternFL());
                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[1]/td[2]/div[2]/div[2]/mat-form-field/div/div[1]/div/input")).sendKeys(Keys.ARROW_DOWN);
                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[1]/td[2]/div[2]/div[2]/mat-form-field/div/div[1]/div/input")).sendKeys(Keys.ENTER);

                /* Enter Front Right Data */
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[2]/td[2]/div[1]/mat-form-field/div/div[1]/div/input"))).sendKeys(vehicleData.getSizeFR());
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[2]/td[2]/div[1]/mat-form-field/div/div[1]/div/input"))).sendKeys(Keys.ARROW_DOWN);
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[2]/td[2]/div[1]/mat-form-field/div/div[1]/div/input"))).sendKeys(Keys.ENTER);

                Thread.sleep(1000);

                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[2]/td[2]/div[2]/div[1]/mat-form-field/div/div[1]/div/input")).sendKeys(vehicleData.getBrandFR());
                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[2]/td[2]/div[2]/div[1]/mat-form-field/div/div[1]/div/input")).sendKeys(Keys.ARROW_DOWN);
                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[2]/td[2]/div[2]/div[1]/mat-form-field/div/div[1]/div/input")).sendKeys(Keys.ENTER);

                Thread.sleep(1000);

                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[2]/td[2]/div[2]/div[2]/mat-form-field/div/div[1]/div/input")).sendKeys(vehicleData.getPatternFR());
                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[2]/td[2]/div[2]/div[2]/mat-form-field/div/div[1]/div/input")).sendKeys(Keys.ARROW_DOWN);
                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[2]/td[2]/div[2]/div[2]/mat-form-field/div/div[1]/div/input")).sendKeys(Keys.ENTER);

                /* Enter Rear Left Data */
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[3]/td[2]/div[1]/mat-form-field/div/div[1]/div/input"))).sendKeys(vehicleData.getSizeRL());
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[3]/td[2]/div[1]/mat-form-field/div/div[1]/div/input"))).sendKeys(Keys.ARROW_DOWN);
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[3]/td[2]/div[1]/mat-form-field/div/div[1]/div/input"))).sendKeys(Keys.ENTER);

                Thread.sleep(1000);

                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[3]/td[2]/div[2]/div[1]/mat-form-field/div/div[1]/div/input")).sendKeys(vehicleData.getBrandRL());
                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[3]/td[2]/div[2]/div[1]/mat-form-field/div/div[1]/div/input")).sendKeys(Keys.ARROW_DOWN);
                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[3]/td[2]/div[2]/div[1]/mat-form-field/div/div[1]/div/input")).sendKeys(Keys.ENTER);

                Thread.sleep(1000);

                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[3]/td[2]/div[2]/div[2]/mat-form-field/div/div[1]/div/input")).sendKeys(vehicleData.getPatternRL());
                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[3]/td[2]/div[2]/div[2]/mat-form-field/div/div[1]/div/input")).sendKeys(Keys.ARROW_DOWN);
                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[3]/td[2]/div[2]/div[2]/mat-form-field/div/div[1]/div/input")).sendKeys(Keys.ENTER);

                /* Enter Rear Right Data */
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[4]/td[2]/div[1]/mat-form-field/div/div[1]/div/input"))).sendKeys(vehicleData.getSizeRL());
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[4]/td[2]/div[1]/mat-form-field/div/div[1]/div/input"))).sendKeys(Keys.ARROW_DOWN);
                wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[4]/td[2]/div[1]/mat-form-field/div/div[1]/div/input"))).sendKeys(Keys.ENTER);

                Thread.sleep(1000);

                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[4]/td[2]/div[2]/div[1]/mat-form-field/div/div[1]/div/input")).sendKeys(vehicleData.getBrandRL());
                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[4]/td[2]/div[2]/div[1]/mat-form-field/div/div[1]/div/input")).sendKeys(Keys.ARROW_DOWN);
                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[4]/td[2]/div[2]/div[1]/mat-form-field/div/div[1]/div/input")).sendKeys(Keys.ENTER);

                Thread.sleep(1000);

                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[4]/td[2]/div[2]/div[2]/mat-form-field/div/div[1]/div/input")).sendKeys(vehicleData.getPatternRL());
                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[4]/td[2]/div[2]/div[2]/mat-form-field/div/div[1]/div/input")).sendKeys(Keys.ARROW_DOWN);
                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[2]/form/div[10]/kendo-grid/div/table/tbody/tr[4]/td[2]/div[2]/div[2]/mat-form-field/div/div[1]/div/input")).sendKeys(Keys.ENTER);

                Thread.sleep(1000);

                webDriver.findElement(By.xpath("/html/body/app-component/div/div/mat-sidenav-container/mat-sidenav-content/div[1]/div[2]/count-survey-component/count-survey-vehicle/count-survey-addedit-inspection/div[1]/span[3]/button")).click();

                Thread.sleep(2500);

                js.executeScript("window.scrollTo(0,0)");

错误是:

    [1576641990.504][WARNING]: Timed out connecting to Chrome, retrying...
[1576644529.565][SEVERE]: Timed out receiving message from renderer: 600.000
org.openqa.selenium.TimeoutException: timeout
  (Session info: chrome=79.0.3945.79)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'SGLT009696', ip: '10.225.5.54', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '12.0.2'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities acceptInsecureCerts: false, browserName: chrome, browserVersion: 79.0.3945.79, chrome: chromedriverVersion: 78.0.3904.70 (edb9c9f3de024..., userDataDir: C:\Users\RAY~1.TON\AppData\..., goog:chromeOptions: debuggerAddress: localhost:61927, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: implicit: 0, pageLoad: 300000, script: 30000, unhandledPromptBehavior: dismiss and notify
Session ID: 9a644474b16f372400389760be2c942b
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:489)
    at rpa.EnterDataToWeb.enterDataToWeb(EnterDataToWeb.java:152)
    at Main.main(Main.java:174)

我了解到在测试期间尝试打开 Chrome DevTools 时会发生这种情况,但我的代码根本没有这样做。请指教。

是 chrome 缓存已满的问题吗?如果是这样,我必须每 30 个左右的条目清除一次吗?

【问题讨论】:

请添加错误和您描述的代码。 使用完整的错误堆栈跟踪更新问题。 您的资源可能用完了?尝试在测试开始/结束时调用 System.gc() 添加了超时的代码 @Jonx,谢谢,会尝试并在此处更新 【参考方案1】:

只需添加这一行:

chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);

这应该可以解决问题。

【讨论】:

【参考方案2】:

此错误消息...

org.openqa.selenium.WebDriverException: disconnected: received Inspector.detached event

...暗示 WebDriverException 是由于 Inspector.detached 事件而引发的。


从您的代码试验来看,很明显您使用Thread.sleep(n); 等待元素可点击 / interacable 调用sendKeys() 这不是理想的方式,如:

Thread.sleep() 将当前线程的执行挂起给定的秒数。该参数可以是一个浮点数,以指示更精确的睡眠时间。实际的挂起时间可能少于请求的时间,因为任何捕获的信号都会在执行该信号的捕获例程后终止 sleep()。此外,由于系统中其他活动的调度,暂停时间可能比请求的时间长。

背靠背调用sendKeys()Thread.sleep() 可能会模拟发送以下任一内容:

Ctrl + Shift + I Ctrl + Shift + J Ctrl + Shift + C F12

每个都将尝试打开google-chrome-devtools

现在,根据文章DevTools window keeps closing,如果您尝试打开google-chrome-devtools,ChromeDriver 会自动断开连接。


解决方案

完整的证明解决方案是将Thread.sleep() 和ImplicitWait 的所有实例替换为ExplicitWait


其他注意事项

根据DesiredCapabilities 的文档,applicationCacheEnabled 是一种读/写功能,它采用 boolean 值并配置会话是否可以与应用程序缓存交互。 p>


实施

applicationCacheEnabled可以通过以下两种方式进行配置:

DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setCapability("applicationCacheEnabled", false);

或者

DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setCapability(CapabilityType.SUPPORTS_APPLICATION_CACHE, false);

因此,您的代码块将是:

System.setProperty("webdriver.chrome.driver","C:\\DRIVERS\\chromedriver.exe");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.SUPPORTS_APPLICATION_CACHE, false);
ChromeOptions opt = new ChromeOptions();
opt.merge(cap);
WebDriver driver = new ChromeDriver(opt);

参考

您可以在以下位置找到相关讨论:

How to fix unknown error: unhandled inspector error: “Cannot find context with specified id”

【讨论】:

感谢@DebanjanB,将尝试更正代码,看看会发生什么

以上是关于org.openqa.selenium.WebDriverException: disconnected: received Inspector.detached event error during的主要内容,如果未能解决你的问题,请参考以下文章