Selenium 简明教程

Selenium WebDriver - Keyboard Events

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

Selenium Webdriver can be used to perform keyboard events operations like key up, and down, enter multiple characters in the middle other operations, and copy and paste operations using the Actions class. The methods keyUp(), keyDown(), and sendKeys() are used to perform these operations.

Basic Methods of Keyboard Events in Actions Class

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

There are multiple methods to perform Keyboard events available in the Actions class. To get more information on these methods, please refer to the below link −

  1. keyDown(CharSequence key) − This method is used to perform a modifier key press, passed as a parameter.

  2. keyDown(WebElement e, CharSequence key) − This method is used to perform a modifier key press post focusing an element. The webElement e, and key to be pressed are passed as parameters.

  3. keyUp(CharSequence key) − This method is used to perform a modifier key release, passed as a parameter.

  4. keyUp(WebElement e, CharSequence key) − This method is used to perform a modifier key release post focusing an element. The webElement e, and key to be released are passed as parameters.

  5. sendKeys(CharSequence key) − This method is used to send keys to elements in focus. The key to be sent is passed as a parameter.

  6. sendKeys(WebElement e, CharSequence key) − This method is used to send keys to the webElement passed as parameter.

  7. build() − This method is used to create a combination of actions having all the actions to be carried on.

  8. perform() − This method is used to perform actions without invoking the build() first.

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

Please note that, while using the methods of Actions Class, we would need to add the import statement −

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

Example 1 - Copy Paste Task

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

Let us take an example on the below page, where we would first enter the text - Selenium beside the Full Name: in the first input box(highlighted) then copy and paste the same text in another input box(highlighted) beside the 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);

代码实现

Code Implementation

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 ,然后复制相同文本并将其粘贴到了第二个输入框中。

In the above example, we had first entered the text Selenium in the first input box and then copied and pasted the same text in the second input box.

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

Finally, we had obtained the entered text in the second input box as a message in the console - Value copied and pasted: Selenium.

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

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Example 2 - Enter Text in Focus Input Box

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

Let us take another example, where we would enter text Selenium within the input box beside Full Name: which in focus.

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();

代码实现

Code Implementation

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

In the above example, we had first entered the text Selenium in the input box in focus and then obtained the text as a message in the console - Value entered to input box in focus: Selenium.

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

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Example 3 - Enter Text in Input Box

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

Let us take another example, where we would enter text Selenium within a designated input box beside Name:.

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();

代码实现

Code Implementation

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

In the above example, we had entered the text Selenium in an input box and then obtained the text as a message in the console - Value entered to input box: Selenium.

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

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Conclusion

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

This concludes our comprehensive take on the tutorial on Selenium Webdriver Keyboard Events. We’ve started with describing basic methods of keyboard events in Actions class,, and examples to illustrate how to handle keyboard events in Selenium Webdriver. This equips you with in-depth knowledge of the Selenium Webdriver Keyboard Events. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.