c# - 发送多个按键 WatiN

标签 c# watin sendkeys

好的,我正在 WatiN 中运行测试,并且使用 SendKeys 方法。根据MSDN网站我可以输入:

System.Windows.Forms.SendKeys.SendWait("{LEFT 2}");

这将向左输入两次。然而,我相信这不起作用,因为应用程序在每次按键之间需要时间。我为了做我需要程序做的事情,我在每次按键之间使用 Thread.Sleep 以确保它们被读取。 有更有效/正确的方法吗?这是我当前的方法代码:

System.Windows.Forms.SendKeys.SendWait("{LEFT}");
Thread.Sleep(500);
System.Windows.Forms.SendKeys.SendWait("{LEFT}");
Thread.Sleep(500);
System.Windows.Forms.SendKeys.SendWait("{ENTER}");

最佳答案

不幸的是,我不相信有这样的事情。根据MSDN SendKeys 存在计时问题:

The SendKeys class is susceptible to timing issues, which some developers have had to work around. The updated implementation is still susceptible to timing issues, but is slightly faster and may require changes to the workarounds.

The SendKeys class tries to use the previous implementation first, and if that fails, uses the new implementation.

As a result, the SendKeys class may behave differently on different operating systems. Additionally, when the SendKeys class uses the new implementation, the SendWait method will not wait for messages to be processed when they are sent to another process. If your application relies on consistent behavior regardless of the operating system, you can force the SendKeys class to use the new implementation by adding the following application setting to your app.config file.

<appSettings> 
  <add key="SendKeys" value="SendInput"/> 
</appSettings> 

To force the SendKeys class to use the previous implementation, use the value "JournalHook" instead.

您可以尝试更改实现,看看结果是否有变化。

或者,根据这个post只是使用 Thread.Sleep(0);您的输入应该起作用后。这不是最优雅的解决方案,但如果它有效,它将比 500 毫秒的暂停更快。

关于c# - 发送多个按键 WatiN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8577427/

相关文章:

watin - WATIN 中是否有在每次页面加载时执行的页面检查器?

powershell - 如何在powershell中发送ctrl + `

c# - 具有多个国家/地区代码顶级域名 (ccTLD) 的 Facebook 应用

c# - 如何合并我拆分的字符串?

C# - 使用 Javascript 从域中抓取 PDF 文件比 watin 更好的方法

Javascript:完成DOM操作时的事件

c# - 如何在 Webbrowser 控件中禁用 "Security Alert"窗口

c# - UWP CreateFileAsync 线程在 Release模式下编译并选中 Compile with .NET Native 工具链时退出

excel - 默认情况下在切片器上启用多选

c# - Sendkeys.Send() 用于右 alt 键?有什么选择吗?