C# Selenium Chrome 60无头手柄通过HTTPS的基本身份验证SAML对话框
铬59 我有一个C#selenium测试需要与之配合 以下是我试图在Windows上处理的SAML身份验证必需对话框: 根据这里给出的答案:)我可以看到在FireFox中处理这个问题的几种变通方法,但在无头模式下Chrome 60没有 在访问测试中的URL(没有凭据)之前,我尝试了以下代码来访问带有凭据的URL,但是Chrome 60似乎有一个错误C# Selenium Chrome 60无头手柄通过HTTPS的基本身份验证SAML对话框,c#,selenium-webdriver,selenium-chromedriver,basic-authentication,C#,Selenium Webdriver,Selenium Chromedriver,Basic Authentication,铬59 我有一个C#selenium测试需要与之配合 以下是我试图在Windows上处理的SAML身份验证必需对话框: 根据这里给出的答案:)我可以看到在FireFox中处理这个问题的几种变通方法,但在无头模式下Chrome 60没有 在访问测试中的URL(没有凭据)之前,我尝试了以下代码来访问带有凭据的URL,但是Chrome 60似乎有一个错误 goTo("http://user:password@localhost"); // Caches auth, but page itself i
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;`
我尝试了第二种方法(),它可以在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,有什么建议吗
需要明确的是:使用chrome v59+尝试使用将不起作用,因为子资源请求将被阻止。也许您可以使用setRequestHeader方法使用XMLHttpRequest执行以前的连接,以便指定身份验证标头。在执行一个请求后,您将获得所有其他类型请求的身份验证。更大的问题可能是XSS,这取决于您的场景,您可能可以解决它。我知道这已经快一年了,但这正是在类似情况下对我起作用的原因。确实,身份验证弹出窗口已更改,ChromeDriver似乎不支持它或http(s)://用户:password@url.comscheme,但我发现的解决方案似乎奏效了。它最初是为验证代理系统而编写的,但可以修改为与任何验证系统一起使用 基本上,你需要做一个chrome扩展来处理在页面上输入登录详细信息。chrome扩展名可以通过
ChromeOptions.AddExtension(字符串文件路径)
添加,扩展名只是一个带有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"
}
您可以使用“HTTP基本身份验证的多路径”Chrome扩展来处理此问题
你可以通过GitHub来做
(或)
从Chrome网络商店下载扩展插件-
(或)
下载扩展名为crx。您可以从
一旦将扩展名下载为crx文件,将其配置到测试/源代码中就非常简单了
这一点可以使用
注意:这是从这个
希望这有帮助 你也可以检查这个,当运行selenium测试时,我也会在chrome中得到登录提示。而且这两种建议都不能在Windows上以无头模式使用Chrome 60;Alert Alert=等待.until(ExpectedConditions.alertIsPresent());警报。身份验证(新用户和密码(用户名、密码));在C#中尝试同样的方法应该有效。虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能无效。-@MarkRotterVeel-添加了要点-感谢您的评论。请务必让我知道是否还有遗漏/我的答案是否可以改进。这种方法对我有效,而其他人没有。我尝试获取登录警报并修改页面URL以包含用户名和密码,但两次尝试均失败。
{
"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"
}
function callbackFn(details) {
return {
authCredentials: {
username: "YOUR_PROXY_USERNAME",
password: "YOUR_PROXY_PASSWORD"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["YOUR_WEBSITE_ADDRESS"]},
['blocking']
);
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();
}
}