31
votes

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.

12
Are you sure that you are focused on the correct frame? - Brian
what errors are you getting? can you provide the stack trace? - Amith
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 commands - user3162976

12 Answers

34
votes

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

myInputElm.click();
myInputElm.clear();
myInputElm.sendKeys('myString');
3
votes

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.

2
votes

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.
2
votes

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");
1
votes

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.

0
votes

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);
0
votes

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");
0
votes

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);
0
votes

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.

0
votes

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;
        }
    });
}
-1
votes

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.

-2
votes

Generally I keep a temporary variable. This should work.

var name = element(by.id('name'));
name.clear();
name.sendKeys('anything');