如何使用 Selenium 一致地删除 input 元素的默认文本?
我们可以使用 Selenium 一致地删除输入元素的默认文本。clear 方法用于删除当前存在于编辑框或文本区域中的值。
Keys.chord 方法也可与 sendKeys 搭配使用。Keys.chord 方法允许一次传递多个键。将键组或字符串作为参数传递给该方法。
首先,将 Keys.CONTROL 和 a 作为参数传递给 Keys.chord 方法。然后,将整个字符串作为参数传递给 sendKeys 方法。最后,我们必须将 Keys.DELETE 传递给 sendKeys 方法。
让我们来看一下我们将在其中删除默认文本的编辑字段。

示例
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class RemDefaultVal{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://tutorialspoint.com/index.htm");
// identify element
WebElement m = driver.findElement(By.id("gsc−i−id1"));
// delete default value with clear()
m.clear();
driver.quit();
}
}示例
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class RemDefaultValKeys{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://tutorialspoint.com/index.htm");
// identify element
WebElement m = driver.findElement(By.id("gsc−i−id1"));
// sending Ctrl+A by the Keys.Chord
String st = Keys.chord(Keys.CONTROL, "a");
l.sendKeys(st);
// DELETE key sent
l.sendKeys(Keys.DELETE);
driver.quit();
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP