我正试图在一个已经打开的chrome中执行我的selenium自动化。
我遵循以下步骤:https://medium.com/@harith.sankalpa/connect-selenium-driver-to-an-existing-chrome-browser-instance-41435b67affd
我打开一个带有以下行的chrome:
/opt/google/chrome/chrome --remote-debugging-port=6666
这是我的java代码
System.setProperty("webdriver.chrome.driver", "/../chromedriver");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress","localhost:6666");
// Initialize browser
WebDriver driver = new ChromeDriver(options);
// Open Google
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("jrichardsz");
// Close browser
driver.close();
当我运行这段代码时,我得到了这个日志:
Starting ChromeDriver 84.0.4147.30 (48b3e868c0aa7e814951969b4c0b6f6949e110a8-refs/branch-heads/4147@{#310}) on port 12155
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
几秒钟后,我得到:无法访问镶边错误
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot connect to chrome at localhost:6666
from chrome not reachable
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'jane_doe', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '***', java.version: '1.8.0_151'
Driver info: driver.version: ChromeDriver
remote stacktrace: #0 0x556168a87ea9 <unknown>
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$errorHandler$0(W3CHandshakeResponse.java:62)
at org.openqa.selenium.remote.HandshakeResponse.lambda$getResponseFunction$0(HandshakeResponse.java:30)
at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:126)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:128)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:74)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
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.startSession(RemoteWebDriver.java:213)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:157)
at OpenedChrome.main(OpenedChrome.java:24)
我也尝试了chromiun和opera,我得到了同样的错误。
我的chrome已打开,但java无法连接到它。
我回顾了这些倒霉的链接
2条答案
按热度按时间qlvxas9a1#
在我的例子中,这是由于我使用端口(6666)在调试模式下启动chrome,这是根据以下条件限制的:Chrome上受限制的[不安全]端口列表
一个完整的chrome列表是here和here。Firefox允许的端口在这里。还有如何强制允许一些端口,here
识别错误的步骤
telnet localhost wxyz
或在Windows中使用nc -zvv localhost wxyz
验证端口侦听http://localhost:wxyz/json/version
的http。如果端口正在侦听,会收到一个json响应。如果由于一些问题,如不允许的端口,chrome没有选择这个端口,http://localhost:wxyz/json/version
会返回一个错误。如果我找到了真实的的端点,我会更新这个答案。如果端口侦听正确,则另一个低级错误是原因:
bihw5rsg2#
可以设置以下属性以检查调试日志
Windows操作系统示例:
如果任何现有的chrome进程正在运行,并且您已使用
则在执行相关selenium脚本时可能会得到以下日志
打开chrome浏览器的干净示例进行调试
chrome.exe --远程调试端口=9222 --不首次运行--不默认浏览器检查--用户数据目录=“D:\remotedebugfolder”
现在用