我使用Selenium Server 2.33.0,Selenium Webdriver JS绑定2.34.0(npm包“selenium-webdriver”)和PhantomJS 1.9.1在Mac 10.6.8上进行身份验证时遇到了问题。我还尝试了其他JS绑定“webdriverjs”和“wd”,但结果类似,因此我认为这不是绑定的问题。
我使用以下内容设置Webdriver:
return new Webdriver.Builder().
usingServer('http://localhost:4444/wd/hub').
withCapabilities({
"browserName": "phantomjs",
"phantomjs.page.settings.userName":user,
"phantomjs.page.settings.password":password
}).build();
我在Selenium服务器日志中看到了以下输出内容:
PhantomJS is launching GhostDriver...
[INFO - 2013-08-13T21:52:40.240Z] GhostDriver - Main - running on port 28904
[INFO - 2013-08-13T21:52:40.394Z] Session [acd0ad70-0462-11e3-95df-4b230b17334d] - CONSTRUCTOR - Desired Capabilities:{"phantomjs.page.settings.password":"xxx","browserName":"phantomjs","phantomjs.page.settings.userName":"xxx"}
[INFO - 2013-08-13T21:52:40.394Z] Session [acd0ad70-0462-11e3-95df-4b230b17334d] - CONSTRUCTOR - Negotiated Capabilities: {"browserName":"phantomjs","version":"1.9.1","driverName":"ghostdriver","driverVersion":"1.0.3","platform":"mac-10.6 (Snow Leopard)-32bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"},"phantomjs.page.settings.password":"xxx","phantomjs.page.settings.userName":"xxx"}
我可以看到协商的功能包含了我指定的用户名和密码。但是当我尝试打开一个使用HTTP基本身份验证的网站页面时,身份验证失败:
14:43:25.504 INFO - Executing: [find element: By.id: auth-username-field-inputEl] at URL: /session/04627dc2-a16c-42b3-b3dc-48e502f7cfec/element)
14:43:29.410 WARN - Exception thrown org.openqa.selenium.NoSuchElementException: Error Message => 'Unable to find element with id 'auth-username-field-inputEl''
如果我对没有身份验证的本地主机网站运行脚本,它可以正常工作。如果我使用Firefox(而不是通过Selenium)查看远程网站,我可以进行身份验证并查看主页。
我还尝试使用“-w”在Webdriver模式下运行PhantomJS,并得到了类似的结果。
如果我使用phantomjs运行这个脚本,它就可以工作:
var page=require('webpage').create();
//page.customHeaders={'Authorization': 'Basic '+btoa('xxx:xxx')};
page.settings.userName="xxx";
page.settings.password="xxx";
var callback=function(status){
if(timer)window.clearTimeout(timer);
if (status=='success' || status=='timedout') {
console.log(status);
console.log(page.plainText);
}else{
console.log('Failed to load.');
}
phantom.exit();
};
var timer=window.setTimeout(callback,60000,'timedout');
var url="http://xxx.com";
page.open(url,callback);
我从这个帖子中复制了这个脚本。我发现在1.9.1版本中,用户名和密码可以正常工作,我不需要设置自定义标头。我在我的Selenium脚本中尝试了自定义标头,但没有任何区别。这可能是GhostDriver的问题。
有人能够让这个工作吗?