Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to delete default values in text field using selenium?

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

like image 523
Wasi Avatar asked May 29 '12 13:05

Wasi


People also ask

How do you clear the contents of a textbox in Selenium?

We can enter text on any field in Selenium. After entering the text, we may need to remove or clear the text we entered in that field. This interaction with the web elements can be achieved with the help of clear() method. Thus a clear() method is used to clear or reset an input field belonging to a form/ edit box.

How do you clear the text box in Selenium without using clear method?

First CONTROL + a, and then DELETE; otherwise, just a segment, until the first special character, of the text is deleted.

How do you overwrite the current input value in the editable field on page in Selenium?

To overwrite a value, we shall first select it with CTRL+A keys and then pass the new value. Thus, Keys. CONTROL, A and the new value are passed as parameters to the Keys.

How do I delete an element in Selenium?

We can delete an element in Selenium webdriver using Python with the help of JavaScript Executor. Selenium is not capable of modifying the structure of DOM directly. It has the feature of injecting JavaScript into the webpage and changing the DOM with the help execute_script method.


3 Answers

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

like image 128
Pavel Janicek Avatar answered Oct 22 '22 15:10

Pavel Janicek


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");
like image 29
Shek Avatar answered Oct 22 '22 17:10

Shek


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

// assuming 'selenium' is a healthy Selenium instance
selenium.type("someLocator", "");
like image 2
Petr Janeček Avatar answered Oct 22 '22 16:10

Petr Janeček