如何使用 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.tw/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.tw/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(); } }
廣告