Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 处理Chrome上弹出的windows身份验证_Selenium_Selenium Webdriver_Webdriver_Autoit - Fatal编程技术网

Selenium 处理Chrome上弹出的windows身份验证

Selenium 处理Chrome上弹出的windows身份验证,selenium,selenium-webdriver,webdriver,autoit,Selenium,Selenium Webdriver,Webdriver,Autoit,我正在尝试使用AutoIt处理Selenium webdriver脚本的基本身份验证弹出窗口。我为Firefox和Internet Explorer编写了一个脚本,但它不适用于Chrome 当我尝试使用Chrome上的弹出式身份验证时,它显示为空。我正在使用以下AutoIt脚本: WinWaitActive("Authentication Required","","120") If WinExists("Authentication Required") Then Send("user

我正在尝试使用AutoIt处理Selenium webdriver脚本的基本身份验证弹出窗口。我为Firefox和Internet Explorer编写了一个脚本,但它不适用于Chrome

当我尝试使用Chrome上的弹出式身份验证时,它显示为空。我正在使用以下AutoIt脚本:

WinWaitActive("Authentication Required","","120")
If WinExists("Authentication Required") Then
    Send("username{TAB}")
    Send("password{Enter}")
EndIf

任何让这项工作起作用的建议都会很有帮助。我没有使用
username@password:google.com
因为重定向时会出现一些身份验证弹出窗口。

我可以让AutoIt通过文本而不是窗口标题来定位窗口。AutoIt窗口信息工具无法识别标题,但可以识别可见文本

因此,我将脚本更改为:

WinWaitActive("","Authentication Required","120")
If WinExists("","Authentication Required") Then
    Send("username{TAB}")
    Send("password{Enter}")
EndIf

它运行得很好。

我使用了您的脚本,并根据需要对其进行了一些扩展。用户和密码不是硬编码的,可以通过命令行或用户输入进行设置。脚本只需启动一次

If(Not IsArray($CmdLine) Or $CmdLine[0] < 2) Then
   $user = InputBox ("User", "Please enter your user", "")
   $pass = InputBox ("Password", "Please enter your password", "", "*M")
Else
   $user = $CmdLine[1]
   $pass = $CmdLine[2]
EndIf


While(True)
   WinWaitActive("", "Authentifizierung erforderlich","120")
   If WinExists("", "Authentifizierung erforderlich") Then
      Send($user)
      Send("{TAB}")
      Send($pass)
      Send("{Enter}")
      Sleep(1000)
   EndIf
WEnd
如果(不是IsArray($CmdLine)或$CmdLine[0]<2),则
$user=InputBox(“用户”,“请输入您的用户”,“”)
$pass=InputBox(“密码”,“请输入您的密码”、“”、“*M”)
其他的
$user=$CmdLine[1]
$pass=$CmdLine[2]
恩迪夫
While(True)
WinWaitActive(“,”Authentifizierung erforderlich“,”120”)
如果WinExists(“,”authentifizierrung erforderlich“)则
发送($user)
发送(“{TAB}”)
发送($pass)
发送(“{Enter}”)
睡眠(1000)
恩迪夫
温德

下一步是找出在chrome中设置的语言,并根据它设置窗口标题。

首先,您不需要AutoIt,只需使用windows API即可。其次,Chrome的基本身份验证对话框不是一个传统的窗口,因此您无法获得它的句柄(尝试使用Spy++)。如果在SendKeys调用之前没有将另一个窗口带到前台,那么这将起作用。您需要找到父Chrome窗口,它可能类似于“URL-GoogleChrome”,将其移动到前面,然后发送密钥。下面是一个例子:

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("]", "{]}");
}
使用系统;
使用System.Runtime.InteropServices;
使用System.Windows.Forms;
[DllImport(“User32.dll”)]
私有静态外部bool SetForegroundWindow(IntPtr点);
[DllImport(“user32.dll”)]
私有静态外部IntPtr FindWindow(字符串类名称,字符串窗口标题);
公共静态void发送基本身份验证(字符串用户名、字符串密码、字符串窗口标题)
{
var hwnd=FindWindow(null,windowTitle);

如果(hwnd.ToInt32()请尝试此操作,我的正在工作,我从nuget获得了AutoItX:

AutoItX.WinWait("title of your browser", "", 9); 
AutoItX.WinActivate("title of your browser");
AutoItX.Send("userid");
AutoItX.Send("{TAB}", 0);
AutoItX.Send("password");
AutoItX.Send("{Enter}", 0);

你能用窗口信息工具更新一下吗?我想知道验证弹出窗口是否真的是页面的一部分……另外,sqa.stackexchange(软件质量保证,以前是selenium问答网站)有大量selenium用户/专家。还有一种不用AutoIT的方法,我使用的是Chrome 61.0.3163.91 64位。AutoIT v3窗口信息工具显示“Chrome旧版窗口”作为Chrome主窗口和基本authn对话框的可见文本和隐藏文本。我仍然尝试了您的示例代码,但找不到该窗口。您如何等待登录提示出现?@PanuHaaramo您可以使用一个延迟的循环重试FindWindow调用,直到返回非0结果。请检查“Autoit v3窗口信息”工具标题(以及所有其他信息)对于Chrome主窗口和基本登录对话框都是一样的。标题是URL。当前我的代码等待标题成为正确的URL,但当标题更改时,登录对话框还不存在。我可以通过睡眠来处理这个问题,但更好的解决方案是等待登录对话框可用。在IE和Firefox中,登录对话框是sepa速率窗口,但不在Chrome中。