• Selenium Video Tutorials

Selenium WebDriver - 鍵盤事件



Selenium WebDriver 可用於執行鍵盤事件操作,例如鍵上、鍵下、在中間輸入多個字元以及使用 Actions 類執行復制和貼上操作。`keyUp()`、`keyDown()` 和 `sendKeys()` 方法用於執行這些操作。

Actions 類中鍵盤事件的基本方法

Actions 類中有多種方法可以執行鍵盤事件。有關這些方法的更多資訊,請參考以下連結:

  • keyDown(CharSequence key) − 此方法用於執行修飾鍵按下,作為引數傳遞。
  • keyDown(WebElement e, CharSequence key) − 此方法用於在聚焦元素後執行修飾鍵按下。WebElement e 和要按下的鍵作為引數傳遞。
  • keyUp(CharSequence key) − 此方法用於執行修飾鍵釋放,作為引數傳遞。
  • keyUp(WebElement e, CharSequence key) − 此方法用於在聚焦元素後執行修飾鍵釋放。WebElement e 和要釋放的鍵作為引數傳遞。
  • sendKeys(CharSequence key) − 此方法用於將鍵傳送到焦點元素。要傳送的鍵作為引數傳遞。
  • sendKeys(WebElement e, CharSequence key) − 此方法用於將鍵傳送到作為引數傳遞的 WebElement。
  • build() − 此方法用於建立一個包含所有要執行的操作的動作組合。
  • perform() − 此方法用於在不先呼叫 build() 的情況下執行操作。

請注意,在使用Actions 類的方法時,我們需要新增匯入語句:

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

示例 1 - 複製貼上任務

讓我們來看一下下面的頁面示例,我們首先在第一個輸入框(已突出顯示)中,在全名:旁邊輸入文字 - Selenium,然後將相同的文字複製並貼上到姓氏:旁邊的另一個輸入框(已突出顯示)中。

Selenium Keyboard Events 2

語法

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://tutorialspoint.tw/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,然後將相同的文字複製並貼上到第二個輸入框中。

最後,我們在控制檯中收到輸入到第二個輸入框中的文字訊息 - 已複製並貼上的值:Selenium

最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。

示例 2 - 在焦點輸入框中輸入文字

讓我們來看另一個示例,我們將在全名:旁邊的焦點輸入框中輸入文字Selenium

Selenium Keyboard Events 3

語法

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://tutorialspoint.tw/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,然後在控制檯中收到文字訊息 - 輸入到焦點輸入框中的值:Selenium

最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。

示例 3 - 在輸入框中輸入文字

讓我們來看另一個示例,我們將在姓名:旁邊的指定輸入框中輸入文字Selenium

Selenium Keyboard Events 4

語法

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://tutorialspoint.tw/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,然後在控制檯中收到文字訊息 - 輸入到輸入框中的值:Selenium

最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。

結論

本教程全面介紹了 Selenium WebDriver 鍵盤事件。我們首先介紹了 Actions 類中鍵盤事件的基本方法,並透過示例說明了如何在 Selenium WebDriver 中處理鍵盤事件。這使您能夠深入瞭解 Selenium WebDriver 鍵盤事件。明智的做法是不斷練習所學內容,並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬您的視野。

廣告