Skip to content

Instantly share code, notes, and snippets.

@SlyNet
Last active May 20, 2020 13:47
Show Gist options
  • Select an option

  • Save SlyNet/6afa61ea9e8c2b86d629a1cb8103cb59 to your computer and use it in GitHub Desktop.

Select an option

Save SlyNet/6afa61ea9e8c2b86d629a1cb8103cb59 to your computer and use it in GitHub Desktop.
Type text in input selenium chrome
public static void SendKeysGently(this IWebElement typeTo, string text, int retryCount = 10)
{
bool introduceDelay = false;
Policy.Handle<Exception>()
.WaitAndRetry(retryCount, i => TimeSpan.FromMilliseconds(500),
(exception, timeSpan, retry, context) =>
{
introduceDelay = retry > 5;
})
.Execute(() =>
{
typeTo.Clear();
if (introduceDelay)
{
for (int i = 0; i < text.Length; i++)
{
typeTo.SendKeys(text[i].ToString());
Thread.Sleep(50);
}
}
else
{
typeTo.SendKeys(text);
}
var numericString = typeTo.GetAttribute("numeric-string");
var actual = typeTo.GetProperty("value");
if (actual == text) return;
if (numericString == text) return;
throw new FailedToTypeProperValueException($"Failed to type '{text}'. " +
$"input value: '{actual}', numeric: '{numericString}'");
});
}
@SlyNet
Copy link
Copy Markdown
Author

SlyNet commented May 20, 2020

uses Polly library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment