我可以在 Selenium 中设置远程允许源而不使用通配符吗?

Car*_*tem 5 java google-chrome google-chrome-devtools selenium-chromedriver selenium-webdriver

我遇到了 Selenium WebDriver 的 WebSocket 问题,这里描述了同样的问题。问题的解决方案是将--remote-allow-origins=*参数添加到我的驱动程序的 ChromeOptions 中。但是,我不想为此参数使用通配符。我想将其设置为仅允许来自我的应用程序的连接。

我相信解决方案将涉及预先确定 WebDriver 的端口。这是我最好的尝试,但它不起作用:

ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito", "--app=" + linkedform);

int port;
try (ServerSocket serverSocket = new ServerSocket(0)) {
    port = serverSocket.getLocalPort();
} catch (IOException e) {
    throw new RuntimeException(e);
}

options.addArguments("--remote-allow-origins=http://localhost:" + port);
ChromeDriverService service = new ChromeDriverService.Builder().usingPort(port).build();
this.chrome = new ChromeDriver(service, options);
Run Code Online (Sandbox Code Playgroud)

我走在正确的道路上吗?这可能吗?

小智 0

你可以尝试这样的事情

ChromeOptions options = new ChromeOptions();
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");

options.addArguments("--remote-allow-origins=*");
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.navigate().to("http://localhost:8080/leiloes");
Run Code Online (Sandbox Code Playgroud)