在 Firefox 中通过 URL 进行 HTTP 基本身份验证不起作用?

发布于 2024-09-05 09:58:21 字数 478 浏览 8 评论 0原文

我知道通常您可以通过在 URL 中传递用户名和密码来登录需要使用 Selenium 进行 HTTP 基本身份验证的网站,例如:

selenium.open("http://myusername:[email protected]/mypath");

我一直在使用 Firefox 2 或 3 运行 Selenium 测试,但仍然收到“需要身份验证” “对话窗口?

更新:这似乎不是 Selenium 问题,而是 Firefox 问题。如果我在 FF 中手动输入 URL,我将收到身份验证对话框,但如果我在 Opera 中输入 URL,则会显示我的页面,但不会显示身份验证对话框。

I know that normally you can login to sites that require HTTP basic authentication with Selenium by passing the username and password in the URL, e.g.:

selenium.open("http://myusername:[email protected]/mypath");

I've been running a Selenium test with Firefox 2 or 3 and there I still get the "Authentication Required" dialog window?

Update: It seems not to be a Selenium problem but rather a Firefox issue. If I enter the URL manually within FF I'll get the authentication dialog, but if I enter the URL in Opera, my page is displayed without showing an authentication dialog.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

绻影浮沉 2024-09-12 09:58:21

我有一个适用于 Firefox 和 Internet Explorer 的解决方案。

对于 Firefox,您需要进入 about:config 并创建长度为 255 的整数 network.http.phishy-userpass-length。这会告诉 Firefox 不要弹出窗口如果用户名和密码少于 255 个字符,则会出现身份验证框。您现在可以使用 http://user:[电子邮件] ;protected] 进行身份验证。

对于 Internet Explorer,您必须编辑注册表。在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE 项中,创建 DWORD 值 iexplore.exeexplorer.exe并确保它们的值为0

我还必须覆盖 NTLM 身份验证。要在 Firefox 中使用 HTTP 基本身份验证语法进行 NTLM 身份验证,只需指定 Firefox 配置字符串 network.automatic-ntlm-auth.trusted-uris (伴随第一个配置选项)中使用的域即可。这将在 IE 中仅通过注册表编辑起作用。

I have a solution for Firefox and Internet Explorer.

For Firefox, you need to go into about:config and create the integer network.http.phishy-userpass-length with a length of 255. This tells Firefox not to popup an authentication box if the username and password are less than 255 characters. You can now use http://user:[email protected] to authenticate.

For Internet Explorer, you must edit the registry. In the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE, create the DWORD values iexplore.exe and explorer.exe and make sure their values are 0.

I had to override NTLM authentication aswell. To NTLM authenticate using the HTTP basic authentication syntax in Firefox, simply specify the domains being used in the Firefox config string network.automatic-ntlm-auth.trusted-uris (accompanying the first config option). This will work in IE with the registy edit alone.

忆悲凉 2024-09-12 09:58:21

为 Druska 的回答做出贡献,您可以使用 Selenium 2 API 进行相同的配置:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.http.phishy-userpass-length", 255);
profile.setPreference("network.automatic-ntlm-auth.trusted-uris","yourDomain");
new FirefoxDriver(profile);

这种方法更简单,您不必要求每个开发人员更改他们的 Firefox 配置。我只用 Firefox 驱动程序进行了测试。

更新

