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();
.
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.
First CONTROL + a, and then DELETE; otherwise, just a segment, until the first special character, of the text is deleted.
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.
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.
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
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");
If you're looking for a solution from Selenium RC, you can use simply
// assuming 'selenium' is a healthy Selenium instance
selenium.type("someLocator", "");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With