Selenium – 通过URL进行基本身份validation
在我的Selenium-Test
(使用chromedriver-2.24
)中,我尝试使用以下语句通过基本身份validation访问我的网页:
WebDriver driver = ...; driver.get("http://admin:admin@localhost:8080/project/");
但谷歌Chrome在控制台中给出了以下警告:
[弃用]其URL包含嵌入凭据(例如
https://user:pass@host/
)的子资源请求被阻止。 有关详细信息,请参阅https://www.chromestatus.com/feature/5669008342777856 。
在标记的链接中提到支持被删除:
在子资源请求中删除对嵌入式凭据的支持。 (已删除)
我现在的问题是,还有另一种从Selenium进行基本身份validation的方法吗?
注意 :这没有帮助: 如何使用Java处理Selenium Webdriver中的HTTP Basic Auth头?
此link
有一些更新:
Chromium Issue 435547
删除Chromium Issue 435547
资源请求中嵌入式凭据的支持。 (已删除)我们应该阻止对包含嵌入凭证的子资源的请求(例如“ http:// ima_user:hunter2@example.com/yay.tiff ”)。 此类资源将作为网络error handling。
但是, 基本身份validationfunction仍适用于Selenium 3.4.0 , geckodriver v0.18.0 , chromedriver v2.31.488763 , Google Chrome 60.x和Mozilla 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");
您还可以创建一个小扩展,以便在请求时自动设置凭据:
options = webdriver.ChromeOptions() options.add_extension(r'C:\dev\credentials.zip')
https://gist.github.com/florentbr/25246cd9337cebc07e2bbb0b9bf0de46
Florent B.的调用URL的方法两次为我工作稍作修改。 在JS中:
driver .get('http://admin:admin@localhost:8080') .then( () => driver.get('http://localhost:8080') )
使用ChromeDriver 2.33.506092处理谷歌浏览器62.0.3202.94并且该方法似乎与geckodriver 0.19.1的firefox 56.0.2和Debian linux 9下的phantomjs 2.1.1兼容。
我认为发生的是第一次调用设置浏览器发送的Authorization标头。 第二个调用将从URL中删除凭据,并且凭据不再应用于子资源。 then
同步两个确保订单的请求。
通过远程调试进行chrome和基本身份validation的新function:仅用于在此处进行链接,因此卡住的人可以找到chrome及更多解决方案: 在seleniumgrid中进行Chrome远程调试