selenium-通过url的基本身份验证

yi0zb3m4  于 2021-07-06  发布在  Java
关注(0)|答案(5)|浏览(364)

在我的 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 更多细节。
在标记的链接中,提到支架被丢弃:
放弃对子资源请求中嵌入凭据的支持((已删除)
我现在的问题是,有没有其他方法可以从selenium进行基本身份验证?
注意:这没有帮助:如何使用java处理seleniumwebdriver中的httpbasic auth头?

xt0899hw

xt0899hw1#

通过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

b09cbbtk

b09cbbtk2#

直接使用selenium driver.get(url)方法在javascript弹出窗口中加载提示进行身份验证的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+"')");
kx5bkwkv

kx5bkwkv3#

这里面有一些更新 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 使用一组有效的凭据,它就可以工作了。

火狐:

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

6l7fqoea4#

chrome的新功能和通过远程调试进行的基本身份验证:只需将其链接到此处,这样陷入困境的人就可以找到chrome的解决方案,还有更多:在seleniumgrid中进行chrome远程调试

yacmzcpb

yacmzcpb5#

弗洛伦特b.的通话方式。两次登陆网址对我来说都很管用,只是稍作修改。在js中:

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

使用chromedriver 2.33.506092开发google chrome 62.0.3202.94,该方法似乎与firefox 56.0.2、geckodriver 0.19.1和phantomjs 2.1.1兼容,所有这些都在debian linux 9下。
我认为第一个调用设置了浏览器发送的授权头。第二个调用从url中删除凭据,并且凭据不再应用于子资源。这个 then 同步两个请求以确保顺序。

相关问题