使用PhantomJS + Selenium处理重定向
问题内容:
我目前正在通过Python中的PhantomJS + Selenium运行浏览器测试。
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')
除非我要get
重定向的页面在上面,否则它可以正常工作。
例:
https://login.vrealizeair.vmware.com/
在这种情况下,get
不能正常工作。页面源为空:<html><head></head></body></html>
。
这是发布的解决方案的一个已知问题,该解决方案涉及添加代码段以适当地处理重定向。
如果您正在使用Selenium运行测试(在我的第一个代码段中),如何/在何处添加此代码? 是它的一部分desired_capabilties
吗?
例:
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);
}
});
我使用PhantomJS 1.9.8和2.0.1-development进行了尝试。
问题答案:
事实证明,由于出现以下错误而无法抓取该页面:SSL handshake failed
。
解决方案是使用以下行初始化驱动程序:
driver = webdriver.PhantomJS(executable_path="./phantomjs", service_args=['--ignore-ssl-errors=true'])