c# - 我在 C# 中使用 selenium webdriver,我无法在证书弹出窗口中单击确定
问题描述
我正在尝试使用 C# 代码选择弹出的证书,但我无法选择弹出窗口并单击确定。
我需要选择证书并单击确定。
有人可以帮忙吗?
这是我正在使用的代码:
//driver.SwitchTo().Alert().Accept();
//IAlert popup = driver.SwitchTo().Alert();
//popup.Accept();
解决方案
你没有提到你使用的是什么浏览器,但这是我为 IE 做的。我实际上并不能直接使用 Selenium,但我使用 Windows 自动化系统让它工作,该系统通常由辅助软件使用,如屏幕阅读器。
此代码是特定于 IE 的,如果您使用不同的浏览器,则必须对其进行调整。
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Automation;
namespace Application.Automation.WindowsDesktop.Interactions
{
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
public static class DesktopInteractions
{
private static Int32 WM_KEYDOWN = 0x100;
private static Int32 WM_KEYUP = 0x101;
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, int Msg, System.Windows.Forms.Keys wParam, int lParam);
/// <summary>
/// This method attempts to interact with the IE Security Certificate Window
/// <exception> Throws InvalidOperationException if it can not locate the window or the submit control </exception>
/// </summary>
public static void HandleWindowsSecurityWindow(string certificateName)
{
AutomationElement securityCertificateWindow = null;
int waitSeconds = 1;
int currentAttempt = 0;
int maximumAttempts = 300;
//Find Security Window
AutomationElement certificateWindow = null;
while (currentAttempt <= maximumAttempts && certificateWindow == null)
{
Debug.WriteLine("Searching for security window");
var ieWindows = AutomationElement.RootElement
.FindAll(TreeScope.Children,
new PropertyCondition(AutomationElement.ControlTypeProperty,
ControlType.Pane))
.Cast<AutomationElement>()
.Where(window => window.Current.Name.Contains("Internet Explorer"));
Debug.WriteLine($"Found {ieWindows.Count()} IE windows");
foreach (var window in ieWindows)
{
Debug.WriteLine($"Looking for security window");
certificateWindow = window.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.ClassNameProperty,
"#32770")) ??
certificateWindow;
}
if (certificateWindow == null)
{
Thread.Sleep((int) TimeSpan.FromSeconds(1)
.TotalMilliseconds);
}
currentAttempt++;
}
if (certificateWindow != null)
{
Debug.WriteLine("Security window found");
if (certificateName != null)
{
// Select the certificate
Debug.WriteLine($"Searching for certificate named '{certificateName}'.");
var certificateListItem = certificateWindow.FindFirst(TreeScope.Descendants,
new AndCondition(new PropertyCondition(AutomationElement.ControlTypeProperty,
ControlType.ListItem),
new PropertyCondition(AutomationElement.NameProperty,
certificateName)));
if (certificateListItem == null)
{
throw new ArgumentException($"No certificate found with the name '{certificateName}'.");
}
Debug.WriteLine("Certificate found");
// This is the only way I could "click" on the desired certificate. Focus the item, then simulate pressing the space-bar.
certificateListItem.SetFocus();
PostMessage((IntPtr) certificateWindow.Current.NativeWindowHandle, WM_KEYDOWN, Keys.Space, 0);
PostMessage((IntPtr) certificateWindow.Current.NativeWindowHandle, WM_KEYUP, Keys.Space, 0);
}
else
{
Debug.WriteLine("No certificate specified, using default.");
}
// Click to OK button
Debug.WriteLine("Clicking OK");
var okButton = certificateWindow.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "OK"));
var click = (InvokePattern) okButton.GetCurrentPattern(InvokePattern.Pattern);
click.Invoke();
}
else
{
throw new InvalidOperationException("Unable to get a handle on the Security certification window.");
}
}
}
}
推荐阅读
- python-3.x - str、double 和 complex 的全部值范围是多少,包括它们在内存中的大小?
- codenameone - 使用 cn1-codescan lib 在扫描屏幕上按下信息按钮后,iOS 应用程序崩溃
- android - 如何获取通过window.open为Android WebView传入的窗口功能?
- javascript - 如何在不影响其子元素的情况下更改元素标签?
- amazon-web-services - 点击网站超链接时,http请求的源IP地址是什么
- javascript - 从本地存储检索后对象方法未定义?
- windows - Azure DevOps 服务器上出现“输入行太长”的批处理命令 vcvarsall.bat 错误
- python - 如何在python中只读取和写入html页面中的文本?
- javascript - 结合 IF 语句和数组平均函数
- javascript - 输入一位数字后,输入字段给我错误