'sendKeys' are not working in Selenium WebDriver

后端 未结 12 1577
暗喜
暗喜 2020-12-05 13:36

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.findEle

相关标签:
12条回答
  • 2020-12-05 14:04

    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");
    
    0 讨论(0)
  • 2020-12-05 14:07

    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 讨论(0)
  • 2020-12-05 14:08

    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 讨论(0)
  • 2020-12-05 14:09

    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 讨论(0)
  • 2020-12-05 14:09

    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 讨论(0)
  • 2020-12-05 14:10

    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.
    0 讨论(0)
提交回复
热议问题