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

Lin*_*ica 7 java selenium google-chrome basic-authentication selenium-webdriver

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

WebDriver driver  = ...;
driver.get("http://admin:admin@localhost:8080/project/");
Run Code Online (Sandbox Code Playgroud)

但谷歌Chrome在控制台中给出了以下警告:

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

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

在子资源请求中删除对嵌入式凭据的支持.(已删除)

我现在的问题是,还有另一种从Selenium进行基本身份验证的方法吗?

注意:这没有帮助:如何使用Java处理Selenium Webdriver中的HTTP Basic Auth头?

Flo*_* B. 5

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

driver.get("http://admin:admin@localhost:8080");
driver.get("http://localhost:8080/project");
Run Code Online (Sandbox Code Playgroud)

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

options = webdriver.ChromeOptions()
options.add_extension(r'C:\dev\credentials.zip')
Run Code Online (Sandbox Code Playgroud)

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


Deb*_*anB 5

对此进行了一些更新link:

Chromium Issue 435547在子资源请求中放弃对嵌入式凭据的支持。(已删除)

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

但是,基本身份验证功能仍然可以通过Selenium-Java绑定与Selenium 3.4.0,geckodriver v0.18.0,chromedriver v2.31.488763,Google Chrome 60.x和Mozilla Firefox 53.0一起使用。

这是示例代码,它尝试使用一组有效的凭据打开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:admin@the-internet.herokuapp.com/basic_auth");
    }
}
Run Code Online (Sandbox Code Playgroud)

铬:

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");
    }
}
Run Code Online (Sandbox Code Playgroud)