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

In WebDriver, if I use sendKeys, it will add my string to the value that already exists in this field. I cannot clear this using the clear () method, because the second thing I do, the webpage will throw an error saying that it should be between 10 and 100. Therefore, I cannot clear it or the error will be thrown earlier I I can add a new value using sendKeys, and if I sendKeys, it will just add it to the existing value.

Is there anything in WebDriver that allows you to overwrite a value in a field?

+63
java selenium-webdriver sendkeys
Jul 14 '10 at 19:10
source share
12 answers

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

from selenium.webdriver.common.keys import Keys element.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55"); 
+78
Jul 15 '10 at 9:20
source share
— -

You can also clear the field before sending its keys.

 element.clear() element.sendKeys("Some text here") 
+99
Apr 21 2018-11-11T00:
source share

Well, that was a few days ago ... In my current case, the answer from ZloiAdun does not work for me, but brings me closer to my solution ...

Instead:

 element.sendKeys(Keys.chord(Keys.CONTROL, "a"), "55"); 

The following code makes me happy:

 element.sendKeys(Keys.HOME, Keys.chord(Keys.SHIFT, Keys.END), "55"); 

Hope this helps someone!

+18
Nov 07 '13 at 17:18
source share

It worked for me.

 mElement.sendKeys(Keys.HOME,Keys.chord(Keys.SHIFT,Keys.END),MY_VALUE); 
+7
May 26 '16 at 11:07
source share

In case this helps someone, the C # equivalent of ZloiAdun's answer:

 element.SendKeys(Keys.Control + "a"); element.SendKeys("55"); 
+6
Aug 13 '14 at 17:35
source share

Use this, it is a reliable solution and works well for all browsers:

 protected void clearInput(WebElement webElement) { // isIE() - just checks is it IE or not - use your own implementation if (isIE() && "file".equals(webElement.getAttribute("type"))) { // workaround // if IE and input type is file - do not try to clear it. // If you send: // - empty string - it will find file by empty path // - backspace char - it will process like a non-visible char // In both cases it will throw a bug. // // Just replace it with new value when it is need to. } else { // if you have no StringUtils in project, check value still empty yet while (!StringUtils.isEmpty(webElement.getAttribute("value"))) { // "\u0008" - is backspace char webElement.sendKeys("\u0008"); } } } 

If the input has type = "file" - do not clear it for IE. It will try to find the file empty and throw an error.

You can find more information in my blog.

+1
Feb 19 '13 at 12:32
source share

Use the following:

 driver.findElement(By.id("id")).sendKeys(Keys.chord(Keys.CONTROL, "a", Keys.DELETE), "Your Value"); 
+1
Jan 04 '18 at 11:12
source share

This solved my problem when I had to deal with an HTML page with embedded JavaScript

 WebElement empSalary = driver.findElement(By.xpath(PayComponentAmount)); Actions mouse2 = new Actions(driver); mouse2.clickAndHold(empSalary).sendKeys(Keys.chord(Keys.CONTROL, "a"), "1234").build().perform(); JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].onchange()", empSalary); 
0
Jun 05 '15 at 6:48
source share

There were problems using most of the methods mentioned, since the text field did not accept keyboard input, and the mouse solution did not look complete.

This worked to simulate a click in a field, select content, and replace it with a new one.

  Actions actionList = new Actions(driver); actionList.clickAndHold(WebElement).sendKeys(newTextFieldString). release().build().perform(); 
0
Jul 04 '16 at 10:14
source share

The original question says clear () cannot be used. This does not apply to this situation. I am adding my working example here, as this post was one of the first Google results to clear input before entering a value.

For input where there are no additional restrictions, I turn on the browser-independent Selenium method using NodeJS. This snippet is part of a shared library that I import with var test = require( 'common' ); in my test cases. This is for defining the standard module.exports node.

 when_id_exists_type : function( id, value ) { driver.wait( webdriver.until.elementLocated( webdriver.By.id( id ) ) , 3000 ) .then( function() { var el = driver.findElement( webdriver.By.id( id ) ); el.click(); el.clear(); el.sendKeys( value ); }); }, 

Find the item, click on it, clear it and send the keys.

This page provides a complete code example and an article that may help.

0
Sep 27 '17 at 23:00
source share

This is easy to do, and I succeeded:

 //Create a Javascript executor JavascriptExecutor jst= (JavascriptExecutor) driver; jst.executeScript("arguments[1].value = arguments[0]; ", 55, driver.findElement(By.id("id"))); 

55 = assigned value

0
Aug 30 '18 at 21:34
source share

What is the Ruby equivalent for using clear? as this does not work for me.

0
Dec 07 '18 at 19:05
source share



All Articles