how to delete default values in text field using selenium?
Asked Answered
S

11

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

Savior answered 29/5, 2012 at 13:5 Comment(2)
What happened when you tried it? Did you get an Exception, or nothing happened, or...?Cislunar
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...Cislunar
G
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", "");
Gur answered 29/5, 2012 at 15:25 Comment(5)
What did it do? What happens when you try to type some text? Is there any exception?Cislunar
it neither throws an exception nor do it removes the values, i even tried to overwrite the value with some other value....Savior
Show us the code of the HTML element. All these things you mention should work.Cislunar
@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?Cislunar
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.Savior
A
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

Amaral answered 29/5, 2012 at 13:18 Comment(4)
@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.Cislunar
@Slanec : I mean if not possible with selenium2 can it be used with junit using RC.Savior
so is it possible with selenium1?Savior
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. @PavelJanicekMisery
E
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");
Epigone answered 20/3, 2015 at 18:40 Comment(1)
This is not working, it delete the text at the beginning of the Edit text valueArmlet
G
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", "");
Gur answered 29/5, 2012 at 15:25 Comment(5)
What did it do? What happens when you try to type some text? Is there any exception?Cislunar
it neither throws an exception nor do it removes the values, i even tried to overwrite the value with some other value....Savior
Show us the code of the HTML element. All these things you mention should work.Cislunar
@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?Cislunar
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.Savior
L
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*);
Lindbom answered 23/6, 2016 at 13:54 Comment(0)
B
2

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

Braynard answered 15/5, 2018 at 6:46 Comment(1)
Doesn't work for me in Java, v3.14. It does nothing.Vagrom
T
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
Truong answered 13/11, 2019 at 17:9 Comment(0)
M
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);
        });
      });
    }
Micrococcus answered 27/4, 2016 at 4:3 Comment(0)
M
1

This worked for me:

driver.findElement(yourElement).clear();
driver.findElement(yourelement).sendKeys("");
Meandrous answered 26/10, 2016 at 10:6 Comment(1)
In Java, I'm seeing that clear() does nothingVagrom
S
0

.clear() can be used to clear the text

  (locator).clear();

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

Serg answered 6/9, 2017 at 9:20 Comment(0)
C
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());

Clarindaclarine answered 25/6, 2021 at 8:31 Comment(0)
L
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()
Lockman answered 1/8, 2021 at 21:54 Comment(1)
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.Cassius

© 2022 - 2024 — McMap. All rights reserved.