当前设置存储在我的 DriverFactory 中:
private static ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();
return webDriver.get();
目前以下方法似乎失败:
public void loadUrl(String url) {
try {
getDriver().get(url);
System.out.println("Successfully navigated to URL: " + url);
} catch (Exception e) {
System.out.println(e.getStackTrace());
Assert.fail("Unable to navigate to URL: " + url + ", Exception: " + e.getMessage());
}
}
设置驱动方法:
public final void setDriver(String browser) throws Exception {
String remoteHubUrl = "http://xxx.xxxx.xxx.xxx:4444/wd/hub/";
try {
switch (setBrowserType(browser)) {
case "grid":
DesiredCapabilities capabilities =new DesiredCapabilities();
capabilities.setBrowserName("chrome");
ChromeOptions op = new ChromeOptions();
op.merge(capabilities);
webDriver.set(new RemoteWebDriver(new URL(remoteHubUrl), op));
break;
}
}
异常消息:
Exception: null
使用旧版本的 chromedriver 似乎没有问题,有什么想法吗?
用于在执行测试之前初始化驱动程序的基本步骤:
@Before
public void setupHook() {
setDriver("grid");
}
最佳答案
我在上述代码中看到的主要问题是,您尝试使用 ChromeOptions 对象而不是 DesiredCapativity 对象实例化 RemoteWebDriver 实例。
RemoteWebDriver 需要 DesiredCapability(请参阅 Selenium code here ),ChromeOptions 无法扩展或实现该功能。它们都扩展了 AbstractCapability,因此您过去可能很幸运,但现在它们已经分歧到不再兼容的程度。
*编辑*
我建议您更新代码来执行此操作:
switch (setBrowserType(browser)) {
case "grid":
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("chrome");
webDriver.set(new RemoteWebDriver(new URL(remoteHubUrl),capabilities));
break;
}
关于java - 异常: null when setting up Selenium using ThreadLocal and ChromeDriver (Version 75)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56294514/