Linux amd64环境下切换Chrome窗口并获取CurrentURL时触发TimeoutException的问题求助
各位大佬好,我最近碰到一个棘手的问题:当切换到新窗口后尝试获取当前URL时,触发了TimeoutException,想请大家帮忙排查下原因。
环境信息
- Selenium版本:
4.13.0 - 操作系统:Linux amd64(内核版本
5.10.109-104.500.amzn2.x86_64) - Java版本:
11.0.19 - 浏览器/驱动:Chrome(版本
114.0.5735.198)+ ChromeDriver(路径/usr/bin/chromedriver,版本113.0.5672.63) - 运行模式:Headless,初始配置为
options.addArguments("--headless");
前置警告信息
运行过程中收到了几个关键警告,我先贴出来:
17:28:03 WARNING: The chromedriver version (113.0.5672.63) detected in PATH at /usr/bin/chromedriver might not be compatible with the detected chrome version (114.0.5735.198); currently, chromedriver 114.0.5735.90 is recommended for chrome 114.*, so it is advised to delete the driver in PATH and retry
17:28:03 WARNING: Unable to find CDP implementation matching 114
17:28:03 WARNING: Unable to find version of CDP to use for 114.0.5735.198. You may need to include a dependency on a specific version of the CDP using something similar to
org.seleniumhq.selenium:selenium-devtools-v86:4.13.0where the version ("v86") matches the version of the chromium-based browser you're using and the version number of the artifact is the same as Selenium's.
问题复现代码
触发异常的核心代码片段如下:
driver.switchTo().window(childWindow); String currentURL = driver.getCurrentUrl();
完整异常栈
17:31:52 org.openqa.selenium.TimeoutException: java.util.concurrent.TimeoutException 17:31:52 Build info: version: '4.13.0', revision: 'ba948ece5b*' 17:31:52 System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.10.109-104.500.amzn2.x86_64', java.version: '11.0.19' 17:31:52 Driver info: org.openqa.selenium.chrome.ChromeDriver 17:31:52 Command: [1d9bc8563bbe6934a6c2c66d3e761fc8, getCurrentUrl {}] 17:31:52 Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 114.0.5735.198, chrome: {chromedriverVersion: 113.0.5672.63 (0e1a4471d5ae..., userDataDir: /tmp/.com.google.Chrome.877XX8}, goog:chromeOptions: {debuggerAddress: localhost:35993}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: linux, proxy: Proxy(), se:cdp: ws://localhost:35993/devtoo..., se:cdpVersion: 114.0.5735.198, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true} 17:31:52 Session ID: 1d9bc8563bbe6934a6c2c66d3e761fc8 17:31:52 at org.openqa.selenium.remote.http.netty.NettyHttpHandler.makeCall(NettyHttpHandler.java:65) 17:31:52 at org.openqa.selenium.remote.http.AddSeleniumUserAgent.lambda$apply$0(AddSeleniumUserAgent.java:42) 17:31:52 at org.openqa.selenium.remote.http.Filter.lambda$andFinally$1(Filter.java:55) 17:31:52 at org.openqa.selenium.remote.http.netty.NettyHttpHandler.execute(NettyHttpHandler.java:48) 17:31:52 at org.openqa.selenium.remote.http.netty.NettyClient.execute(NettyClient.java:96) 17:31:52 at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:188) 17:31:52 at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:196) 17:31:52 at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:171) 17:31:52 at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:602) 17:31:52 at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:675) 17:31:52 at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:679) 17:31:52 at org.openqa.selenium.remote.RemoteWebDriver.getCurrentUrl(RemoteWebDriver.java:316)
已尝试的解决方案
我试过将Headless模式替换为options.addArguments("--headless=new");,但新问题出现了——窗口大小配置options.addArguments("window-size=1920,1080");不再生效,而且原有的TimeoutException问题也没有解决。
真心希望各位能帮忙分析下,这个TimeoutException到底是哪里出了问题?
备注:内容来源于stack exchange,提问作者Mahesh C

