我想删除文本框的默认值以输入新值,但我不知道如何操作。
我正在考虑使用 CTRL + a 然后删除,但我不知道该怎么做。
我甚至使用了WebDriver的命令driver.findElement("locator").clear();
。
答案 0 :(得分:40)
代码有用吗?因为你写的代码应该做的事情:
driver.findElement("locator").clear();
如果没有用,那就试试这个:
WebElement toClear = driver.findElement("locator");
toClear.sendKeys(Keys.CONTROL + "a");
toClear.sendKeys(Keys.DELETE);
也许你必须将Keys.CONTROL + "a"
转换为CharSequence,但第一种方法应该是神奇的
答案 1 :(得分:3)
对于页面对象模型 -
@FindBy(xpath="//foo")
public WebElement textBox;
现在在您的函数中
public void clearExistingText(String newText){
textBox.clear();
textBox.sendKeys(newText);
}
用于一般的硒结构 -
driver.findElement(By.xpath("//yourxpath")).clear();
driver.findElement(By.xpath("//yourxpath")).sendKeys("newText");
答案 2 :(得分:2)
如果您正在寻找Selenium RC的解决方案,您可以使用
// assuming 'selenium' is a healthy Selenium instance
selenium.type("someLocator", "");
答案 3 :(得分:2)
您可以使用以下代码。它选择字段中的预先存在的值,并用新值覆盖它。
driver.findElement(By.xpath("*enter your xpath here*")).sendKeys(Keys.chord(Keys.CONTROL, "a"),*enter the new value here*);
答案 4 :(得分:1)
以下函数将逐个删除输入字符,直到输入字段为空,使用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);
});
});
}
答案 5 :(得分:1)
这对我有用:
driver.findElement(yourElement).clear();
driver.findElement(yourelement).sendKeys("");
答案 6 :(得分:0)
.clear()可用于清除文本
(locator).clear();
使用clear和locator删除该精确定位器中的所有值。
答案 7 :(得分:0)
driver.findElement(locator).clear()
- 此命令适用于所有情况
答案 8 :(得分:0)
# A tibble: 14 x 4
# Groups: user_id [4]
user_id flag order has_changed
<dbl> <chr> <dbl> <chr>
1 1 aaa 1 yes
2 1 aaa 2 yes
3 1 aaa 3 yes
4 1 bbb 4 yes
5 2 bbb 1 no
6 2 bbb 2 no
7 3 aaa 1 yes
8 3 aaa 2 yes
9 3 bbb 3 yes
10 4 ccc 1 no
11 4 aaa 2 no
12 4 aaa 3 no
13 4 aaa 4 no
14 4 aaa 5 no
对我不起作用。但是确实如此:
clear()
答案 9 :(得分:0)
在软件测试服务中,这可以通过多种方式实现,上面显示的一些选项如下所示。
driver.executeScript("document.getElementByXpath('element').setAttribute('value', 'abc')");
使用动作类 动作动作=新动作(驱动程序);
actions.click(driver.findElement(element) .keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).sendKeys(Keys.BACK_SPACE).build().perform()) ;
答案 10 :(得分: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()