java - 错误 Selenium 无法检索文档状态已断开 : received Inspector. 分离事件

标签 java selenium

我有一段代码可以通过网页中的 Selenium 来自动化 Java 测试。 Java代码发布在这个问题中,此外XML中的Pom和我得到的Error也发布在这里。问题是有时并不总是发生,但我不知道问题是什么。我的 Chrome 版本是:版本 78.0.3904.87。提前感谢任何帮助。

System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\afp2\\Downloads\\maven-examples-master\\maven-examples-master\\java-project\\drivers\\chromedriver.exe");
        WebDriver browser = new ChromeDriver();
        browser.get("http://localhost:14612/Modelo_Experto_Amazonas_TC.aspx");
        browser.findElement(By.id("txtLogin")).sendKeys("andres");
        browser.findElement(By.id("txtClave")).sendKeys("burocr");
        browser.findElement(By.id("BLogin")).click();
        if (waitForNotElement(browser, By.id("dtlFinal_imbReporte_216"))) {
            browser.findElement(By.id("dtlFinal_imbReporte_216")).click();
        }
        browser.findElement(By.id("txt_OtrosGastos")).sendKeys("100");
        Select dropdown = new Select(browser.findElement(By.id("ddlTipoCliente")));
        dropdown.selectByVisibleText("DEPENDIENTE");
        browser.findElement(By.id("txtNumeroDocumentoSolicitante")).sendKeys("1722714795");
        browser.findElement(By.id("txt_ingresos")).sendKeys("1500");
        browser.findElement(By.id("btnAceptar")).click();

接下来是 pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mkyong.hashing</groupId>
    <artifactId>java-project</artifactId>
    <packaging>jar</packaging>
    <version>1</version>
    <name>java-project</name>
    <url>http://maven.apache.org</url>

    <properties>
        <!-- https://maven.apache.org/general.html#encoding-warning -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.11</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.1.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.11.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>com.mkyong.hashing.App</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <fork>true</fork>
                    <executable>C:\Program Files\Java\jdk1.8.0_201\bin\javac.exe</executable>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.mkyong.hashing.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <!-- fat-jar -->
            <!-- https://books.sonatype.com/mvnex-book/reference/customizing-sect-custom-packaged.html -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.mkyong.hashing.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>simple-command</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
</project>

我有时遇到的错误是:

[1573159867.544][SEVERE]: Unable to retrieve document state disconnected: received Inspector.detached event
[WARNING] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source)
    at java.lang.reflect.Method.invoke (Unknown Source)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:297)
    at java.lang.Thread.run (Unknown Source)
Caused by: org.openqa.selenium.WebDriverException: disconnected: received Inspector.detached event
  (Session info: chrome=78.0.3904.87)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.17763 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:26:55.152Z'
System info: host: 'ECLTAMFR15', ip: '172.23.142.12', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.35.528161 (5b82f2d2aae0ca..., userDataDir: C:\Users\afp2\AppData\Local...}, 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: 78.0.3904.87, webStorageEnabled: true}
Session ID: 18984819085c0b41581b1a4f7c8b5089
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance (Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance (Unknown Source)
    at java.lang.reflect.Constructor.newInstance (Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable (ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed (ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue (JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode (AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode (AbstractHttpResponseCodec.java:44)
    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:545)
    at org.openqa.selenium.remote.RemoteWebElement.execute (RemoteWebElement.java:279)
    at org.openqa.selenium.remote.RemoteWebElement.click (RemoteWebElement.java:83)
    at com.mkyong.hashing.App.main (App.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown Source)
    at java.lang.reflect.Method.invoke (Unknown Source)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:297)
    at java.lang.Thread.run (Unknown Source)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.031 s
[INFO] Finished at: 2019-11-07T15:51:07-05:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default-cli) on project java-project: An exception occured while executing the Java class. null: InvocationTargetException: disconnected: received Inspector.detached event
[ERROR]   (Session info: chrome=78.0.3904.87)
[ERROR]   (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.17763 x86_64) (WARNING: The server did not provide any stacktrace information)
[ERROR] Command duration or timeout: 0 milliseconds
[ERROR] Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:26:55.152Z'
[ERROR] System info: host: 'ECLTAMFR15', ip: '172.23.142.12', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
[ERROR] Driver info: org.openqa.selenium.chrome.ChromeDriver
[ERROR] Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.35.528161 (5b82f2d2aae0ca..., userDataDir: C:\Users\afp2\AppData\Local...}, 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: 78.0.3904.87, webStorageEnabled: true}
[ERROR] Session ID: 18984819085c0b41581b1a4f7c8b5089
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

最佳答案

当 selenium 尝试访问不再可用的元素时,这是一个常见的异常。

// pseudo code example:
webDriver.get("abc.com");
Element button = webDriver.findElement( button selector );
webDriver.get("xyz.com");
button.click();  // exception here


// another example:
Element button = webDriver.findElement( button selector );

// let's assume, after clicking the button 
// JS performs some UI events and then removes the button from DOM
button.click(); // UI actions performed
button.click(); // exception

也许您应该尝试记录每个操作以找到不稳定的确切行。它应该有助于进一步调查。

关于java - 错误 Selenium 无法检索文档状态已断开 : received Inspector. 分离事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58756893/

相关文章:

java - 使用 Web 服务从数据库检索数据

java - 对 NaN 值使用指数平滑

java - 如何使用java查找操作系统中的用户名

Java CDI。拦截器仅在类中的第一个方法调用中被调用

python - 在 python3 中使用 selenium 无法在页面中找到元素

c# - 从 .NET 调用 Java API - 最佳方法

selenium - 使用 Selenium 修改innerHTML

python - 如何从 YouTube 页面抓取 View

Selenium WebDriverException : Session [null] not available and is not among the last 1000 terminated sessions

C#- Selenium : StaleElementReferenceException on any element on any webpage after refresh or page change