selenium – 其他方式基本authentication比通过url

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

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

但Google Chrome在控制台中给了我以下警告:

[不推荐使用]子网资源URL包含embedded式凭证的请求(例如https://user:pass@host/ )被阻止。 有关更多详细信息,请参见https://www.chromestatus.com/feature/5669008342777856 。

在被标记的链接被提及支持被丢弃了:

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

我现在的问题是,还有其他的方式来从selenium基本authentication?

注意 :这并没有帮助: 如何在Selenium Webdriver中使用Java处理HTTP Basic Auth头文件?

这是你的问题的答案:

这个link有一些更新,公开宣布:

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

我们应该阻止对包含embedded凭证的子资源的请求(例如“ http:// ima_user:hunter2@example.com/yay.tiff ”)。 这些资源将被视为networking错误。

此信息按照Chromium Issue 435547

我想要分享的Basic Authenticationfunction仍然适用于Selenium 3.4.0geckodriver v0.18.0chromedriver v2.31.488763Google 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"); } } 

如果答案是你的问题,让我来回答。

通过url的基本身份validation仅对子资源被阻止。 所以你仍然可以在域上使用它:

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

您也可以创build一个小扩展名,以便在请求时自动设置凭据:

 options = webdriver.ChromeOptions() options.add_extension(r'C:\dev\credentials.zip') 

https://gist.github.com/florentbr/25246cd9337cebc07e2bbb0b9bf0de46