简体   繁体   English

C# Selenium:如何在 sendkeys() 中发送每个字符;

[英]C# Selenium: How to send each character in sendkeys();

I have always found that sending keys to a web page text box has been fast.我一直发现将密钥发送到网页文本框的速度很快。 I need to do something where instead of the keys being instantly typed, I want each character to have a delay / sleep.我需要做一些事情,而不是立即键入键,我希望每个角色都有延迟/睡眠。 Example:例子:

  1. User enters “Apple” in the program.用户在程序中输入“Apple”。 We take that as the string “string answer = Console.ReadLine();”.我们将其视为字符串“string answer = Console.ReadLine();”。
  2. We then send the keys to the web page text box by delaying each character eg A sleep p sleep p sleep l sleep e.然后我们通过延迟每个字符将密钥发送到网页文本框,例如 A sleep p sleep p sleep l sleep e。

That is what I currently need help with.这就是我目前需要帮助的。

You can use Thread.Sleep after each send character.and you can change number 10 and use each number .您可以在每个发送字符后使用Thread.Sleep您可以更改数字 10 并使用每个数字。

string answer ="Apple";
char[] charArray = answer.ToCharArray();
for (int i = 0; i < charArray.Length; i++)
{
    element.SendKeys(charArray[i].ToString());
    System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10));//10 is test
}

You can loop through the string that you want to send pausing however much time you'd like (1 second in this example) like this:您可以像这样循环播放要发送的字符串,但可以根据需要暂停多长时间(在本例中为 1 秒):

foreach (char letter in answer)
{
   element.SendKeys(letter.ToString());

   System.Threading.Thread.Sleep(1000);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM