在 Selenium WebDriver 中按下的鍵 (Ctrl+A)。
藉助 Selenium Webdriver 我們可以按下鍵 (CTRL+A)。有很多方法可以執行此操作。我們可以使用 Keys.chord() 方法來模擬此鍵盤操作。
Keys.chord() 方法有助於同時按下多個鍵。它接受一系列的鍵或字串作為方法的引數。要按下 CTRL+A,它需要 Keys.CONTROL 和 "a" 作為引數。
示例
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 PressCtrlA{
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.tw/index.htm";
driver.get(url);
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
// identify element
WebElement l = driver.findElement(By.id("gsc-i-id1"));
// enter text then ctrl+a with Keys.chord()
l.sendKeys("Selenium");
String s = Keys.chord(Keys.CONTROL, "a");
l.sendKeys(s);
driver.quit()
}
}我們還可以透過只使用 sendKeys() 方法來按下 CTRL+A。我們必須將 Keys.CONTROL 與字串 A 一起傳遞,其中使用 + 拼接,作為方法的引數。
示例
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 SendCtrlA{
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.tw/index.htm";
driver.get(url);
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
// identify element
WebElement l = driver.findElement(By.id("gsc-i-id1"));
// enter text then ctrl+a
l.sendKeys("Selenium");
l.sendKeys(Keys.CONTROL+"A");
driver.quit()
}
}輸出

廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP