Created
May 20, 2020 13:42
-
-
Save SlyNet/1b6b8197683584219e353552583df998 to your computer and use it in GitHub Desktop.
selenium clear input for sure
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 ClearForChrome(this IWebElement webElement) | |
{ | |
webElement.Click(); | |
if (!string.IsNullOrEmpty(webElement.GetAttribute("value"))) | |
{ | |
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | |
{ | |
webElement.SendKeys(Keys.Control + 'a'); | |
} | |
else if(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | |
{ | |
RemoteWebElement remoteWebElement = (RemoteWebElement)webElement; | |
IJavaScriptExecutor javaScriptExecutor = (IJavaScriptExecutor)remoteWebElement.WrappedDriver; | |
// cant use ctrl + a on mac https://bugs.chromium.org/p/chromedriver/issues/detail?id=30#c13 | |
javaScriptExecutor.ExecuteScript("arguments[0].select();", webElement); | |
} | |
webElement.SendKeys(Keys.Delete); | |
} | |
if (!string.IsNullOrEmpty(webElement.GetAttribute("value"))) | |
{ | |
for (int i = 0; i < 2; i++) | |
{ | |
var existingValue = webElement.GetAttribute("value"); | |
webElement.SendKeys(Keys.End); | |
for (int c = 0; c < existingValue.Length; c++) | |
{ | |
webElement.SendKeys(Keys.Backspace); | |
webElement.SendKeys(Keys.Delete); | |
Thread.Sleep(20); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment