'sendKeys' are not working in Selenium WebDriver
Asked Answered
G

13

38

I am not able to put any value in my application using WebDriver. My application is using frames.

I am able to clear the value of my textbox with driver.findElement(By.name("name")).clear();, but I'm unable to put any value using driver.findElement(By.name("name")).sendKeys("manish");. The click command works for another button on the same page.

Greatniece answered 5/1, 2014 at 17:2 Comment(4)
Are you sure that you are focused on the correct frame?Subhuman
what errors are you getting? can you provide the stack trace?Mcbee
Yes i am focusing on correct frame, else i believe following command should not works "driver.findElement(By.name("name")).clear()", in debugger mode before executing following command i put some value which clear by using this, so it should focus on correct frame. And also it is surprising that i am not getting any sort of error on executing following commandsGreatniece
I saw similar kind of problem, like sendKeys data visually showing in the input box but on form submit it not worked in Safari as I saw in the form payload the data was not passed. I got a solution, after set the sendKeys, I also trigger a change method on the input box and it resolves my issue.Indeterminacy
P
40

I also had that problem, but then I made it work by:

myInputElm.click();
myInputElm.clear();
myInputElm.sendKeys('myString');
Presentation answered 12/9, 2014 at 8:52 Comment(2)
Yes. I have a theory as to why that is. I think those of us affected by this may be because the element we are trying to send keys to is in a frame and maybe this helps selenium get to it? THANK YOU!!Hwu
Did you resolve the issue by adding .click() method before clear()???Prepossess
Q
7

Before sendkeys(), use the click() method (i.e., in your case: clear(), click(), and sendKeys()):

driver.findElement(By.name("name")).clear();
driver.findElement(By.name("name")).click(); // Keep this click statement even if you are using click before clear.
driver.findElement(By.name("name")).sendKeys("manish");
Quito answered 14/8, 2018 at 20:58 Comment(0)
M
3

Try clicking on the textbox before you send keys.

It may be that you need to trigger an event on the field before input and hopefully the click will do it.

Meddle answered 5/1, 2014 at 17:38 Comment(1)
How can we click using JavaScript please sugggestGreatniece
C
3

Clicking the element works for me too, however, another solution I found was to enter the value using JavaScript, which doesn't require the element to have focus:

var _element= driver.FindElement(By.Id("e123"));
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("arguments[0].setAttribute('value', 'textBoxValue')", _element);
Chesterfield answered 9/5, 2016 at 8:44 Comment(1)
Thank you for mentioning this, I'm going to try it. In my case, sendkeys is working partially - it very slowly sends some of the characters and then gives up before finishing! So it's not a focus problem. It's some other very strange problem.Vocation
A
2

I experienced the same issue and was able to collect the following solution for this:

  1. Make sure element is in focus → try to click it first and enter a string.
  2. If there is some animation for this input box, apply some wait, not static. you may wait for an element which comes after the animation. (My case)
  3. You can try it out using Actions class.
Anesthesiologist answered 5/8, 2018 at 13:6 Comment(1)
+1 for the animation hint, that was my problem. I have a modal dialog in Angular 9 and selenium starts writing in the inputs before the open-animation is done. It appears as if the ending of the animation interrrupts sendKeys. In my case I got some of the letters sent, but not all.Rothberg
S
1

Use JavaScript to click in the field and then use sendkeys() to enter values.

I had a similar problem in the past with frames. JavaScript is the best way.

