如何使用 Selenium 删除文本字段中的默认值?
我们可以使用 Selenium webdriver 删除文本字段中的默认值。有多种方法可以做到这一点。我们可以使用 clear() 方法,它重置文本框或文本区域字段中已经存在的值。
我们可以在 sendKeys() 中使用 Keys.chord() 方法。Keys.chord() 方法有助于同时按下多个键。它接受一系列按键或字符串作为方法的参数。
要删除默认值,可以使用 Keys.CONTROL、"a" 作为参数。然后将此字符串作为参数发送到 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 DelDefaultVal{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://tutorialspoint.com/index.htm";
driver.get(url);
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
// identify element
WebElement l = driver.findElement(By.id("gsc-i-id1"));
// input text
l.sendKeys("Selenium");
// delete default value with clear()
l.clear();
driver.quit()
}
}示例
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class DelDefaultValue{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://tutorialspoint.com/index.htm";
driver.get(url);
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
// identify element
WebElement l = driver.findElement(By.id("gsc-i-id1"));
// input text
l.sendKeys("Selenium");
// sending Ctrl+a by Keys.Chord()
String s = Keys.chord(Keys.CONTROL, "a");
l.sendKeys(s);
// sending DELETE key
l.sendKeys(Keys.DELETE);
driver.quit()
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP