首先你不需要AutoIt,你可以使用windows API。其次,Chrome的基本身份验证对话框不是传统的窗口,因此您无法获取它的句柄(尝试使用Spy ++)。这可以起作用的唯一原因是,如果您在SendKeys呼叫之前未将其他窗口置于前台。您需要找到父级Chrome窗口,可能类似于“URL - 谷歌浏览器”,将其移到前面,然后发送密钥。这里有一个例子:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr point);
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string className, string windowTitle);
public static void SendBasicAuthentication(string username, string password, string windowTitle)
{
var hwnd = FindWindow(null, windowTitle);
if (hwnd.ToInt32() <= 0 || !SetForegroundWindow(hwnd)) return;
SendKeys.SendWait(username.EscapeStringForSendKeys());
SendKeys.SendWait("{TAB}");
SendKeys.SendWait(password.EscapeStringForSendKeys());
SendKeys.SendWait("{ENTER}");
}
static string EscapeStringForSendKeys(this string input)
{
// https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx
// must do braces first
return input.Replace("{", "{{}")
.Replace("}", "{}}")
.Replace("^", "{^}")
.Replace("%", "{%}")
.Replace("~", "{~}")
.Replace("(", "{(}")
.Replace(")", "{)}")
.Replace("[", "{[}")
.Replace("]", "{]}");
}
希望有帮助。
您可以使用窗口信息工具进行更新吗?我想知道,如果认证弹出窗口实际上是页面的一部分...此外,在sqa.stackexchange(软件质量保证,以前是一个硒问答网站)有大量的硒用户/专家。 – Colyn1337
有没有使用AutoIT的另一种方法https://stackoverflow.com/questions/11522434/how-to-handle-login-pop-up-window-using-selenium-webdriver/30067944#30067944 –