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

时间:2017-07-27 08:54:59

标签: java google-chrome selenium selenium-webdriver http-basic-authentication

在我的Selenium-Testchromedriver-2.24)中,我试图通过基本身份验证访问我的网页,声明如下:

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

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

  

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

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

  

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

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

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

5 个答案:

答案 0 :(得分:5)

仅通过子资源阻止通过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

答案 1 :(得分:4)

link中有一些更新:

  

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

     

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

但是,基本身份验证功能仍适用于 Selenium 3.4.0 geckodriver v0.18.0 chromedriver v2.31.488763 < / em>, Google Chrome 60.x Mozilla Firefox 53.0 通过 Selenium-Java 绑定。

以下是尝试使用一组有效凭据打开网址 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:admin@the-internet.herokuapp.com/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:admin@the-internet.herokuapp.com/basic_auth");
    }
}

答案 2 :(得分:2)

Florent B.的调用URL的方法两次为我工作稍作修改。在JS中:

60

使用ChromeDriver 2.33.506092处理谷歌浏览器62.0.3202.94,该方法似乎与geckodriver 0.19.1的firefox 56.0.2和Debian linux 9下的phantomjs 2.1.1兼容。

我认为正在发生的是第一次调用设置浏览器发送的Authorization标头。第二个调用将从URL中删除凭据,并且凭据不再应用于子资源。 driver .get('http://admin:admin@localhost:8080') .then( () => driver.get('http://localhost:8080') ) 同步确保订单的两个请求。

答案 3 :(得分:2)

通过远程调试进行Chrome和基本身份验证的新功能:仅用于在此处进行链接,因此卡住的人可以找到Chrome及更多解决方案:Chrome remote debugging in a seleniumgrid

答案 4 :(得分:0)

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

driver.get("https://www.google.com");
JavascriptExecutor jse = (JavascriptExecutor) driver;
URL = "https://username:password@www.example.com";
jse.executeScript("window.open('"+URL+"')");