出于某种原因(也许是在https://stackoverflow.com/a/14348701/解释的) 256245),上述解决方案不适用于较新版本的 Firefox。这是现在对我有用的方法(使用 Firefox 19.0.2 进行测试):

  1. 安装 AutoAuth Firefox 插件;
  2. 访问需要认证的站点。输入您的用户名和密码,并确保选择保存凭据;
  3. 将 AutoAuth 安装文件保存在硬盘上:在插件页面,右键单击“添加到 Firefox”并“链接另存为”;
  4. 实例化 Firefox Webdriver 如下:

    FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default");
    文件pluginAutoAuth = new File("src/test/resources/autoauth-2.1-fx+fn.xpi");
    firefoxProfile.addExtension(pluginAutoAuth);
    返回新的 FirefoxDriver(firefoxProfile);
    

确保使用保存插件安装的正确路径实例化pluginAutoAuth 文件。如果您对使用默认配置文件感到不舒服,您可以使用 Firefox 配置文件管理器并创建一个特定于您的测试的配置文件。

参考这个新的解决方案: http://watirmelon.com/2012/06/27/automatic-firefox-authentication-when-using-selenium-webdriver-with-autoauth/

Contributing to Druska´s answer, you can do the same configuration using Selenium 2 API:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.http.phishy-userpass-length", 255);
profile.setPreference("network.automatic-ntlm-auth.trusted-uris","yourDomain");
new FirefoxDriver(profile);

This approach is simpler and you do not have to ask every developer to change their Firefox configuration. I only tested with the Firefox driver.

UPDATE:

For some reason (maybe the ones explained at https://stackoverflow.com/a/14348701/256245), the above solution does not work with newer versions of Firefox. Here is what works for me now (tested with Firefox 19.0.2):

  1. Install AutoAuth Firefox plugin;
  2. Visit the site where the authentication is needed. Enter your username and password and make sure to choose to save the credentials;
  3. Save AutoAuth installation file at your hard drive: at the plugin page, right click at “Add to Firefox” and “Save link as”;
  4. Instantiate Firefox webdriver as following:

    FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default");
    File pluginAutoAuth = new File("src/test/resources/autoauth-2.1-fx+fn.xpi");
    firefoxProfile.addExtension(pluginAutoAuth);
    return new FirefoxDriver(firefoxProfile);
    

Make sure to instantiate the pluginAutoAuth File with the correct path where you saved the plugin installation. If you do not feel comfortable using the default profile, you can use Firefox Profile Manager and create one specific to your tests.

Reference to this new solution: http://watirmelon.com/2012/06/27/automatic-firefox-authentication-when-using-selenium-webdriver-with-autoauth/

橘寄 2024-09-12 09:58:21

在上下文根后添加斜杠:

而不是:
selenium.open("http://myusername:[电子邮件受保护]/mypath");

使用:
selenium.open("http://myusername:[电子邮件受保护]/mypath/");

它在上下文根的末尾添加了斜杠,这使得世界变得不同。如果没有斜杠,弹出窗口将打开,如果有斜杠,它将按预期进行身份验证。

请注意,这不是 selenium bug 或其他什么,而是 Firefox 的问题。您也可以使用命令行亲自查看:

 C:\Program Files\Mozilla Firefox>firefox http://myusername:[email protected]/mypath/

对我来说,即使没有设置网络 uri,它也可以工作:

FirefoxProfile profile = new FirefoxProfile();
//profile.setPreference("network.automatic-ntlm-auth.trusted-uris", "mydomain.com");
//profile.setPreference("network.negotiate-auth.trusteduris", "mydomain.com");

WebDriver driver = new FirefoxDriver(profile);

driver.navigate().to("http://myusername:[email protected]/mypath/");

版本
火狐浏览器 19.0,
硒-java 2.31.0

Add a slash after the context root:

Instead of:
selenium.open("http://myusername:[email protected]/mypath");

use:
selenium.open("http://myusername:[email protected]/mypath/");

It makes all the difference of the world adding the slash at the end of the context root. Without the slash, the popup opens, with the slash it gets authenticated as expected.

Note, that this is not a selenium bug or whatnot, but a firefox thing. You can use your command line as well to see for yourself:

 C:\Program Files\Mozilla Firefox>firefox http://myusername:[email protected]/mypath/

For me, it works even without settings the networks uris:

FirefoxProfile profile = new FirefoxProfile();
//profile.setPreference("network.automatic-ntlm-auth.trusted-uris", "mydomain.com");
//profile.setPreference("network.negotiate-auth.trusteduris", "mydomain.com");

WebDriver driver = new FirefoxDriver(profile);

driver.navigate().to("http://myusername:[email protected]/mypath/");

versions
Firefox 19.0,
selenium-java 2.31.0

一绘本一梦想 2024-09-12 09:58:21

如果您使用的是 FireFox 驱动程序...您可以创建 FireFox 配置文件并将用户名/密码保存在密码管理器中,并使用插件自动登录。请记住,如果您在 Selenium 中创建 FireFox 或 Chrome 驱动程序,默认情况下它会使用匿名配置文件。因此,您的常规扩展/附加组件/等都不会被使用。因此,最好创建一个可以在源代码管理中分发和保存的配置文件。

1) 在 Windows 中,从运行/开始菜单中键入“firefox.exe -p”以调出配置文件管理器并创建一个自定义配置文件管理器,并将其与其余代码一起保存在某个位置。

2) 选中“启动时不询问”

