Last active
May 20, 2020 13:47
-
-
Save SlyNet/6afa61ea9e8c2b86d629a1cb8103cb59 to your computer and use it in GitHub Desktop.
Type text in input selenium chrome
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}'"); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
uses Polly library