我正在使用带有铬 3.141.59 和铬驱动程序 79 的硒 79。随机地,我从RemoteWebDriver.get(url);
和该会话中删除了硒服务器。但 Chrome 窗口仍保持打开状态。因此,我无法使用相同的用户目录创建新会话。在我每次尝试创建新会话时,chrome 窗口都会打开,但会话创建失败。所以那些 全部打开 Chrome 窗口导致内存泄漏!我尝试从服务器设置超时和浏览器超时,但没有帮助。知道发生了什么吗?
我通过以下方式启动服务器:
java -jar -Dselenium.LOGGER.level=ALL selenium-server-standalone-3.141.59.jar -timeout 250 -browserTimeout 300
我随机得到的例外:
Caused by: org.openqa.selenium.WebDriverException: java.net.ConnectException: Connection refused (Connection refused)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'myhost', ip: 'myip', os.name: 'Linux', os.arch: 'amd64', os.version: '4.14.154-128.181.amzn2.x86_64', java.version: '1.8.0_201'
Driver info: mypackage.SeleniumHelper$2
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 79.0.3945.88, chrome: {chromedriverVersion: 79.0.3945.36 (3582db32b3389..., userDataDir: /var/tmp/username...}, goog:chromeOptions: {debuggerAddress: localhost:35341}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webdriver.remote.sessionid: 66ddc30a30affc4ba52a539bc41...}
Session ID: 66ddc30a30affc4ba52a539bc411ac2c
at sun.reflect.GeneratedConstructorAccessor1082.newInstance(Unknown Source) ~[?:?]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_201]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[?:1.8.0_201]
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187) ~[selenium-remote-driver-3.141.59.jar:?]
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122) ~[selenium-remote-driver-3.141.59.jar:?]
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) ~[selenium-remote-driver-3.141.59.jar:?]
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158) ~[selenium-remote-driver-3.141.59.jar:?]
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552) ~[selenium-remote-driver-3.141.59.jar:?]
at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:277) ~[selenium-remote-driver-3.141.59.jar:?]
... 65 more
我的镶边选项:
ChromeOptions options = new ChromeOptions ();
options.addArguments ("user-data-dir=/var/tmp/username");
options.addArguments ("disable-gpu");
options.addArguments ("disable-impl-side-painting");
options.addArguments ("disable-dev-shm-usage");
options.addArguments ("disable-infobars");
options.addArguments ("disable-gpu-sandbox");
options.addArguments ("no-sandbox");
options.addArguments ("disable-accelerated-2d-canvas");
options.addArguments ("disable-accelerated-jpeg-decoding");
options.addArguments ("test-type=ui");
options.addArguments ("no-proxy-server");
您需要考虑以下几点:
--- user-data-dir:指浏览器存储用户配置文件的目录。因此,您不能传递任何任意值。请参阅:这个和这个讨论。 --
- disable-gpu:禁用 GPU 硬件加速。如果软件渲染器未到位,则 GPU 进程将不会启动。然而,
--disable-gpu
论点的目的是在Windows平台上启用google-chrome-headless。 这是必需的,因为SwiftShader早些时候在无头模式下在Windows上失败了断言。当你在Linux操作系统上时,你需要删除它。请参阅:这个和这个讨论。 - 理想情况下,您只添加根据测试规范必须添加的参数。
示例最小代码块:
public class A_Chrome { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.addArguments("start-maximized"); options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation")); options.setExperimentalOption("useAutomationExtension", false); WebDriver driver = new ChromeDriver(options); driver.get("https://www.google.com/"); driver.quit(); } }
最后,当程序引发异常时,WebDriver 实例将失去对浏览上下文的控制,并且两者都变成僵尸进程。因此,谷歌浏览器窗口保持打开状态。
如果使用 try and except 函数怎么办?除了,您让驱动程序关闭 chrome 并再次重新打开它以创建新会话?
try:
#set your try code here
except TimeoutException:
print('Page took too long to load or there was a different problem :(')
driver.quit()
try:
#set your new code here
except:
#set your except here
或者,您可以尝试在"除外"之后打开一个新的chrome窗口