Selenium 脚本不能在打开的浏览器上运行

Selenium scripts don't run on open brower

提问人:user2066728 提问时间:11/1/2023 更新时间:11/1/2023 访问量:71

问:

我在 Ubuntu 上将 selenium 与 java 一起使用。我正在尝试在已经打开的浏览器上运行硒代码。我找到了一些解决方案,但它们都为我崩溃了。如果我不执行以下任何操作,我可以打开浏览器并正常运行我的代码。

我包括是因为我需要在启动浏览器时添加它。options.addArguments("--remote-allow-origins=*");

这是我尝试过的和结果。

在终端中,我键入浏览器已启动。在另一个终端中,我键入 .我得到的结果。这应该意味着我的端口很好。google-chrome --remote-debugging-port=9988sudo netstat -tulpn |grep :9988tcp 0 0 127.0.0.1:9988 0.0.0.0:* LISTEN 47330/chrome --remo

当我在新浏览器中输入 http://127.0.0.1:9988 时,没有任何反应。从我所读到的内容来看,它应该复制调试器窗口。

我运行以下代码,但在实例化浏览器时总是崩溃。我已经尝试了所有选项的组合。

private void ports() {
        System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");     
        ChromeOptions options = new ChromeOptions();
        
        options.addArguments("--remote-allow-origins=*");
//      options.addArguments("--remote-debugging-port=9988");

        options.setExperimentalOption("debuggerAddress", "localhost:9988");
//      options.setExperimentalOption("debuggerAddress", "127.0.0.1:9988");

        WebDriver browser = new ChromeDriver(options);      
        browser.get("https://www.bbccom/");

    }

它崩溃,输出如下:

Starting ChromeDriver 119.0.6045.105 (38c72552c5e15ba9b3117c0967a0fd105072d7c6-refs/branch-heads/6045@{#1103}) on port 46485
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.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: cannot connect to chrome at localhost:9989
from chrome not reachable
Build info: version: '4.3.0', revision: 'a4995e2c09*'
System info: host: 'clay-HP-ENVY-17-Notebook-PC', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '6.2.0-36-generic', java.version: '17.0.8.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [], debuggerAddress: localhost:9989, extensions: []}}], desiredCapabilities=Capabilities {browserName: chrome, goog:chromeOptions: {args: [], debuggerAddress: localhost:9989, extensions: []}}}]
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:144)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:102)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:67)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:156)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:569)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:264)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:179)
    at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:101)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:81)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:70)
    at top.OpenBrowser.ports(OpenBrowser.java:131)
    at top.OpenBrowser.run(OpenBrowser.java:31)
    at top.Main.main(Main.java:15)

这些是我尝试过的地方: https://www.youtube.com/watch?v=Rrkj4tdXngY

另一个 https://cosmocode.io/how-to-connect-selenium-to-an-existing-browser-that-was-opened-manually/#launch-browser-with-custom-flags

Java Eclipse 谷歌浏览器 网络驱动程序 硒 驱动程序

评论

0赞 user2066728 11/2/2023
我的硒版本不是最新的。这就是问题所在。

答: 暂无答案