27

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();.

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
Wasi
  • 743
  • 4
  • 18
  • 37
  • What happened when you tried it? Did you get an Exception, or nothing happened, or...? – Petr Janeček May 29 '12 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... – Petr Janeček May 29 '12 at 14:51

11 Answers11

51

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

Pavel Janicek
  • 14,128
  • 14
  • 53
  • 77
  • @user1374181 I don't understand your question - what does it mean "simple JUnit"? These are Selenium WebDriver commands taking advantage of the [Keys enum](http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/Keys.html) in Selenium. – Petr Janeček May 29 '12 at 14:47
  • @Slanec : I mean if not possible with selenium2 can it be used with junit using RC. – Wasi May 29 '12 at 15:02
  • so is it possible with selenium1? – Wasi May 29 '12 at 15:52
  • 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 – kjosh Jun 06 '18 at 20:52
3

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");
Shek
  • 1,543
  • 6
  • 16
  • 34
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", "");
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • What did it do? What happens when you try to type some text? Is there any exception? – Petr Janeček May 29 '12 at 16:45
  • it neither throws an exception nor do it removes the values, i even tried to overwrite the value with some other value.... – Wasi May 30 '12 at 06:12
  • Show us the code of the HTML element. All these things you mention should work. – Petr Janeček May 30 '12 at 08:10
  • @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? – Petr Janeček May 30 '12 at 09:06
  • 1
    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. – Wasi Jun 02 '12 at 10:06
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*);
2

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

Luuklag
  • 3,897
  • 11
  • 38
  • 57
dom thomas
  • 109
  • 1
  • 4
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
parsecer
  • 4,758
  • 13
  • 71
  • 140
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);
        });
      });
    }
uri wald
  • 328
  • 1
  • 5
  • 9
1

This worked for me:

driver.findElement(yourElement).clear();
driver.findElement(yourelement).sendKeys("");
galoget
  • 722
  • 9
  • 15
kljaksa
  • 11
  • 1
0

.clear() can be used to clear the text

  (locator).clear();

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

Sanju Abel
  • 111
  • 5
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());

Vishal
  • 121
  • 1
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()
  • 2
    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. – Jeremy Caney Aug 02 '21 at 02:39