我正在将硒3.141.59与chrome 79和chromedriver 79一起使用。随机地,我从RemoteWebDriver.get(url);
中收到一个异常,并且该会话从硒服务器中删除。但是Chrome窗口保持打开状态。因此,我无法使用相同的用户目录创建新会话。每次尝试创建新会话时,都会打开chrome窗口,但会话创建失败。因此,所有这些打开的Chrome窗口都会导致内存泄漏!我试图从服务器设置超时和browserTimeout,但没有帮助。知道发生了什么事吗?
我通过以下方式启动服务器:
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");
答案 0 :(得分:2)
您需要考虑一些事项:
--disable-gpu
的目的是在google-chrome-headless平台上启用windows。早于SwiftShader fails an assert on Windows in headless mode就需要它。在linux os 上时,您需要将其删除。请参阅:this和this讨论。 示例最小代码块:
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实例将失去对 Browsing Context 的控制,并且两者都变成Zombie process。因此google-chrome窗口保持打开状态。
答案 1 :(得分:0)
如果您使用try和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窗口