Selenium - 通过 url 进行基本身份验证 [英] Selenium - Basic Authentication via url
问题描述
在我的 Selenium-Test
(使用 chromedriver-2.24
)中,我尝试使用以下语句通过基本身份验证访问我的网页:
In my Selenium-Test
(with chromedriver-2.24
) I'm trying to access my webpage via basic authentication with the following statement:
WebDriver driver = ...;
driver.get("http://admin:admin@localhost:8080/project/");
但谷歌浏览器在控制台中给了我以下警告:
But Google Chrome gives me the following warning in the console:
[弃用] URL 包含嵌入式凭据(例如 https://user:pass@host/
)的子资源请求被阻止.有关详细信息,请参阅 https://www.chromestatus.com/feature/5669008342777856.
[Deprecation] Subresource requests whose URLs contain embedded credentials (e.g.
https://user:pass@host/
) are blocked. See https://www.chromestatus.com/feature/5669008342777856 for more details.
在标记的链接中提到已放弃支持:
In the tagged link is mentioned that the support was dropped:
放弃对子资源请求中嵌入凭据的支持.(已移除)
Drop support for embedded credentials in subresource requests. (removed)
我现在的问题是,有没有其他方法可以从 Selenium 进行基本身份验证?
My question now is, is there an other way to basic-authenticate from Selenium?
注意:这没有帮助:如何使用 Java 在 Selenium Webdriver 中处理 HTTP Basic Auth 标头?
推荐答案
在这个<中有一些更新strong>link
为:
Chromium 问题 435547
取消对子资源请求中嵌入凭据的支持.(已移除)
Chromium Issue 435547
Drop support for embedded credentials in subresource requests. (removed)
我们应该阻止对包含嵌入式凭据的子资源的请求(例如http://ima_user:Hunter2@example.com/yay.tiff").此类资源将作为网络错误处理.
We should block requests for subresources that contain embedded credentials (e.g. "http://ima_user:hunter2@example.com/yay.tiff"). Such resources would be handled as network errors.
但是,基本身份验证功能仍然适用于Selenium 3.4.0、geckodriver v0.18.0、chromedriver v2.31.488763、Google Chrome 60.x 和 Mozilla Firefox 53.0 通过 Selenium-Java 绑定.
However, Basic Authentication functionality still works with Selenium 3.4.0, geckodriver v0.18.0, chromedriver v2.31.488763, Google Chrome 60.x and Mozilla Firefox 53.0 through Selenium-Java bindings.
这是尝试打开 URL 的示例代码 http://the-internet.herokuapp.com/basic_auth 使用一组有效的凭据,它可以工作.
Here is the example code which tries to open the URL http://the-internet.herokuapp.com/basic_auth with a valid set of credentials and it works.
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");
}
}
这篇关于Selenium - 通过 url 进行基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!