java 通过 .htaccess 弹出窗口让 Selenium 登录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2085348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Selenium to login via .htaccess popup
提问by sunil
I am using WebDriver (Selenium2) with Java on linux. I am using WebDriver to auto fill form and submit it. I am facing problem with htaccess sites i.e., I am not able to access htaccess site through WebDriver.
我在 linux 上使用带有 Java 的 WebDriver (Selenium2)。我正在使用 WebDriver 自动填写表单并提交。我遇到了 htaccess 站点的问题,即,我无法通过 WebDriver 访问 htaccess 站点。
Can anyone help me out in this regard?
任何人都可以在这方面帮助我吗?
Thanks in advance, Sunil
提前致谢,苏尼尔
回答by Bozho
From Selenium FAQ(which is down at the moment):
来自Selenium 常见问题解答(目前已关闭):
How do I use Selenium to login to sites that require HTTP basic authentication (where the browser makes a modal dialog asking for credentials)?
Use a username and password in the URL, as described in RFC 1738: Test Type
open http://myusername:[email protected]/blah/blah/blahNote that on Internet Explorer this won't work, since Microsoft has disabled usernames/passwords in URLs in IE. However, you can add that functionality back in by modifying your registry, as described in the linked KB article. Set an "iexplore.exe" DWORD to 0 in HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE.
If you don't want to modify the registry yourself, you can always just use Selenium Remote Control, which automatically sets that that registry key for you as of version 0.9.2.
我如何使用 Selenium 登录到需要 HTTP 基本身份验证的站点(浏览器在其中创建一个模态对话框,要求提供凭据)?
在 URL 中使用用户名和密码,如 RFC 1738 中所述:测试类型
open http://myusername:[email protected]/blah/blah/blah请注意,在 Internet Explorer 上这将不起作用,因为 Microsoft 已禁用 IE 中 URL 中的用户名/密码。但是,您可以通过修改注册表来重新添加该功能,如链接的知识库文章中所述。在 HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE 中将“iexplore.exe”DWORD 设置为 0。
如果您不想自己修改注册表,您可以随时使用 Selenium 远程控制,它会自动为您设置该注册表项,从 0.9.2 版开始。
回答by Dave Hunt
There is an outstanding issue for WebDriver to support basic and digest HTTP authentication. If you want to be notified of changes I suggest voting for the issue at http://code.google.com/p/selenium/issues/detail?id=34
WebDriver 支持基本和摘要式 HTTP 身份验证存在一个突出问题。如果您想收到更改通知,我建议您在http://code.google.com/p/selenium/issues/detail?id=34 上对该问题进行投票
回答by David
Until there is full support for this across browsers for WebDriver (or Selenium), alternate option is to integrate w/ desktop GUI automation tools, where the desktop GUI tool will automate the HTTP authentication part. You can probably find some examples for this or file downloads, uploads if you google for things like "Selenium AutoIt", etc.
在 WebDriver(或 Selenium)的浏览器完全支持此功能之前,替代选项是集成桌面 GUI 自动化工具,其中桌面 GUI 工具将自动化 HTTP 身份验证部分。如果您在谷歌上搜索诸如“Selenium AutoIt”之类的内容,您可能会找到一些关于此或文件下载、上传的示例。
For a cross platform solution, replace AutoIt with Sikuli or something similar.
对于跨平台解决方案,用 Sikuli 或类似的东西替换 AutoIt。
回答by Will Budreau
The solution from the Selenium FAQ does not work - FireFox now adds a prompt to confirm that the user means to authenticate which does not have an obvous-to-me Selenium task.
Selenium 常见问题解答中的解决方案不起作用 - FireFox 现在添加了一个提示,以确认用户是否打算进行身份验证,其中没有对我来说很明显的 Selenium 任务。
"You are about to log in to the site "my.domain.com" with the username "myuser"
“您即将使用用户名“myuser”登录站点“my.domain.com”
The cheapest solution is to manually enter the credentials once with the browser profile that the selenium session uses and let the browser save them. (I did this in mid-test) Also added the profile integer value network.http.phishy-userpass-length;255
最便宜的解决方案是使用 selenium 会话使用的浏览器配置文件手动输入一次凭据,然后让浏览器保存它们。(我在测试中这样做了)还添加了配置文件整数值 network.http.phishy-userpass-length;255
This other question pointed me at the way to do it programmatically, i.e. using Selenium 2
另一个问题向我指出了以编程方式进行操作的方法,即使用 Selenium 2
回答by Kati Holasz
i'm using this and it's working for me.
我正在使用它,它对我有用。
public void login(String username, String password){
WebDriver driver = getDriver();
String URL = "http://" + username + ":" + password + "@" + "link";
driver.get(URL);
driver.manage().window().maximize();
}

