在关闭远程桌面连接的情况下,在远程服务器上发送键盘 RETURN 键
Sending Keyboard RETURN Key on remote server with Remote Desktop Connection Closed
我有一个 WinForms WebView2 项目,它是一个简单的机器人 运行网页上的一些 JavaScript。我正在使用 JS 单击网页上的元素并下载文件。该网站提示我“您确定要离开该页面吗?”对话框,所以我想发送 RETURN 键盘按键以确认我想离开网页并下载文件。
我想 运行 使用 windows 任务计划程序自动执行此操作。只要我远程连接到我的服务器并且 window 没有最小化,这种方法就有效。然而,当我没有远程连接到我的服务器时,我的输入键 returns 和错误。
我已经用 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();
}
我启动了我的机器人,然后退出了我的远程会话。我在return的时候,程序执行到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();
}
按照相同的过程,我启动了我的机器人并退出了我的远程会话。当我 return 时,我遇到了这个错误:
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# 兼容的方式来发送击键?或者有没有办法防止对话框弹出?谢谢!
你做错了:-)
您应该尝试查看:CoreWebView2.ScriptDialogOpening Event。
scriptdialogopening
的事件参数带有 Accept()
方法,可在对话框中单击 Ok
。
这是事件参数 link:CoreWebView2ScriptDialogOpeningEventArgs
现在您不必再为 SendKeys
操心了。
我有一个 WinForms WebView2 项目,它是一个简单的机器人 运行网页上的一些 JavaScript。我正在使用 JS 单击网页上的元素并下载文件。该网站提示我“您确定要离开该页面吗?”对话框,所以我想发送 RETURN 键盘按键以确认我想离开网页并下载文件。
我想 运行 使用 windows 任务计划程序自动执行此操作。只要我远程连接到我的服务器并且 window 没有最小化,这种方法就有效。然而,当我没有远程连接到我的服务器时,我的输入键 returns 和错误。
我已经用 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();
}
我启动了我的机器人,然后退出了我的远程会话。我在return的时候,程序执行到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();
}
按照相同的过程,我启动了我的机器人并退出了我的远程会话。当我 return 时,我遇到了这个错误:
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# 兼容的方式来发送击键?或者有没有办法防止对话框弹出?谢谢!
你做错了:-)
您应该尝试查看:CoreWebView2.ScriptDialogOpening Event。
scriptdialogopening
的事件参数带有 Accept()
方法,可在对话框中单击 Ok
。
这是事件参数 link:CoreWebView2ScriptDialogOpeningEventArgs
现在您不必再为 SendKeys
操心了。