pythonselenium-webdriverphantomjsghostdriver

Handling Redirection w/ PhantomJS + Selenium


I currently run browser tests via PhantomJS + Selenium in Python.

desired_capabilities = dict(DesiredCapabilities.PHANTOMJS)
desired_capabilities["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36")
driver = webdriver.PhantomJS(executable_path="./phantomjs", desired_capabilities=desired_capabilities)
driver.get('http://google.com')

This works fine, unless the page I'm trying to get has a redirect on it.

Example:

https://login.vrealizeair.vmware.com/

In this case, the get doesn't work properly. The page source is empty: <html><head></head></body></html>.

This is a known issue with solutions posted that involve adding a snippet of code to handle redirects appropriately.

How/where do you add this code if you're running tests with Selenium (in my first code snippet)? Is it part of desired_capabilties?

Example:

page.onNavigationRequested = function(url, type, willNavigate, main) {
    if (main && url!=myurl) {
        myurl = url;
        console.log("redirect caught")
        page.close()
        renderPage(url);
    }
};

page.open(url, function(status) {
    if (status==="success") {
    console.log(myurl);
        console.log("success")
            page.render('yourscreenshot.png');
            phantom.exit(0);
    } else {
        console.log("failed")
            phantom.exit(1);
    }
});

I tried it with PhantomJS 1.9.8 and 2.0.1-development.


Solution

  • It turns out the page couldn't be crawled due an error: SSL handshake failed.

    The solution is to use the following line to initialize the driver:

    driver = webdriver.PhantomJS(executable_path="./phantomjs", service_args=['--ignore-ssl-errors=true'])