javaselenium-webdriversendkeys

Selenium WebDriver: I want to overwrite value in field instead of appending to it with sendKeys using Java


In WebDriver, if I use sendKeys it will append my string to the value that already exists in the field. I can't clear it by using clear() method because the second I do that, the webpage will throw an error saying that it has to be between 10 and 100. So I can't clear it or an error will be thrown before I can put in the new value using sendKeys, and if I sendKeys it just appends it to the value already there.

Is there anything in WebDriver that lets you overwrite the value in the field?


Solution

  • I think you can try to firstly select all the text in the field and then send the new sequence:

    from selenium.webdriver.common.keys import Keys
    element.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55");