我目前通过Python中的PhantomJS + Selenium运行浏览器测试。处理重定向w/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>
。
这是一个known issue,发布的解决方案包括添加一小段代码来适当地处理重定向。
如果您使用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的开发尝试。
你用什么版本?也许你需要更新到PhantomJS 2. – 2015-03-31 08:02:55
我在1.9.8和2.0.1开发版中尝试过。 – okoboko 2015-04-01 04:20:27