3) 下载 AutoAuth 插件 https ://addons.mozilla.org/en-US/firefox/addon/autoauth/

4) 访问需要 HTTP 基本身份验证的网站并保存凭据

下次您访问该网站时,AutoAuth 将让您登录,无需使用 HTTP 基本身份验证出现需要验证的提示。

如果您有 NTLM,则可以修改配置设置以包含主机名:network.automatic-ntlm-auth.trusted-uris

If you are using the FireFox Driver ... You can create a FireFox profile and save the username/pass in password manager and use an add-on to auto login. Remember if you create a FireFox or Chrome driver in Selenium, by default it uses an anonymous profile. So none of your regular extensions/add-ons/etc will be used. So it's best ot create a profile that can be distributed and saved in source control.

1) In Windows, from the run/start menu type "firefox.exe -p" to bring up the Profile Manager and create a custom one and save it in a location with the rest of your code.

2) Don't ask at startup is checked

3) Download AutoAuth add-on https://addons.mozilla.org/en-US/firefox/addon/autoauth/

4) Visit the site that requires HTTP Basic Authentication and save the credentials

Next time you visit the site, AutoAuth will login you without the authentication required prompt showing up.

If you have NTLM, you can modify the configuration setting to include the host names: network.automatic-ntlm-auth.trusted-uris

葵雨 2024-09-12 09:58:21

您可以尝试像这样直接操作标头:

首先,当您开始时,您必须启用 Selenium ti 操作标头:

selenium.start("addCustomRequestHeader=true");

