28

I want to delete a default value of a textbox to enter the new value, but I am not getting how to do that.

I was thinking to use CTRL+a and then Delete but I'm not sure how to do this.

I even used WebDriver's command driver.findElement("locator").clear();.

2
  • What happened when you tried it? Did you get an Exception, or nothing happened, or...? Commented May 29, 2012 at 14:46
  • Also, can you write to the element? If you tried element.sendKeys("bla"), would the text append itself to the existing one? ... If nothing helps, please show us the HTML code of the element. Something there could be done in a unusual way that breaks things... Commented May 29, 2012 at 14:51

11 Answers 11

53

And was the code helpful? Because the code you are writing should do the thing:

driver.findElement("locator").clear();

If it does not help, then try this:

WebElement toClear = driver.findElement("locator");
toClear.sendKeys(Keys.CONTROL + "a");
toClear.sendKeys(Keys.DELETE);

maybe you will have to do some convert of the Keys.CONTROL + "a" to CharSequence, but the first approach should do the magic

Sign up to request clarification or add additional context in comments.

4 Comments

@user1374181 I don't understand your question - what does it mean "simple JUnit"? These are Selenium WebDriver commands taking advantage of the Keys enum in Selenium.
@Slanec : I mean if not possible with selenium2 can it be used with junit using RC.
so is it possible with selenium1?
It is causing error " error: invalid element state: Element is not currently interactable and may not be manipulated " when I am using ` Global.driver.FindElement(rangeFrom).Clear(); `. Unfortunately this works when debug code it works. May be it needs additional wait times. @PavelJanicek
4

For page object model -

 @FindBy(xpath="//foo")
   public WebElement textBox;

now in your function

 public void clearExistingText(String newText){
    textBox.clear();
    textBox.sendKeys(newText);
  }

for general selenium architecture -

driver.findElement(By.xpath("//yourxpath")).clear();
driver.findElement(By.xpath("//yourxpath")).sendKeys("newText");

1 Comment

This is not working, it delete the text at the beginning of the Edit text value
2

If you're looking for a solution from Selenium RC, you can use simply

// assuming 'selenium' is a healthy Selenium instance
selenium.type("someLocator", "");

5 Comments

What did it do? What happens when you try to type some text? Is there any exception?
it neither throws an exception nor do it removes the values, i even tried to overwrite the value with some other value....
Show us the code of the HTML element. All these things you mention should work.
@user1374181 Now I'm seriously baffled. Why did I get the accept? Right, it is the correct answer, but it wasn't working in your case, was it?
ur command worked but was issue with the page, it was acting like a pop up when it wasn't,i changed selectWindow to selectPop and my problem was solved using ur suggestions. so thank u very much.
2

You can use the code below. It selects the pre-existing value in the field and overwrites it with the new value.

driver.findElement(By.xpath("*enter your xpath here*")).sendKeys(Keys.chord(Keys.CONTROL, "a"),*enter the new value here*);

Comments

2

driver.findElement(locator).clear() - This command will work in all cases

1 Comment

Doesn't work for me in Java, v3.14. It does nothing.
2

clear() didn't work for me. But this did:

input.sendKeys(Keys.CONTROL, Keys.chord("a")); //select all text in textbox
input.sendKeys(Keys.BACK_SPACE); //delete it
input.sendKeys("new text"); //enter new text

Comments

1

The following function will delete the input character one by one till the input field is empty using PromiseWhile

driver.clearKeys = function(element, value){
  return element.getAttribute('value').then(function(val) {
    if (val.length > 0) {
      return new Promise(function(resolve, reject) {
        var len;
        len = val.length;
        return promiseWhile(function() { 
          return 0 < len;
        }, function() {
          return new Promise(function(resolve, reject) {
            len--;
            return element.sendKeys(webdriver.Key.BACK_SPACE).then(function()              {
              return resolve(true);
            });
          });
        }).then(function() {
          return resolve(true);
        });
      });
    }

Comments

1

This worked for me:

driver.findElement(yourElement).clear();
driver.findElement(yourelement).sendKeys("");

1 Comment

In Java, I'm seeing that clear() does nothing
0

.clear() can be used to clear the text

  (locator).clear();

using clear with the locator deletes all the value in that exact locator.

Comments

0

In software testing services this can be achieved by many ways some of the options are displayed above remaining are as follow.

  • Using java script

driver.executeScript("document.getElementByXpath('element').setAttribute('value', 'abc')");

Using action class Actions actions = new Actions(driver);

actions.click(driver.findElement(element) .keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).sendKeys(Keys.BACK_SPACE).build().perform());

Comments

0
actions = ActionChains(driver)
ak = driver.find_element_by_id('blogname')
actions.move_to_element(ak)
actions.click()
actions.key_down(Keys.CONTROL).send_keys('a').key_down(Keys.DELETE)
actions.perform()

1 Comment

Welcome to Stack Overflow, and thank you for contributing an answer. Would you kindly edit your answer to to include an explanation of your code? That will help future readers better understand what is going on, and especially those members of the community who are new to the language and struggling to understand the concepts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.