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

问题描述 投票:0回答:6

在我的硒测试中(使用 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 了解更多详细信息。

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

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

我现在的问题是,还有其他方法可以使用硒进行基本身份验证吗?

注意:这个其他问题对我没有帮助:How to Handle HTTP Basic Auth headers in Selenium Webdriver using Java ?

java google-chrome selenium-webdriver basic-authentication
6个回答
10
投票

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

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

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

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

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


7
投票

link
有一些更新:

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

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

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

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

火狐:

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");
    }
}

铬:

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");
    }
}

2
投票

Florent B. 在 URL 上调用 .get 两次的方法对我来说很有效,只需稍加修改即可。在 JS 中:

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

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

我相信正在发生的是第一个调用设置了浏览器发送的授权标头。第二次调用从 URL 中删除凭据,并且凭据不再应用于子资源。

then
同步两个请求以确保顺序。


2
投票

chrome 的新功能和通过远程调试进行基本身份验证:只是为了将其链接到这里,因此陷入困境的人可以找到 chrome 等的解决方案:seleniumgrid 中的 Chrome 远程调试


2
投票

Selenium 4 支持基本身份验证

WebDriver driver = new ChromeDriver();
HasAuthentication authentication = (HasAuthentication) driver;
authentication.register(() -> new UsernameAndPassword("username", "pwd"));
driver.get("your-site.com");

https://www.selenium.dev/blog/2021/a-tour-of-4-authentication/


-1
投票

直接使用selenium driver.get(URL)方法在JavaScript Popup中加载提示进行身份验证的URL将不支持这种Basic身份验证,我也被困在这里很长一段时间。这是因为 Chrome 驱动程序在更新 59(可能)之后将不允许这种身份验证技术。仍然存在通过 Selenium 的后门,使用浏览器中的 JavaScript 引擎来加载此类 URL。

driver.get("https://www.google.com");
JavascriptExecutor jse = (JavascriptExecutor) driver;
URL = "https://username:[email protected]";
jse.executeScript("window.open('"+URL+"')");
© www.soinside.com 2019 - 2024. All rights reserved.