c# - Selenium Chrome 60 Headless 处理基于 HTTPS 的基本身份验证 SAML 对话框

标签 c# selenium-webdriver selenium-chromedriver basic-authentication

Chrome 59 removed support for https://user:password@example.com URLs .

我有一个 C# selenium 测试需要使用 Chrome Version 60 on Windows in 'headless' mode

ChromeOptions options = new ChromeOptions();
options.AddArgument("headless");
driver = new ChromeDriver(chrome, options);

这是我试图在 Windows 上处理的需要 SAML 身份验证的对话框: Basic Auth Dialog

基于此处给出的答案:How to handle authentication popup with Selenium WebDriver using Java ) 我可以看到在 FireFox 中处理此问题的几种解决方法,但在 headless 模式下对于 Chrome 60 没有任何解决方法。

在访问被测 URL(无凭据)之前,我已经尝试使用以下代码访问带凭据的 URL,但它似乎有一个 bug使用 Chrome 60。

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,但在 Headless 模式下NOT 不适用于 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 年有更好的解决方案,并且有一种方法可以在 headless 模式下与 Chrome 60 一起使用,有什么指示吗?

明确一点:尝试使用 embedded credentials将无法使用 chrome v59+,因为子资源请求将被阻止。

最佳答案

我知道这已经快一年了,但这就是在类似情况下最终为我工作的原因。确实,身份验证弹出窗口已更改,ChromeDriver 似乎不再支持它或 http(s)://user:password@url.com 方案,但工作 -我发现了here似乎可以解决问题。它最初是为验证代理系统而编写的,但可以修改为与任何验证系统一起使用。

基本上,您需要制作一个 chrome 扩展来处理在页面上输入登录详细信息。可以使用 ChromeOptions.AddExtension(string FilePath) 添加 chrome 扩展,扩展只是一个带有 manifest.json 和任何代码文件的 zip 文件来完成工作。这是您需要的文件。

list .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"
}

背景.js

function callbackFn(details) {
    return {
        authCredentials: {
            username: "YOUR_PROXY_USERNAME",
            password: "YOUR_PROXY_PASSWORD"
        }
    };
}

chrome.webRequest.onAuthRequired.addListener(
    callbackFn,
    {urls: ["YOUR_WEBSITE_ADDRESS"]},
    ['blocking']
);

关于c# - Selenium Chrome 60 Headless 处理基于 HTTPS 的基本身份验证 SAML 对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45374320/

相关文章:

C# 匿名类和列表<object>

c# - PorterDuff.Mode.Multiply 没有按预期工作?黑色背景而不是透明

java - Selenium java 。无法定位元素

python - 尝试通过 Selenium 和 Python 单击 Surveymonkey.com 中的复选框时出现 ElementClickInterceptedException

java - 为什么 Selenium 驱动程序无法识别 Facebook 登录页面的 ID 元素?

java - 如何初始化 StringoriginalHandle = driver.getWindowHandle();一次?

selenium - ERROR :ssl_client_socket_openssl. cc(1158)] ChromeDriver Chrome 浏览器和 Selenium 握手失败

c# - 反序列化时不调用构造函数

c# - 在 WPF 的 HierarchicalDataTemplate 中获取 TreeViewItem 的父级

html - 如何使用Selenium和webdriver提取部分网页源代码?