java - 使用 selenium webdriver + phantom 时无法加载 https url

标签 java selenium ssl phantomjs

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/

相关文章:

python - 使 selenium 更快地运行网站列表

wordpress - 在 Wordpress 多站点上强制使用 https

java - H.323协议(protocol)端点到端点发送和接收什么来发起调用?

java - Selenium跳过具有隐藏属性的html元素

java - 使用线程本地避免创建映射以减少 GC 但失败

Maven在运行testng测试用例时抛出错误

java - 使用 pfx 文件在 jetty 服务器中配置 SSL

linux - 如何在 openhpi 安装期间解决 openssl 问题?

java - 需要帮助使用 eclipse 制作算命程序

Java 罗马数字转整数 : null pointer