Selenium - 通过url进行基本认证 [英] Selenium - Basic Authentication via url

查看:799
本文介绍了Selenium - 通过url进行基本认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但Google Chrome在控制台中给了我以下警告:


[弃用]其URL包含嵌入凭证的子资源(例如 https:// user:pass @ host / )是受阻。有关详细信息,请参见 https://www.chromestatus.com/feature/5669008342777856 。 p>

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


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

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



注意 :这并没有帮助:

这次 link 在那里公开宣布:


支持子资源请求中的嵌入凭证。 (已删除)

我们应该阻止对包含嵌入式凭据的子资源的请求(例如 http:// ima_user:hunter2@example.com/yay.tiff )。这些资源将作为网络错误处理。



这些信息是按照 Chromium Issue 435547

我想分享基本身份验证功能仍然有效与 Selenium 3.4.0 geckodriver v0.18.0 chromedriver v2.31.488763 Google Chrome 60.x Mozilla Firefox 53.0 至 Selenium-Java 绑定。



以下是试图打开URL的示例代码 http://the-internet.herokuapp.com/basic_auth 以及一组有效的凭证。



Firefox:



  import org.openqa.selenium.WebDriver; 
导入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()。(http:// admin:admin@the-internet.herokuapp.com/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\\\\\\\\\\\\\\\'
ChromeOptions选项=新ChromeOptions();
options.addArguments(start-maximized);
options.addArguments(disable-infobars);
options.addArguments( - disable-extensions);
WebDriver驱动程序=新的C​​hromeDriver(选项);
driver.navigate()。(http:// admin:admin@the-internet.herokuapp.com/basic_auth);
}
}


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

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/5669008342777856 for more details.

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?

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

解决方案

There were some updates in this link where it was publicly announced that:

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

We should block requests for subresources that contain embedded credentials (e.g. "http://ima_user:hunter2@example.com/yay.tiff"). Such resources would be handled as network errors.

This information is as per Chromium Issue 435547

I will like to share that Basic Authentication functionality still works with Selenium 3.4.0, geckodriver v0.18.0, chromedriver v2.31.488763, Google Chrome 60.x and Mozilla Firefox 53.0 through Selenium-Java bindings.

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

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:admin@the-internet.herokuapp.com/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:admin@the-internet.herokuapp.com/basic_auth");
    }
}

这篇关于Selenium - 通过url进行基本认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