在 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.com/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.com/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