【问题标题】:Selenium Error MainClientExec:103 - Connection discardedSelenium 错误 MainClientExec:103 - 连接被丢弃
【发布时间】:2023-06-10 01:36:01
【问题描述】:

我用 Java 开发了一个 Selenium 项目,它运行良好。但是当我试图从另一个项目中调用它以进行集成时,它给出了这个错误,如下所述。

    2016-07-25 16:29:06 DEBUG RequestAddCookies:122 - CookieSpec selected: default
    2016-07-25 16:29:06 DEBUG RequestAuthCache:76 - Auth cache not set in the context
    2016-07-25 16:29:06 DEBUG PoolingHttpClientConnectionManager:249 - Connection request: [route: {}->http://localhost:13517][total kept alive: 0; route allocated: 0 of 2000; total allocated: 0 of 2000]
    2016-07-25 16:29:06 DEBUG PoolingHttpClientConnectionManager:282 - Connection leased: [id: 0][route: {}->http://localhost:13517][total kept alive: 0; route allocated: 1 of 2000; total allocated: 1 of 2000]
    2016-07-25 16:29:06 DEBUG MainClientExec:234 - Opening connection {}->http://localhost:13517
    2016-07-25 16:29:06 DEBUG DefaultHttpClientConnectionOperator:131 - Connecting to localhost/127.0.0.1:13517
    2016-07-25 16:29:06 DEBUG DefaultHttpClientConnectionOperator:138 - Connection established 127.0.0.1:52920<->127.0.0.1:13517
    2016-07-25 16:29:06 DEBUG DefaultManagedHttpClientConnection:90 - http-outgoing-0: set socket timeout to 10800000
    2016-07-25 16:29:06 DEBUG MainClientExec:255 - Executing request POST /session HTTP/1.1
    2016-07-25 16:29:06 DEBUG MainClientExec:260 - Target auth state: UNCHALLENGED
    2016-07-25 16:29:06 DEBUG MainClientExec:266 - Proxy auth state: UNCHALLENGED
    2016-07-25 16:29:06 DEBUG headers:135 - http-outgoing-0 >> POST /session HTTP/1.1
    2016-07-25 16:29:06 DEBUG headers:138 - http-outgoing-0 >> Content-Type: application/json; charset=utf-8
    2016-07-25 16:29:06 DEBUG headers:138 - http-outgoing-0 >> Content-Length: 122
    2016-07-25 16:29:06 DEBUG headers:138 - http-outgoing-0 >> Host: localhost:13517
    2016-07-25 16:29:06 DEBUG headers:138 - http-outgoing-0 >> Connection: Keep-Alive
    2016-07-25 16:29:06 DEBUG headers:138 - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.1 (Java/1.8.0_77)
    2016-07-25 16:29:06 DEBUG headers:138 - http-outgoing-0 >> Accept-Encoding: gzip,deflate
    2016-07-25 16:29:06 DEBUG wire:72 - http-outgoing-0 >> "POST /session HTTP/1.1[\r][\n]"
    2016-07-25 16:29:06 DEBUG wire:72 - http-outgoing-0 >> "Content-Type: application/json; charset=utf-8[\r][\n]"
    2016-07-25 16:29:06 DEBUG wire:72 - http-outgoing-0 >> "Content-Length: 122[\r][\n]"
    2016-07-25 16:29:06 DEBUG wire:72 - http-outgoing-0 >> "Host: localhost:13517[\r][\n]"
    2016-07-25 16:29:06 DEBUG wire:72 - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
    2016-07-25 16:29:06 DEBUG wire:72 - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.1 (Java/1.8.0_77)[\r][\n]"
    2016-07-25 16:29:06 DEBUG wire:72 - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
    2016-07-25 16:29:06 DEBUG wire:72 - http-outgoing-0 >> "[\r][\n]"
    2016-07-25 16:29:06 DEBUG wire:86 - http-outgoing-0 >> "{"desiredCapabilities":{"browserName":"chrome","chromeOptions":{"args":[],"extensions":[]},"version":"","platform":"ANY"}}"
    2016-07-25 16:29:06 DEBUG wire:72 - http-outgoing-0 << "HTTP/1.1 200 OK[\r][\n]"
    2016-07-25 16:29:06 DEBUG wire:72 - http-outgoing-0 << "Content-Length:689[\r][\n]"
    2016-07-25 16:29:06 DEBUG wire:72 - http-outgoing-0 << "Content-Type:application/json; charset=utf-8[\r][\n]"
    2016-07-25 16:29:06 DEBUG wire:72 - http-outgoing-0 << "Connection:close[\r][\n]"
    2016-07-25 16:29:06 DEBUG wire:72 - http-outgoing-0 << "[\r][\n]"
    2016-07-25 16:29:06 DEBUG wire:86 - http-outgoing-0 << "{"sessionId":"8130f26be5faa0e88b1342ad068e1fd0","status":0,"value":{"acceptSslCerts":true,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"browserName":"chrome","chrome":{"chromedriverVersion":"2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4)","userDataDir":"C:\\Users\\rahal\\AppData\\Local\\Temp\\scoped_dir10508_15086"},"cssSelectorsEnabled":true,"databaseEnabled":false,"handlesAlerts":true,"hasTouchScreen":false,"javascriptEnabled":true,"locationContextEnabled":true,"mobileEmulationEnabled":false,"nativeEvents":true,"platform":"Windows NT","rotatable":false,"takesHeapSnapshot":true,"takesScreenshot":true,"version":"51.0.2704.103","webStorageEnabled":true}}"
    2016-07-25 16:29:06 DEBUG headers:124 - http-outgoing-0 << HTTP/1.1 200 OK
    2016-07-25 16:29:06 DEBUG headers:127 - http-outgoing-0 << Content-Length:689
    2016-07-25 16:29:06 DEBUG headers:127 - http-outgoing-0 << Content-Type:application/json; charset=utf-8
    2016-07-25 16:29:06 DEBUG headers:127 - http-outgoing-0 << Connection:close
    2016-07-25 16:29:06 DEBUG DefaultManagedHttpClientConnection:81 - http-outgoing-0: Close connection
    2016-07-25 16:29:06 DEBUG MainClientExec:103 - Connection discarded

我找到了错误报告的 Jason 数据

{   "sessionId": "8be0897af892264346d7d8f7b3e4e7fe",   "status": 33,  
     "value": {
         "message": "session not created exception\nfrom disconnected: received Inspector.detached event\n  (Session info:
     chrome=51.0.2704.103)\n  (Driver info: chromedriver=2.21.371459
     (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 10.0
     x86_64)"   } }

我添加了Maven依赖如下

<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>review-amender</groupId>
    <artifactId>review-amender</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <description>This project intergrate Dataextractor,Preporcessore, RAUpgradeTest</description>
    <build>
        <!-- <sourceDirectory>src</sourceDirectory> -->
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>it.codegen.rnd</groupId>
            <artifactId>data-extractor</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

问题是程序不会因任何异常而终止,Chrome Web 驱动程序也不会停止其工作进程。
当我从内部运行 Selenium 项目时,它运行良好。但不是从外部运行即使我添加了必要的依赖项。

问题:为什么这个 Selenium 项目表现出不可预测的行为,这是因为 Maven 依赖问题吗?

请帮忙

【问题讨论】:

  • @Evan 我知道其他人可以编辑我的问题并接受它,非常感谢编辑
  • 我能够找到问题。问题是由来自父项目(在我的情况下是“数据提取器”)的依赖项(“log4j”)引起的。我排除了该依赖项并添加“Logback”作为替代记录器,项目开始工作。但是我仍然没有得到为什么会发生这种情况的答案。所以我的问题和赏金仍然有效,直到我得到正确的答案。谢谢。
  • 如果有人能解决这个错误,无论如何我都会提供 50 赏金。如果您需要更多详细信息,请通知我

标签: java eclipse maven selenium


【解决方案1】:

我的猜测是,您所看到的是由于 Maven 依赖问题。我曾经遇到过类似的问题,每当我们尝试访问它的 cookie 时,Chrome WebDriver 就会由于 NumberFormatException 而崩溃。根有一个不兼容的传递依赖(例如,它需要一个特定版本的依赖,而我们的项目正在拉入一个旧版本并且 maven 认为这一切都好)。在我们的例子中,修复是在运行 selenium 测试的测试项目中显式添加对正确版本的依赖。不幸的是,我不能比这更清楚,因为我不知道有问题的项目的 Maven 依赖结构。试试看:

mvn dependency:tree

然后检查网络驱动依赖的版本。

此外,您似乎正在使用 chrome web 驱动程序 2.21 和 chrome 51。当前的 chrome web 驱动程序是 2.22,发行说明指出它修复了 chrome 51 的许多问题

https://sites.google.com/a/chromium.org/chromedriver/downloads

编辑(添加):另外,不确定代码执行是否会执行任何代码,但如果开发工具打开,听起来可能会发生异常,因为这会断开 Chrome WebDriver 与 Chrome 浏览器的连接. getting selenium error - disconnected: received Inspector.detached event using chrome driver version 2.20.x

【讨论】:

  • 不走运,我什至用 Dependency Heirarchy 检查它。正确解决所有必要的依赖关系
  • 我刚刚编辑了答案以包含指向当前 chrome Web 驱动程序的链接。您可能想尝试将其升级到最新版本,因为发行说明指出它修复了 chrome 51 的许多问题。
  • 赞成您提出的检查 MavebDependency Heirarchy 的建议(非常感谢)。但仍然无法接受答案,因为我仍然没有得到正确的答案。
  • @RahalKanishka 没问题...还找到了一个可能相关的链接...将其添加到上述答案中。希望对您有所帮助!
【解决方案2】:

随着 chrome 发布支持 Chrome v49-52latest version 2.22(2016-06-06),他们修复了一些区域。 更改包括许多错误修复,尤其是对于 Chrome 51+

变更详情请参考notes

他们已经解决了与会话相关的问题 issue#1355

所以请更新您的 chrome 驱动程序版本并使用更新后的 selenium 版本。

【讨论】:

  • 我怀疑是因为我在使用 Fire Fox Web Driver 时出现了同样的错误。但我会尝试新版本的 Chrome 驱动程序
  • 很遗憾,Chrome WebDriver (2.22) 的新版本没有按照您的建议运行,也很抱歉延迟。