在Selenium-Test
(带有chromedriver-2.24
)中,我尝试使用以下语句通过基本身份验证访问我的网页:
WebDriver driver = ...;
driver.get("http://admin:admin@localhost:8080/project/");
但是Google Chrome浏览器在控制台中向我发出以下警告:
[弃用]其URL包含嵌入式凭据(例如
https://user:pass@host/
)的子资源请求被阻止。
在标记的链接中提到该支持已被删除:
在子资源请求中放弃对嵌入式凭据的支持。(已删除)
我现在的问题是,是否有另一种方法可以从Selenium进行基本身份验证?
对此进行了一些更新 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");
}
}