webdriver clear() is not clearing the existing text
I have tried clear()
and it's not working, so tried .sendKeys(Keys.chord(Keys.CONTROL, "a", Keys.BACK_SPACE))
but my senior doesn't want me to use the keys. So unsure how can i clear the text?
HTML of the element:
<input name="Quantity_110787" type="number" min="0" ng-model="invoiceLine.quantity" class="form-control ng-pristine ng-valid ng-not-empty ng-valid-min ng-touched" ng-keypress="$ctrl.preventKey($event)" aria-invalid="false">
Code trials:
public void sendText(String text) {
for (String control : controls) {
if(getWebDriver().findElement(By.cssSelector(control)).getAttribute("type").contains("number")) {
getWebDriver().findElement(By.cssSelector(control)).sendKeys(Keys.chord(Keys.CONTROL, "a", Keys.BACK_SPACE), text);
break;
}
}
I want the text field to be cleared, so i can enter the new text. Please help.
This should work as @Dark Knight said,
driver.findElement("locator").clear();
try to click before
driver.findElement("locator").click();
driver.findElement("locator").clear();
else try using sendKeys
driver.findElement("locator").click();
driver.findElement("locator").sendKeys("");
else try JS:
WebElement wb = driver.findElement("locator");
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].value='';", wb);
Thanks,
尝试这样做。
driver.findElement("locator").clear();
The desired element is a Angular element so to invoke clear()
method you need to induce WebDriverwait for the element to be clickable and you can use either of the following solutions:
cssSelector
:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.form-control.ng-pristine.ng-valid.ng-not-empty.ng-valid-min.ng-touched[type='number'][name^='Quantity_']"))); elem.click(); elem.clear();
xpath
:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='form-control ng-pristine ng-valid ng-not-empty ng-valid-min ng-touched' and @type='number'][starts-with(@name, 'Quantity_')]"))); elem.click(); elem.clear();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.