【发布时间】:2021-03-01 05:52:13
【问题描述】:
我有一个 WinForms WebView2 项目,它是一个在网页上运行一些 JavaScript 的简单机器人。我正在使用 JS 单击网页上的元素并下载文件。该网站提示我“您确定要离开该页面吗?”对话框,所以我想发送 RETURN 键盘按下以确认我想离开网页并下载文件。
我想使用 Windows 任务计划程序自动运行它。只要我远程连接到我的服务器并且窗口没有最小化,这种方法就可以工作。但是,当我没有远程连接到我的服务器时,我的回车键返回并出现错误。
我已经用 SendKeys 尝试过这段代码
//Get Webview proccess
Process p = Process.GetProcessesByName("msedgewebview2").FirstOrDefault();
if (p != null)
{
IntPtr h = p.MainWindowHandle;
SetForegroundWindow(h);
printToTextbox("pressed ENTER key");
SendKeys.SendWait("{ENTER}");
await checkDownloadFinished();
}
我启动我的 Bot,然后退出我的远程会话。当我返回时,程序到达SendKeys.SendWait("{ENTER}");时会出现此错误
System.ComponentModel.Win32Exception: 'Access is denied'
我也试过 Nuget 包 InputSimulator
Process p = Process.GetProcessesByName("msedgewebview2").FirstOrDefault();
if (p != null)
{
IntPtr h = p.MainWindowHandle;
SetForegroundWindow(h);
printToTextbox("pressed ENTER key");
InputSimulator s = new InputSimulator();
s.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.RETURN);
await checkDownloadFinished();
}
按照相同的过程,我启动我的机器人并退出我的远程会话。当我返回时,我遇到了这个错误:
System.Exception: 'Some simulated input commands were not sent successfully. The most common reason
for this happening are the security features of Windows including User Interface Privacy Isolation
(UIPI). Your application can only send commands to applications of the same or lower elevation.
Similarly certain commands are restricted to Accessibility/UIAutomation applications. Refer to the
project home page and the code samples for more information.'
还有其他与 c# 兼容的方式来发送击键吗?或者有没有办法防止对话框弹出?谢谢!
【问题讨论】:
标签: c# .net sendkeys webview2 inputsimulator