我在 java-selenium 中遇到 geckodriver (FF) 问题。 我正在通过 TEST-NG 并行执行(6 个以上线程)运行多个测试场景。
有时,在开始时(打开新浏览器)我会遇到 unreachableBrowserException。所以我 try catch 它,等待 100 毫秒并尝试再次创建它。它可以工作,但它会使第一个失败的浏览器保持打开状态,并显示空白页面。
问题是第一个窗口在初始化为驱动程序之前打开,因此无法通过 driver.close() 或其他方式关闭它(它为空)。所以我必须手动关闭它们。
所以,我的问题是,有人遇到过这种行为吗? 如何关闭此类浏览器而不影响其他线程?
while(true) {
try {
setDriver(DriverFactory.createInstance(getBrowserInstance()));
break;
} catch (UnreachableBrowserException e) {
try {
Thread.sleep(100);
log.info("UnreachableBrowserException! Needed to wait for 100ms ");
removeDriver();
counter++;
} catch (Exception e1) {
log.info("Thread could not wait!");
e1.printStackTrace();
}
}
if(counter>100){
log.info("Was not able to create a browser session!");
break;
}
}
以及 .createInstance 的部分:
final ProfilesIni profilesIni = new ProfilesIni();
System.setProperty("webdriver.gecko.driver", "drivers/geckodriver.exe");
System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"false");
firefoxProfile = new FirefoxProfile();
firefoxProfile.setAcceptUntrustedCertificates(true);
firefoxProfile.setAssumeUntrustedCertificateIssuer(false);
firefoxProfile.setPreference("app.update.auto", false);
firefoxProfile.setPreference("app.update.enabled", false);
firefoxProfile = profilesIni.getProfile("selenium_profile");
capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
return new FirefoxDriver(capabilities); //Here it throws the exception.
Selenium/Java/geckodriver 版本是最新的。不幸的是,FF 版本是 44.02(无法更新)。 如果有任何提示/提示,我将不胜感激。
亲切的问候, 马丁
最佳答案
由于您使用的是 Mozilla Firefox 版本 44.02 并且无法更新,您必须考虑很多事情。
- 要使用
Selenium 3.x
,您必须强制使用geckodriver
。看看为什么here
。 - Firefox 44.02 未启用 marionette,因此您必须通过
DesiredCapability
将marionette
设置为false
> 类。看看为什么here
- 有几个
Selenium
版本依赖项和GeckoDriver
版本依赖项。看到他们here
- 您无法创建新的
Firefox 配置文件
,然后再次尝试使用现有的Firefox 配置文件
。 假设您有一个名为
selenium_profile
的现有Firefox 配置文件
,以下代码将在新的浏览器 session 中打开 Firefox 配置文件:System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe"); ProfilesIni profile = new ProfilesIni(); FirefoxProfile firefoxProfile = profile.getProfile("selenium_profile"); firefoxProfile.setAcceptUntrustedCertificates(true); firefoxProfile.setAssumeUntrustedCertificateIssuer(false); firefoxProfile.setPreference("app.update.auto", false); firefoxProfile.setPreference("app.update.enabled", false); DesiredCapabilities dc = DesiredCapabilities.firefox(); dc.setCapability(FirefoxDriver.PROFILE, firefoxProfile); dc.setCapability("marionette", false); WebDriver driver = new FirefoxDriver(dc); driver.get("http://www.google.com");
现在您可以轻松返回这些功能。
关于java - UnreachableBrowserException firefox 驱动程序 selenium java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45811630/