How can I enter an email address into text input field in Edge using Selenium WebDriver? How can I enter an email address into text input field in Edge using Selenium WebDriver? selenium selenium

How can I enter an email address into text input field in Edge using Selenium WebDriver?


As a workaround, you can set the input value directly via ExecuteScript():

IWebElement email = driver.FindElement(By.Id("email"));IJavaScriptExecutor js = driver as IJavaScriptExecutor;string script = "arguments[0].setAttribute('value', 'arguments[1]');";js.ExecuteScript(script, email, "dummy@user.de");

Or, what you can do is to create a fake input element with a predefined value equal to the email address. Select the text in this input, copy and paste into the target input.

Not pretty, but should only serve as a workaround:

// create elementIJavaScriptExecutor js = driver as IJavaScriptExecutor;string script = @"    var el = document.createElement('input');    el.type = 'text';    el.value = 'arguments[0]';    el.id = 'mycustominput';    document.body.appendChild(el);";js.ExecuteScript(script, "dummy@user.de");// locate the input, select and copyIWebElement myCustomInput = driver.FindElement(By.Id("mycustominput"));el.SendKeys(Keys.Control + "a");  // selectel.SendKeys(Keys.Control + "c");  // copy// locate the target input and pasteIWebElement email = driver.FindElement(By.Id("email"));email.SendKeys(Keys.Control + "v");  // paste


It wasn't as easy as I thought after all. Issues with alecxe's answer:

  1. arguments[0].setAttribute('value', '...'); works only the first time you call it. After calling element.Clear();, it doesn't work any more. Workaround: arguments[0].value='...';
  2. The site doesn't react on the JavaScript call like it would on element.SendKeys();, e.g. change event is not invoked. Workaround: Send the first part of the string up to the last "forbidden" character via JavaScript, the rest via WebElement.SendKeys (in this particular order, bc if you do another JavaScript call to the same field after SendKeys(), there will occur no change event either).

I also realized that there are more "forbidden" characters in Edge, e.g. accented or Eastern European ones (I'm Central European). The problem with 2. is that the last character might be a forbidden character. In this case, I append a whitespace. Which of course affects the test case behavior, but I haven't had any other idea.

Full C# code:

public static void SendKeys(this IWebElement element, TestTarget target, string text){    if (target.IsEdge)    {        int index = text.LastIndexOfAny(new[] { '@', 'Ł', 'ó', 'ź' }) + 1;        if (index > 0)        {            ((IJavaScriptExecutor) target.Driver).ExecuteScript(                "arguments[0].value='" + text.Substring(0, index) + "';", element);            text = index == text.Length ? Keys.Space : text.Substring(index);        }    }    element.SendKeys(text);}


This problem used to occur in old browsers. Apparently it returned in Edge.

You can try sending the string in pieces

IWebElement email = driver.FindElement(By.Id("email"));email.SendKeys("dummy");email.SendKeys("@");email.SendKeys("user.de");

Or try using @ ASCII code

driver.FindElement(By.Id("email")).SendKeys("dummy" + (char)64 + "user.de");