然后您必须使用一些基本的编码和标头操作,如下所示:

    String authHeader = "";
    try {
    BASE64Encoder coder = new BASE64Encoder();
    authHeader = coder.encode("developers:Str492ight".getBytes());
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    setUpSelenium();
    startSelenium();
    selenium.addCustomRequestHeader("Authorization", "Basic " + authHeader);
    selenium.open("/");
    selenium.waitForPageToLoad("10000");

Basic 后面的空格是必要的。这就是基本 HTTP 身份验证标头的样子。

此外,您可以使用一些 Http Watchers 来查看请求是否包含您的身份验证请求。

要么使用 Wireshark,要么更好的是 Fiddler 或 Charles Proxy。

希望有帮助。
盖尔盖伊。

You could try to manipulate the headers directly like this:

First when you start, you have to enable Selenium ti manipulate headers:

selenium.start("addCustomRequestHeader=true");

Then you have to use some basic encoding and header manipulation like this:

    String authHeader = "";
    try {
    BASE64Encoder coder = new BASE64Encoder();
    authHeader = coder.encode("developers:Str492ight".getBytes());
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    setUpSelenium();
    startSelenium();
    selenium.addCustomRequestHeader("Authorization", "Basic " + authHeader);
    selenium.open("/");
    selenium.waitForPageToLoad("10000");

The space after Basic is necessary. This is how a basic HTTP authentication header looks like..

Further more you could use some Http Watchers to see if the request contains your auth request.

Either use Wireshark, or better is Fiddler or Charles Proxy.

Hope that helped.
Gergely.

2024-09-12 09:58:21

如前所述,addCustomRequestHeader 解决方案只能在代理注入模式下工作。但是当我尝试实现它时,我遇到了与代理注入模式相关的其他问题。

我不清楚代理注入在使用 Java 客户端时是否有效。每当我调用 open() 时,都会收到一个奇怪的错误:“this.onXhrStateChange.bind 不是一个函数”。
我发现的唯一解决方案意味着您需要向 open() 方法添加一个值为“true”的额外参数,但 Java 客户端 API 只接受单个参数。

因此,我不得不接受上面解释的浏览器配置解决方案,我对此不太满意,因为它们取决于供应商支持它们的意愿。

将您的测试移植到 Selenium 2(目前仍为 alpha)可能是一个更好的前景,但就我而言,在 Selenium Grid 支持 Selenium 2 之前这是不可能的。

希望可以帮助任何人,
塞巴斯蒂安

As mentioned, the addCustomRequestHeader solution can only work with proxy injection mode. But when I tried to implement it, I got into other issues related to that proxy injection mode.

It's not clear to me if proxy injection even work at all when using the Java client. Anytime I would call open(), I got a weird error stating: "this.onXhrStateChange.bind is not a function".
The only solution I found implied that you need to add an extra parameter with the value 'true' to the open() method but the Java client API only accepts a single parameter.

So I had to settle for the browser config solutions explained above which I don't really feel comfortable with since they depend on the vendor's willingness to support them.

Porting your tests to Selenium 2 (still alpha as of now) might be a better prospect but in my case it won't be possible until Selenium Grid supports Selenium 2.

Hope that can help anyone,
Sebastien

一场春暖 2024-09-12 09:58:21

Firefox 17 默认情况下禁止“用户名:密码”(RFC1738) 处理(之前曾有效)。但是,我发现它可以通过以下方式重新启用:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.negotiate-auth.trusteduris", hostname);
driver = new FirefoxDriver(profile);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
selenium = new WebDriverBackedSelenium(driver, "http:// + username + ":"
    + password + "@"
    + hostname + ":" + port + baseUrl);

适用于 Selenium 2.28.0、Firefox 17;用于 DigestAuth 登录。

Firefox 17 'username:password' (RFC1738) processing is disallowed by default in Firefox (it had worked earlier). However, I've found that it can be re-enabled by:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.negotiate-auth.trusteduris", hostname);
driver = new FirefoxDriver(profile);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
selenium = new WebDriverBackedSelenium(driver, "http:// + username + ":"
    + password + "@"
    + hostname + ":" + port + baseUrl);

Works on Selenium 2.28.0, Firefox 17; used for DigestAuth login.

简单爱 2024-09-12 09:58:21

那么,您可以使用 Sikuli 脚本来处理 Windows 和 Linux 环境中的 Firefox 身份验证弹出窗口。

  • Windows/Linux 中下载并安装 Sikuli(需要安装依赖项)
  • 使用以下命令用于处理弹出窗口的 Sikuli 脚本:其中 Authentilcat1.png 是弹出图像,它将

在范围 (100) 内处理 100 个弹出窗口
而存在(模式(“Authentlcatl.png”)。类似(0.99)):
print("发现身份验证弹出窗口")
等待(2)
类型(“admin”+Key.TAB)
type("admin" + Key.ENTER)

  • 使用以下代码从 Java 代码触发和终止 Sikuli 脚本:

触发 Sikuli 脚本

String[] lincmd = { "bash", "-c", "sudo java -jar Sikuli-X/Sikuli-IDE/sikuli-script.jar Sikuli-X/Sikuli-IDE/new1.sikuli/" };

java.lang.Runtime.getRuntime( ).exec(lincmd);

终止 Sikuli 脚本

String[] lincmd = { "bash", "-c", "sudo kill $(ps aux | grep '[s]ikuli' | awk '{print $2}')" };

java.lang.Runtime.getRuntime().exec(lincmd);

  • 从 Java 代码触发 Sikuli 脚本后,Sikuli 脚本将作为另一个进程单独运行,因此最终在 Java 代码中终止 Sikuli 脚本。

    从 Java 代码触发

  • 因此,每当屏幕中出现弹出窗口时,Sikuli 脚本都会处理。

Well, you can make you use of the Sikuli script to handle this Firefox Authentication popup in Windows as well as in Linux environment.

  • Download and Setup Sikuli in Windows/Linux(Need to install dependencies)
  • Use the following Sikuli Script to handle popup: where Authentilcat1.png is the popup image and it will handle 100 popups

for i in range (100):
while exists(Pattern("Authentlcatl.png").similar(0.99)):
print("Found Authentication Popup")
wait(2)
type("admin" + Key.TAB)
type("admin" + Key.ENTER)

  • Use the following code to trigger and terminate the Sikuli script from Java code:

To Trigger the Sikuli Script:

String[] lincmd = { "bash", "-c", "sudo java -jar Sikuli-X/Sikuli-IDE/sikuli-script.jar Sikuli-X/Sikuli-IDE/new1.sikuli/" };

java.lang.Runtime.getRuntime().exec(lincmd);

To Terminate the Sikuli Script:

String[] lincmd = { "bash", "-c", "sudo kill $(ps aux | grep '[s]ikuli' | awk '{print $2}')" };

java.lang.Runtime.getRuntime().exec(lincmd);

  • After triggering the Sikuli script from Java code,Sikuli script will run as another process separately,so finally in the Java code terminating the Sikuli script.

  • So whenever the popup appears in the screen,Sikuli script will handle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文