java Selenium - 通过 url 进行基本身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45345882/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:39:07  来源:igfitidea点击:

Selenium - Basic Authentication via url

javagoogle-chromeseleniumselenium-webdriverbasic-authentication

提问by Lino

In my Selenium-Test(with chromedriver-2.24) I'm trying to access my webpage via basic authentication with the following statement:

在我的Selenium-Test(with chromedriver-2.24) 中,我尝试使用以下语句通过基本身份验证访问我的网页:

WebDriver driver  = ...;
driver.get("http://admin:admin@localhost:8080/project/");

But Google Chrome gives me the following warning in the console:

但是谷歌浏览器在控制台中给了我以下警告:

[Deprecation] Subresource requests whose URLs contain embedded credentials (e.g. https://user:pass@host/) are blocked. See https://www.chromestatus.com/feature/5669008342777856for more details.

[弃用] 其 URL 包含嵌入式凭据(例如https://user:pass@host/)的子资源请求被阻止。有关更多详细信息,请参阅https://www.chromestatus.com/feature/5669008342777856

In the tagged link is mentioned that the support was dropped:

在标记的链接中提到支持已被删除:

Drop support for embedded credentials in subresource requests. (removed)

取消对子资源请求中嵌入凭据的支持。(已删除)

My question now is, is there an other way to basic-authenticate from Selenium?

我现在的问题是,有没有其他方法可以从 Selenium 进行基本身份验证?

NOTE: this has not helped: How to Handle HTTP Basic Auth headers in Selenium Webdriver using Java ?

注意:这没有帮助:How to Handle HTTP Basic Auth headers in Selenium Webdriver using Java ?

采纳答案by DebanjanB

There were some updates in this linkas :

这方面有一些更新link

Chromium Issue 435547Drop support for embedded credentials in subresource requests. (removed)

We should block requests for subresources that contain embedded credentials (e.g. "http://ima_user:[email protected]/yay.tiff"). Such resources would be handled as network errors.

Chromium Issue 435547取消对子资源请求中嵌入凭据的支持。(已删除)

我们应该阻止对包含嵌入式凭据的子资源的请求(例如“ http://ima_user:[email protected]/yay.tiff”)。此类资源将作为网络错误处理。

However, Basic Authenticationfunctionality still works with Selenium 3.4.0, geckodriver v0.18.0, chromedriver v2.31.488763, Google Chrome 60.xand Mozilla Firefox 53.0through Selenium-Javabindings.

但是,通过Selenium-Java绑定,基本身份验证功能仍然适用于Selenium 3.4.0geckodriver v0.18.0chromedriver v2.31.488763Google Chrome 60.xMozilla Firefox 53.0

Here is the example code which tries to open the URL http://the-internet.herokuapp.com/basic_authwith a valid set of credentials and it works.

这是示例代码,它尝试使用一组有效的凭据打开 URL http://the-internet.herokuapp.com/basic_auth并且它可以工作。

Firefox:

火狐:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BasicAuthentication_FF 
{
    public static void main(String[] args) 
    {
        System.setProperty("webdriver.gecko.driver", "C:\Utility\BrowserDrivers\geckodriver.exe");
        WebDriver driver =  new FirefoxDriver();
        driver.navigate().to("http://admin:[email protected]/basic_auth");
    }
}

Chrome:

铬合金:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class BasicAuthentication_Chrome 
{
    public static void main(String[] args) 
    {
        System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("start-maximized");
        options.addArguments("disable-infobars");
        options.addArguments("--disable-extensions");
        WebDriver driver =  new ChromeDriver(options);
        driver.navigate().to("http://admin:[email protected]/basic_auth");
    }
}

回答by Florent B.

The basic authentication via url is blocked only for sub resources. So you could still use it on the domain:

仅针对子资源阻止通过 url 进行的基本身份验证。所以你仍然可以在域上使用它:

driver.get("http://admin:admin@localhost:8080");
driver.get("http://localhost:8080/project");

You could also create a small extension to automatically set the credentials when they are requested:

您还可以创建一个小的扩展来在请求时自动设置凭据:

options = webdriver.ChromeOptions()
options.add_extension(r'C:\dev\credentials.zip')

https://gist.github.com/florentbr/25246cd9337cebc07e2bbb0b9bf0de46

https://gist.github.com/florentbr/25246cd9337cebc07e2bbb0b9bf0de46

回答by Tongfa

Florent B.'s approach of calling .get on the URL twice worked for me with a slight modification. In JS:

Florent B. 在 URL 上调用 .get 的方法两次对我有用,但稍作修改。在JS中:

driver
        .get('http://admin:admin@localhost:8080')
        .then( () => driver.get('http://localhost:8080') )

working on google chrome 62.0.3202.94 with ChromeDriver 2.33.506092 and the approach seems compatible with firefox 56.0.2 with geckodriver 0.19.1, and phantomjs 2.1.1 all under Debian linux 9.

在使用 ChromeDriver 2.33.506092 的 google chrome 62.0.3202.94 上工作,该方法似乎与带有 geckodriver 0.19.1 的 firefox 56.0.2 和 phantomjs 2.1.1 兼容,所有这些都在 Debian linux 9 下。

What I believe is happening is the first call sets up the Authorization header sent by the browser. The second call removes the credentials from the URL and the credentials no longer are applied to subresources. The thensynchronizes the two requests ensuring order.

我相信正在发生的是第一个调用设置浏览器发送的授权标头。第二个调用从 URL 中删除凭据,并且凭据不再应用于子资源。在then同步两个要求,确保秩序。

回答by Henning Luther

New features for chrome and basic authentication via remote-debug: just for linking it here, so people who are stuck can find a solution for chrome and more: Chrome remote debugging in a seleniumgrid

chrome 的新功能和通过远程调试的基本身份验证:仅用于将其链接到此处,以便卡住的人可以找到 chrome 等的解决方案:seleniumgrid 中的 Chrome 远程调试