OS: Windows 10 pro IDE: Eclipse Neon JDK: version 1.8.0_91 Selenium standalone server: version 3.0.1 phantom.exe: version 2.1.1 Windows phantom.jar: version 1.2.1 TestNG: version 6.9.9
Description: I'm trying to use phantomJS headless browser together with selenium webdriver and java. I need to load a https url which has a certificate error (The security certificate presented by this website was not issued by a trusted certificate authority). I need to pass this issue. I've tried to do it (among other failed attempts) as follows:
class initializeTest.java
public class initializeTest{
public static WebDriver driver = null;
public static WebDriver settingBrowser(String browser) throws InterruptedException {
if(browser.equalsIgnoreCase("phantom")){
File file = new File("C:\\Program Files\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
DesiredCapabilities capabilitiesPhantomJS = DesiredCapabilities.phantomjs();
capabilitiesPhantomJS.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {"--ssl-protocol=tlsv1"});
capabilitiesPhantomJS.setPlatform(Platform.ANY);
capabilitiesPhantomJS.setJavascriptEnabled(true);
capabilitiesPhantomJS.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver = new PhantomJSDriver();
}
}
return driver;
}
作为 TestNG 类运行的类的代码摘录
@BeforeTest
public void navigate() throws InterruptedException{
String selectBrowser = "phantom"
this.driver = initializeTest.settingBrowser(selectBrowser);
driver.navigate().to(https://....);
thread.sleep(5000);
System.out.println(driver.getTitle());
System.out.println("Begin 2");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.urlContains("login"));
System.out.println(driver.getTitle());
}
结果:
1.第一个 println 运行,但控制台没有打印任何内容。
2。打印“Begin 2”,最后一个 println 没有执行(未找到 url 处的登录字符串)。
3。第三个println没有执行
原因是没有加载url,虽然我不明白为什么。 有没有人有想法或知道解决方法?
最佳答案
您可能需要添加另一个命令行标志:
--ignore-ssl-errors
(或者可能是 --ignore-ssl-errors=yes
。)
即
capabilitiesPhantomJS.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {"--ssl-protocol=tlsv1", "--ignore-ssl-errors"});
关于java - 使用 selenium webdriver + phantom 时无法加载 https url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41380724/