PhantomJSDriver works for HTTP but not for HTTPS

2019-03-03 00:04发布

问题:

public class FooTest {
    WebDriver driver;

    @Before
    public void beforeTest() {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setJavascriptEnabled(true);
        capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

        driver = new PhantomJSDriver(capabilities);
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
    }

    @Test
    public void test() {
        driver.get("http://www.example.com");

        WebElement e = driver.findElement(By.tagName("h1"));
        System.out.println("TEXT" + e.getAttribute("innerHTML"));
        assertNotNull(e);
        driver.quit();
    }

}

Hi, I'm just simply trying get the h1 tag in www.example.com which says "Example Domain." The code works for http://www.example.com but not for https://www.exmaple.com. How can I fix this problem? Thanks

回答1:

PhantomJSDriver does not support (all) DesiredCapabilities.

You will need:

caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {"--web-security=no", "--ignore-ssl-errors=yes"});
driver = new PhantomJSDriver(caps);

Documented here: https://github.com/detro/ghostdriver/issues/233