如何使用 Selenium C# 在 chrome 中填充需要身份验证的弹出窗口

How to fill Authentication Required popup in chrome with Selenium C#

我正在尝试使用 Selenium 打开一个 Intranet 网站,该网站重定向到另一个 link 进行登录,并在有效登录时返回原始 URL。例如 - 当我启动 webdriver 并导航到原始站点 URL https://DemoOriginalWebsite.Com 时,浏览器被重定向到 https://Validateyourselfbeforeaccessing.com:9030 并在弹出窗口 window 下方显示以输入用户 ID 和密码。

我尝试如下传递凭据但没有成功。

尝试 1 : http://username:pswd@DemoOriginalWebsite.Com

尝试 2 : https://username:pswdValidateyourselfbeforeaccessing.com:9030

认证URL无法直接访问

我的实际代码:

IWebDriver chromeDriver;
ChromeOptions options = new ChromeOptions();
options.AddArgument("disable-infobars");
options.AddUserProfilePreference("credentials_enable_service", false);        
options.AddUserProfilePreference("profile.password_manager_enabled", false);
chromeDriver = new ChromeDriver(options);
chromeDriver.Manage().Window.Maximize();     chromeDriver.Navigate().GoToUrl(@"http://username:pswd@DemoOriginalWebsite.Com");

有什么建议吗

您将不得不使用 AutoIT。安装 AutoIT,在 AutoIT 中编写脚本并导出为 .exe 文件。这个 .exe 你必须调用你的 selenium

WinWait("Untitled - Google Chrome", "" , 10)) //This will wait for 10 seconds for window with the specified title
WinActivate("Untitled - Google Chrome"); // This will send the focus of the cursor to the window with the specified title
Send("username");

//1st argument : moves the cursor's focus from Username textbox to Password text box.  
//2nd argument : false,  over here tell that it is not a text but raw key
Send("{TAB}", false); 
Send("password");
Send("{Enter}", false);// this will mimic the action of pressing enter button.

我是这样想的。

1 - 向项目添加了 AutoIt NuGet 包。

2 - 如下使用:

IWebDriver driverIE = new InternetExplorerDriver();
driverIE.Manage().Window.Maximize();
driverIE.Navigate().GoToUrl(@"https://DemoOriginalWebsite.Com");
AutoItX.ControlFocus("Windows Security", "", "Edit1");
AutoItX.ControlSetText("Windows Security", "", "Edit1","userid");
AutoItX.ControlFocus("Windows Security", "", "Edit2");
AutoItX.ControlSetText("Windows Security", "", "Edit2", "password");
AutoItX.ControlClick("Windows Security", "", "Button2");
//Do your work.
driverIE.Dispose();

我遵循的教程。 Tutorial 1 and Tutorial 2