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();
.
This question is related to
selenium
automation
selenium-rc
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
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*);
.clear() can be used to clear the text
(locator).clear();
using clear with the locator deletes all the value in that exact locator.
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");
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);
});
});
}
This worked for me:
driver.findElement(yourElement).clear();
driver.findElement(yourelement).sendKeys("");
driver.findElement(locator).clear()
- This command will work in all cases
Source: Stackoverflow.com