我在 Mac 10.6.8 上使用 Selenium Server 2.33.0、Selenium Webdriver JS 绑定(bind) 2.34.0(npm 包“selenium-webdriver”)和 PhantomJS 1.9.1 进行身份验证时遇到问题。我还尝试了其他 JS 绑定(bind)“webdriverjs”和“wd”,结果相似,所以我认为这个绑定(bind)没有问题。
我使用这个设置 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);
我从这个 posting 复制了这个脚本.我发现在 1.9.1 中,用户名和密码可以正常工作,我不需要设置 customHeader。我在我的 Selenium 脚本中尝试了 customHeader,但没有任何区别。这可能是 GhostDriver 的问题。
有没有人能够让这个工作?
最佳答案
我也遇到了同样的情况。
我的理论是我们正在使用 NTLM(Windows 身份验证)发出请求,这将请求 401(未授权)并要求使用用户名/密码组合重新发送请求。 GhostDriver 很清楚设置适用 only during the initial call ,这可能会避免传入凭据。
令人沮丧的是,JavaScript 版本工作得很好,但 C# 版本却失败得如此惊人。
如果有任何建议,我很乐意听取。
关于selenium-webdriver - 如何使用 Selenium Webdriver 和 PhantomJS 进行基本身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18221422/