Chrome的59 removed support for https://user:[email protected] URLs。
我有一个需要与Chrome Version 60 on Windows in 'headless' mode工作一个C#硒测试
ChromeOptions options = new ChromeOptions();
options.AddArgument("headless");
driver = new ChromeDriver(chrome, options);
下面是SAML认证所需的对话框我想处理Windows的:
基于这里给出的答案是:在无头模式How to handle authentication popup with Selenium WebDriver using Java)我可以看到几个工作在Firefox处理这个,但没有为Chrome 60。
我试过下面的代码访问URL测试(不含凭证)之前访问与证书的URL但它似乎有一个与Chrome的60 bug。
goTo("http://user:password@localhost"); // Caches auth, but page itself is blocked
goTo("http://localhost"); // Uses cached auth, page renders fine
// Continue test as normal
我可以看到下面的代码在Firefox处理认证和对话从来没有弹出:
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", "https://saml.domain.com");
profile.EnableNativeEvents = false;`
我已经尝试了第二种方法(using AutoIt),以及适用于Chrome 60,但无头模式在Chrome 60不起作用。
//Use AutoIt to wait 4 seconds for the authentication required dialog to appear
au3.Sleep(4000);
//Use AutoIT to send in the credentials from app.config that are encrypted
au3.Send(USERNAME + "{TAB}" + PASSWORD + "{ENTER}");
//Refresh the page
driver.Navigate().Refresh();
我希望有一个更好的解决方案,现在在2017年,并且有将与Chrome的60无头模式下工作,任何指针的方法?
只是要清楚:试图用embedded credentials使用Chrome V59 +,因为子资源请求都会被封锁将无法工作。
我知道这几乎是一个岁,但是这是结束了在类似的情况为我工作。这是事实,认证弹出已发生变化,ChromeDriver似乎并不支持或http(s)://user:[email protected]
方案了,但变通,我发现here似乎这样的伎俩。它最初是写来验证代理系统,但可以修改任何认证系统的工作。
基本上你需要做的是处理页面上输入登录细节的Chrome扩展。 Chrome扩展程序,可与ChromeOptions.AddExtension(string FilePath)
添加和扩展仅仅是一个带有manifest.json
任何代码文件做的工作的zip文件。这里有你需要的文件。
的manifest.json
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
background.js
function callbackFn(details) {
return {
authCredentials: {
username: "YOUR_PROXY_USERNAME",
password: "YOUR_PROXY_PASSWORD"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["YOUR_WEBSITE_ADDRESS"]},
['blocking']
);
也许你可以执行使用一个XMLHttpRequest使用方法setRequestHeader,所以你可以指定认证头先前的连接。执行你会被认证为所有其他类型的请求的一个请求后。更大的问题可能是XSS,根据您的情况,你也许可以变通办法。
您可以使用Chrome浏览器扩展了“HTTP基本身份验证的MultiPass”来处理这个问题。
你可以通过GitHub的MultiPass for HTTP basic authentication做
(要么)
下载从Chrome网上应用店的扩展 - MultiPass Chrome Extension
(要么)
下载扩展为CRX。你可以把它从chrome-extension-downloader CRX
一旦你下载扩展为CRX文件 - 配置相同的到您的测试/来源是非常简单的。
而这可以通过Sample Basic Auth-Site进行测试。
public class ChromeAuthTest {
WebDriver driver;
public ChromeAuthTest() {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
}
private void initDriver() {
ChromeOptions cOptions = new ChromeOptions();
cOptions.addExtensions(new File("MultiPass-for-HTTP-basic-authentication_v.crx"));
driver = new ChromeDriver(cOptions);
configureAuth(
"https://the-internet.herokuapp.com/basic_auth",
"admin",
"admin");
}
private void configureAuth(String url, String username, String password) {
driver.get("chrome-extension://enhldmjbphoeibbpdhmjkchohnidgnah/options.html");
driver.findElement(By.id("url")).sendKeys(url);
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.className("credential-form-submit")).click();
}
public void doTest() {
initDriver();
driver.get("https://the-internet.herokuapp.com/basic_auth");
System.out.println(driver.getTitle());
driver.quit();
}
public static void main(String[] args) {
new ChromeAuthTest().doTest();
}
}
注:这是从这个Answer拍摄。
希望这可以帮助!