Selenium错误MainClientExec:103 - 连接被丢弃

10
我在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 driver也没有停止其工作进程。当我从项目内部运行Selenium Project时,它运行良好。但即使我添加了必要的依赖项,从项目外部运行时也不行。

问题:为什么这个Selenium项目表现出不可预测的行为?这是由于Maven依赖问题引起的吗?
请帮忙。


@Evan,我不知道其他人是否可以编辑我的问题并接受它,无论如何,非常感谢您的编辑。 - Rahal Kanishka
我成功找到了问题所在。问题是由一个依赖项("log4j")引起的,该依赖项来自父项目(在我的情况下是"data-extractor")。我排除了该依赖项并添加了"Logback"作为替代记录器,项目开始正常工作了。 但是我仍然没有得到为什么会发生这种情况的答案。因此,我的问题和悬赏奖励将继续存在,直到我得到正确的答案。 谢谢。 - Rahal Kanishka
如果有人能解决这个错误,无论如何我都会提供50个赏金。如果需要更多细节,请通知我。 - Rahal Kanishka
2个回答

2
我的猜测是您所见的问题可能是由于Maven依赖关系出现了问题。曾经我也遇到过类似的问题,Chrome WebDriver会因为NumberFormatException而崩溃,每当我们试图访问它的cookies时都会出现这种情况。根本原因在于不兼容的传递性依赖(例如,它需要某个依赖项的特定版本,而我们的项目正在引用较旧的版本,而Maven认为一切都很好)。在我们的情况下,解决方法是在运行Selenium测试的测试项目中明确添加正确版本的依赖项。不幸的是,我无法更清楚地说明原因,因为我不知道涉及的项目的Maven依赖结构。请尝试查看:
mvn dependency:tree

接着检查Web驱动程序依赖项的版本。

此外,看起来您正在使用Chrome Web Driver 2.21和Chrome 51。当前的Chrome Web Driver是2.22,发布说明表明它修复了许多Chrome 51的问题。

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

编辑(添加):另外,不确定代码执行是否有任何问题,但如果开发人员工具打开,听起来会发生这种异常,因为这将断开Chrome WebDriver与Chrome浏览器之间的连接。 获取Selenium错误-使用Chrome驱动程序版本2.20.x时收到Inspector.detached事件而断开连接


没有运气,我甚至检查了依赖层次结构。所有必要的依赖项都已正确解决。 - Rahal Kanishka
我刚刚编辑了答案,包括当前Chrome Web驱动程序的链接。您可能希望尝试升级到最新版本,因为发布说明中指出它修复了许多Chrome 51的问题。 - Aaron Davis
感谢您提出的建议,我已经点赞并查看了Maven依赖关系(非常感谢)。但是,由于我仍然没有得到正确的答案,所以我无法接受该答案。 - Rahal Kanishka
1
@RahalKanishka 没问题...我还找到了一个可能相关的链接...已经添加到上面的答案中。希望能有所帮助! - Aaron Davis

1
随着Chrome发布了支持Chrome v49-52的最新版本2.22(2016年6月6日),他们已经修复了一些问题。变化包括许多错误修复,特别是针对Chrome 51+。如需更改详细信息,请参阅notes。他们解决了一个与会话相关的问题issue#1355。因此,请更新您的Chrome驱动程序版本并使用更新的Selenium版本。

我怀疑这个问题,因为当我使用 Fire Fox Web Driver 时也出现了同样的错误。但是我会尝试新版本的 Chrome Driver。 - Rahal Kanishka
很遗憾地说,Chrome WebDriver(2.22)的新版本并没有像你建议的那样工作,同时也为延迟感到抱歉。 - Rahal Kanishka

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接