Selenium 简明教程

Selenium WebDriver - Keyboard Events

Selenium Webdriver 可用于执行键盘事件操作,如按键抬起和按下、在中间输入多个字符、其他操作,以及使用 Actions 类复制和粘贴操作。方法 keyUp()、keyDown() 和 sendKeys() 用于执行这些操作。

Basic Methods of Keyboard Events in Actions Class

Actions 类提供了执行键盘事件的多种方法。若要获取有关这些方法的更多信息,请参阅以下链接−

  1. keyDown(CharSequence key) − 此方法用于执行修饰符键按下,作为参数传递。

  2. keyDown(WebElement e, CharSequence key) − 此方法用于在聚焦元素后执行修饰符键按下。webElement e 和要按下的键作为参数传递。

  3. keyUp(CharSequence key) − 此方法用于执行修饰符键释放,作为参数传递。

  4. keyUp(WebElement e, CharSequence key) − 此方法用于在聚焦元素后执行修饰符键释放。webElement e 和要释放的键作为参数传递。

  5. sendKeys(CharSequence key) − 此方法用于向焦点元素发送键。要发送的键作为参数传递。

  6. sendKeys(WebElement e, CharSequence key) − 此方法用于向作为参数传递的 webElement 发送键。

  7. build() − 此方法用于创建动作组合,其中包含要执行的所有动作。

  8. perform() − 此方法用于在不首先调用 build() 的情况下执行动作。

请注意,在使用 * Actions Class* 的方法时,我们需要添加导入语句−

import org.openqa.selenium.interactions.Actions in our tests.

Example 1 - Copy Paste Task

让我们以下面的页面为例,我们首先在第一个输入框(高亮显示)的 Full Name: 旁边输入文本 - Selenium ,然后复制相同文本并将其粘贴到 Last Name: 旁边的另一个输入框(高亮显示)。

selenium keyboard events 2

Syntax

WebDriver driver = new ChromeDriver();

// Identify the first input box with xpath locator
WebElement e = driver.findElement(By.xpath("<value of xpath>"));

// enter some text
e.sendKeys("Selenium");

// chose the key as per platform
Keys k = Platform.getCurrent().is(Platform.MAC) ? Keys.COMMAND : Keys.CONTROL;

// object of Actions class to copy then paste
Actions a = new Actions(driver);
a.keyDown(k);
a.sendKeys("a");
a.keyUp(k);
a.build().perform();

// Actions class methods to copy text
a.keyDown(k);
a.sendKeys("c");
a.keyUp(k);
a.build().perform();

// Action class methods to tab and reach to next input box
a.sendKeys(Keys.TAB);
a.build().perform();

// Actions class methods to paste text
a.keyDown(k);
a.sendKeys("v");
a.keyUp(k);
a.build().perform();

// Identify the second input box with xpath locator
WebElement s = driver.findElement(By.xpath("<value of xpath>"));

// Getting text in the second input box
String text = s.getAttribute("value");
System.out.println("Value copied and pasted: " + text);

代码实现

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;

public class CopyPasteAction {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage where we will identify an element
      driver.get("https://www.tutorialspoint.com/selenium/practice/register.php");

      // Identify the first input box with xpath locator
      WebElement e = driver.findElement(By.xpath("//*[@id='firstname']"));

      // enter some text
      e.sendKeys("Selenium");

      // chose the key as per platform
      Keys k = Platform.getCurrent().is(Platform.MAC) ? Keys.COMMAND : Keys.CONTROL;

      // object of Actions class to copy then paste
      Actions a = new Actions(driver);
      a.keyDown(k);
      a.sendKeys("a");
      a.keyUp(k);
      a.build().perform();

      // Actions class methods to copy text
      a.keyDown(k);
      a.sendKeys("c");
      a.keyUp(k);
      a.build().perform();

      // Action class methods to tab and reach to next input box
      a.sendKeys(Keys.TAB);
      a.build().perform();

      // Actions class methods to paste text
      a.keyDown(k);
      a.sendKeys("v");
      a.keyUp(k);
      a.build().perform();

      // Identify the second input box with xpath locator
      WebElement s = driver.findElement(By.xpath("//*[@id='lastname']"));

      // Getting text in the second input box
      String text = s.getAttribute("value");
      System.out.println("Value copied and pasted: " + text);

      // Closing browser
      driver.quit();
   }
}
Value copied and pasted: Selenium

Process finished with exit code 0

在上面的示例中,我们首先在第一个输入框中输入了文本 Selenium ,然后复制相同文本并将其粘贴到了第二个输入框中。

最后,我们已将第二个输入框中输入的文本作为控制台中的消息获取 - Value copied and pasted: Selenium

最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。

Example 2 - Enter Text in Focus Input Box

让我们再举一个例子,我们将在处于焦点状态的 Full Name: 旁边的输入框内输入文本 Selenium

selenium keyboard events 3

Syntax

WebDriver driver = new ChromeDriver();

// Identify the input box with xpath locator
WebElement e = driver.findElement(By.xpath("<value of xpath>"));

// object of Actions class to input text in focus
Actions a = new Actions(driver);
a.click(e).sendKeys("Selenium").build().perform();

代码实现

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;

public class EnterTextInFocus {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage where we will identify an element
      driver.get("https://www.tutorialspoint.com/selenium/practice/text-box.php");

      // Identify the first input box with xpath locator
      WebElement e = driver.findElement(By.xpath("//*[@id='fullname']"));

      // object of Actions class to input text in focus
      Actions a = new Actions(driver);
      a.click(e).sendKeys("Selenium").build().perform();

      // Getting text in the second input box
      String text = e.getAttribute("value");
      System.out.println("Value entered to input box in focus: " + text);

      // Closing browser
      driver.quit();
   }
}
Value entered to input box in focus: Selenium

Process finished with exit code 0

在上面的示例中,我们首先在焦点输入框中输入了文本 Selenium ,然后将该文本作为控制台中的消息获取 - Value entered to input box in focus: Selenium

最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。

Example 3 - Enter Text in Input Box

让我们再举一个例子,我们将在 Name: 旁边的指定输入框内输入文本 Selenium

selenium keyboard events 4

Syntax

WebDriver driver = new ChromeDriver();

// Identify the input box with xpath locator
WebElement e = driver.findElement(By.xpath("<value of xpath>"));

// object of Actions class to input text in focus
Actions a = new Actions(driver);
a.sendKeys(e, "Selenium").build().perform();

代码实现

package org.example;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;

public class EnterText {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage where we will identify an element
      driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // Identify input box with xpath locator
      WebElement e = driver.findElement(By.xpath("//*[@id='name']"));

      // object of Actions class to input text
      Actions a = new Actions(driver);
      a.sendKeys(e,"Selenium").build().perform();

      // Getting text in the second input box
      String text = e.getAttribute("value");
      System.out.println("Value entered to input box: " + text);

      // Closing browser
      driver.quit();
   }
}
Value entered to input box: Selenium

Process finished with exit code 0

在上面的示例中,我们在输入框中输入了文本 Selenium ,然后将该文本作为控制台中的消息获取 - Value entered to input box: Selenium

最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。

Conclusion

这总结了我们对 Selenium Webdriver 键盘事件教程的全面讲解。我们从描述 Actions 类中键盘事件的基本方法开始,并举例说明如何在 Selenium Webdriver 中处理键盘事件。这使你深入了解 Selenium Webdriver 键盘事件。明智的做法是不断实践你学到的知识,并探索其他与 Selenium 相关的知识,以加深你的理解并拓宽你的视野。