Santinasantini answered 5/1, 2014 at 20:46 Comment(2)
How could i click using JavaScript. I would be very thankful if you could provide me the codeGreatniece
I would say that Javascript should never "be the best way". It should only be a last resort. Automating with javascript execution is not actually proving the application is working via user interaction but that it works if you fire javascript directly. I would always try lots of other ideas via the Selenium API such using Actions to move the mouse over the field etc. (ie. trigger the javascript via interaction, rather than execution.Meddle
Z
1

In my case, I had some actions.keyDowns(Keys.CONTOL).XXXX;

But I forgot to add the keyUp for that button and that prevented from sending keys and resulted in weird behaviors

Adding X.keyUp() after the x.keyDown() fixed the issue
Zucker answered 10/1, 2023 at 14:25 Comment(0)
C
0

First pass the driver control to the frame using:

  driver.switchTo().frame("pass id/name/index/webelement");

After that, perform the operation which you want to do on the webelement present inside the frame:

 driver.findElement(By.name("name")).sendKeys("manish");
Cchaddie answered 3/2, 2014 at 10:15 Comment(0)
U
0

I have gone with the same problem where copy-paste is also not working for that text box.

The below code is working fine for me:

WebDriver driver = new FirefoxDriver();
String mobNo = "99xxxxxxxx";
WebElement mobileElementIrs = 
driver.findElement(By.id("mobileNoPrimary"));
mobileElementIrs.click();
mobileElementIrs.clear();
mobileElementIrs.sendKeys(mobNo);
Unlace answered 23/1, 2016 at 5:11 Comment(0)
C
0

I had a similar problem too, when I used

getDriver().findElement(By.id(idValue)).clear();
getDriver().findElement(By.id(idValue)).sendKeys(text);

The value in "text" was not completely written into the input. Imagine that "Patrick" sometimes write "P" another "Pat",...so the test failed

The fix is a workaround and uses JavaScript:

((JavascriptExecutor)getDriver()).executeScript("$('#" + idValue + "').val('" + value + "');");

Now it is fine.

Instead of

driver.findElement(By.id("idValue")).sendKeys("text");

use,

((JavascriptExecutor)getDriver()).executeScript("$('#" + "idValue" + "').val('" + "text" + "');");

This worked for me.

Codfish answered 7/4, 2017 at 9:46 Comment(0)
M
0

I had a similar problem recently and tried some of the suggestions above, but nothing worked. In the end it fell back on a brute-force retry which retries if the input box wasn't set to what was expected.

I wanted to avoid thread.sleep for obvious reasons and saw different examples of it failing that looked like some kind of race or timing condition.

public void TypeText(string id, string text)
{
    const int numberOfRetries = 5;
    for (var i = 1; i < numberOfRetries; i++)
    {
        try
        {
            if (TryTypeText())
                return;
        }
        catch (Exception)
        {
            if (i == numberOfRetries)
                throw;
        }
    }

    bool TryTypeText()
    {
        var element = _webDriver.FindElement(By.Id(id));
        element.Click();
        element.Clear();
        element.SendKeys(text);
        if (element.TagName.ToLower() == "input"
            && !DoesElementContainValue(element, text, TimeSpan.FromMilliseconds(1000)))
        {
            throw new ApplicationException($"Unable to set the type the text '{text}' into element with id {id}. Value is now '{element.GetAttribute("value")}'");
        }
        return true;
    }
}

private bool DoesElementContainValue(IWebElement webElement, string expected, TimeSpan timeout)
{
    var wait = new WebDriverWait(_webDriver, timeout);
    return wait.Until(driver =>
    {
        try
        {
            var attribute = webElement.GetAttribute("value");
            return attribute != null && attribute.Contains(expected);
        }
        catch (StaleElementReferenceException)
        {
            return false;
        }
    });
}
Misconception answered 16/6, 2020 at 13:42 Comment(1)
What is the dollar sign in front of "Unable to for (not a rhetorical question)?Jackhammer
E
-1

Try using JavaScript to sendkeys().

WebElement element = driver.findElement(By.name("name"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

More information on JavaScript Executor can be found at JavascriptExecutor - Selenium.

Epicrisis answered 6/1, 2014 at 6:45 Comment(0)
G
-2

Generally I keep a temporary variable. This should work.

var name = element(by.id('name'));
name.clear();
name.sendKeys('anything');
Gitagitel answered 5/3, 2015 at 9:40 Comment(1)
What do you mean by "keep a temporary variable"? What is the purpose? And how is it supposed to be used and for what?Jackhammer

© 2022 - 2024 — McMap. All rights reserved.