【问题标题】:Selenium - Basic Authentication via urlSelenium - 通过 url 进行基本身份验证
【发布时间】:2018-01-02 21:26:46
【问题描述】:

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

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

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

[弃用] 其 URL 包含嵌入式凭据(例如 https://user:pass@host/)的子资源请求将被阻止。详情请见https://www.chromestatus.com/feature/5669008342777856

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

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

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

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

【问题讨论】:

  • @jadupl 链接中建议的解决方案对我不起作用,第一次调用后凭据未缓存...
  • 那么您可能唯一的解决方案是设置您将与 WebDriver 连接的代理服务器,并且代理服务器将添加基本身份验证标头

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


【解决方案1】:

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

【讨论】:

  • 您的第一个建议对我不起作用:/ 在运行 selenium 测试时,我仍然会在 chrome 中收到登录提示。另一个我没看过
  • @florent-b,我一直在到处寻找这样的东西!选项 #2 解决了 AJAX 请求、CGI 表单提交、重定向等问题。
  • 该扩展程序运行良好,但不适用于 CI 上无头模式下的 Chrome :-(
  • 任何人都可以帮助并扩展 JAVA 的这个答案吗? - 单独上面的两个选项行不起作用
  • 完美无瑕!谢谢
【解决方案2】:

link 中有一些更新:

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

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

但是,基本身份验证功能仍然适用于 Selenium 3.4.0geckodriver v0.18.0chromedriver v2.31.488763、Google Chrome 60.xMozilla Firefox 53.0 通过 Selenium-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: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");
    }
}

【讨论】:

    【解决方案3】:

    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 上工作,该方法似乎与带有 geckodriver 0.19.1 的 firefox 56.0.2 和 phantomjs 2.1.1 都在 Debian linux 9 下兼容。

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        直接使用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+"')");
        

        【讨论】:

          猜你喜欢
          • 2017-09-18
          • 2019-02-05
          • 2016-06-17
          • 1970-01-01
          • 1970-01-01
          • 2017-07-02
          相关资源
          最近更新 更多