如何通过 Selenium2 发送键盘快捷键 ALT SHIFT z(热键)?
我们可以通过 Selenium webdriver 发送键盘快捷键 ALT SHIFT z(热键)。这可以通过 Keys 类完成。我们将使用 Keys.chord 方法并传递 Keys.ALT、Keys.SHIFT 和 z 作为该方法的参数。
从 Keys.chord 方法中获得的整个值都作为字符串形式获得。然后将它作为参数发送到 sendKeys 方法。
语法
String s = Keys.chord(Keys.ALT, Keys.SHIFT,"z");
driver.findElement(By.tagName("html")).sendKeys(s);示例
带 Keys.chord 方法的代码实现。
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;
public class HtKeys{
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");
// sending ALT,SHIFT and z by the Keys.Chord
String s = Keys.chord(Keys.ALT, Keys.SHIFT,"z");
driver.findElement(By.tagName("html")).sendKeys(s);
driver.quit();
}
}还可以借助 Actions 类来完成此操作。我们必须创建 Actions 类的对象,并在该对象上应用 keyUp 和 keyDown 方法,并将 Keys.ALT、Keys.SHIFT 和 z 传递给这些方法。
语法
Actions a = new Actions(driver);
a.keyDown(Keys.ALT).keyDown(Keys.SHIFT).
sendKeys("z").keyUp(Keys.ALT).keyUp(Keys.SHIFT).build().perform();示例
带 Actions 的代码实现。
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;
import org.openqa.selenium.Keys;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
public class HotKeyAction{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://tutorialspoint.com/index.htm");
// Actions class with keyUp and keyDown methods
Actions a = new Actions(driver);
a.keyDown(Keys.ALT).keyDown(Keys.SHIFT).
sendKeys("z").keyUp(Keys.ALT).keyUp(Keys.SHIFT).build().perform();
driver.quit();
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP