• Selenium Video Tutorials

Selenium WebDriver - Action 類



Selenium Webdriver 可用於執行鍵盤和滑鼠操作。這包括諸如拖放、單擊、雙擊、右鍵單擊、使用控制鍵、其他鍵操作等操作。所有這些操作都是透過指定的使用者交換 API 執行的,在 Selenium Webdriver 中稱為 Actions 類。

Action 類方法

下面列出了 Actions 類中的一些可用方法:

  • click() - 此方法用於在滑鼠當前位置執行單擊。
  • build() - 此方法用於建立一個包含所有要執行的操作的動作組合。
  • perform() - 此方法用於執行操作,而無需先呼叫 build()。
  • release() - 此方法用於在滑鼠當前位置釋放滑鼠操作。
  • release(WebElement e) - 此方法用於在作為引數傳遞的 webElement e 的中間釋放滑鼠操作。
  • doubleClick(WebElement e) - 此方法用於在作為引數傳遞的 webElement e 的中間執行雙擊。
  • click(WebElement e) - 此方法用於在作為引數傳遞的 webElement e 的中間執行單擊。
  • doubleClick() - 此方法用於在滑鼠當前位置執行雙擊。
  • contextClick() - 此方法用於在滑鼠當前位置執行右鍵單擊。
  • contextClick(WebElement e) - 此方法用於在作為引數傳遞的 webElement e 的中間執行右鍵單擊。
  • moveToElement(WebElement e) - 此方法用於將滑鼠移動到作為引數傳遞的 webElement e 的中間。
  • moveToElement(WebElement e, int x-offset, int y-offset) - 此方法用於將滑鼠移動到視點中元素的偏移位置。webElement e、x 和 y 偏移值作為引數傳遞。
  • clickAndHold() - 此方法用於在滑鼠當前位置執行單擊(不釋放)。
  • clickAndHold(WebElement e) - 此方法用於在作為引數傳遞的 webElement e 的中間執行單擊(不釋放)。
  • 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。

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

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

示例 1 - 單擊、右鍵單擊和雙擊

讓我們以以下頁面為例,我們將透過分別單擊網頁上的單擊我、右鍵單擊我雙擊我按鈕來執行單擊、右鍵單擊和雙擊。

Selenium Action Class 1

在分別執行單擊我、右鍵單擊我雙擊我按鈕後,我們將在頁面上分別收到訊息您有一個動態點選您已雙擊

Selenium Action Class 2

程式碼實現

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;

public class ActionsMouse {
   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
      driver.get("https://tutorialspoint.tw/selenium/practice/buttons.php");

      // identify element with xpath for click
      WebElement m = driver.findElement
         (By.xpath("/html/body/main/div/div/div[2]/button[1]"));

      // object of Actions class to move then click
      Actions a = new Actions(driver);
      a.moveToElement(m).click().build().perform();

      // get text after click
      WebElement t = driver.findElement(By.xpath("//*[@id='welcomeDiv']"));
      System.out.println("Text after click: " + t.getText());

      // identify element with xpath for double click
      WebElement n = driver.findElement
         (By.xpath("/html/body/main/div/div/div[2]/button[3]"));

      // double click
      a.moveToElement(n).doubleClick().build().perform();

      // get text after double click
      WebElement x = driver.findElement(By.xpath("//*[@id='doublec']"));
      System.out.println("Text after double click: " + x.getText());

      // identify element with xpath for right click
      WebElement y = driver.findElement
         (By.xpath("/html/body/main/div/div/div[2]/button[2]"));

      // right click
      a.moveToElement(y).contextClick().build().perform();

      // Closing browser
      driver.quit();
   }
}

輸出

Text after click: You have done a dynamic click
Text after double click: You have Double clicked

Process finished with exit code 0

在上面的示例中,我們執行了單擊、雙擊和右鍵單擊,然後在控制檯中收到了訊息 - 單擊後的文字:您已執行動態單擊雙擊後的文字:您已雙擊

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

示例 2 - 滑鼠懸停

讓我們再舉一個例子,最初選單Navbar為黑色。

Selenium Action Class 3

但是,當懸停並按住它時,選單 Navbar 的顏色從黑色變為綠色,如下面的影像所示。

Selenium Action Class 4

程式碼實現

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;

public class ActionsClickandHold {
   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
      driver.get("https://tutorialspoint.tw/selenium/practice/menu.php#");

      // identify element with xpath for click and hold
      WebElement m = driver.findElement
         (By.xpath("/html/body/main/div/div/div[2]/nav/div/a"));

      // get element color in rgba format
      String s = m.getCssValue("color");
      System.out.println("rgba code for color element: " + s );

      // object of Actions class to click and hold
      Actions a = new Actions(driver);
      a.clickAndHold(m).build().perform();

      // get element color in rgba format
      String c = m.getCssValue("color");
      System.out.println("rgba code for color for element after click and hold: " + c);

      // Closing browser
      driver.quit();
   }
}

輸出

rgba code for color element: rgba(51, 51, 51, 1)
rgba code for color for element after click and hold: rgba(64, 169, 68, 1)

Process finished with exit code 0

示例 3 - 拖放

讓我們以以下頁面為例,我們將把帶有文字將我拖到我的目標的源框拖到帶有文字在此處放置的目標框。

Selenium Action Class 5

一旦整個操作完成,我們將在網頁上獲得文字已放置!,如下面的影像中突出顯示的那樣。

Selenium Action Class 6

程式碼實現

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;

public class DragAndDrp {
   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);

      // URL launch for accessing drag and drop elements
      driver.get("https://tutorialspoint.tw/selenium/practice/droppable.php");

      // identify source and target elements for drag and drop
      WebElement sourceElement= driver.findElement(By.id("draggable"));
      WebElement targetElement= driver.findElement(By.id("droppable"));

      // drag and drop operations without build and perform methods
      Actions a = new Actions(driver);
      a.dragAndDrop(sourceElement, targetElement).build().perform();

      // identify text after element is dropped
      WebElement text = driver.findElement(By.xpath("//*[@id='droppable']/p"));
      System.out.println("Text is : " + text.getText());

      // quitting browser after drag and drop operations completed
      driver.quit();
   }
}

輸出

Text is after dragging: Dropped!

Process finished with exit code 0

在上面的示例中,我們首先從源定位器到目標定位器執行了拖放操作,並且還在控制檯中收到了訊息 - 文字為:已放置!(在本文中,此應用程式中的拖放操作完成後會收到此訊息)。

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

示例 4 - 鍵操作

讓我們再舉一個例子,我們將在輸入框中使用 Action 類的 Key Up Key Down 方法輸入大寫字母的文字SELENIUM。請注意,在將值傳送到 sendKeys() 方法時,我們將傳遞selenium 以及按下 SHIFT 鍵。因此,我們將在輸入框中獲得輸入的輸出為SELENIUM

package org.example;

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 org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;

public class KeyAction {
   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']"));

      // Actions class
      Actions a = new Actions(driver);

      // moving to an input box and clicking on it
      a.moveToElement(e).click();

      // key UP and DOWN action for SHIFT
      a.keyDown(Keys.SHIFT);
      a.sendKeys("Selenium").keyUp(Keys.SHIFT).build().perform();

      // get value entered
      System.out.println("Text entered: " + e.getAttribute("value"));

      // Closing browser
      driver.quit();
   }
}

輸出

Text entered: SELENIUM

Process finished with exit code 0

在上面的例子中,我們在輸入框中輸入了文字selenium 以及按下 SHIFT 鍵,因此得到了大寫的輸入文字,並在控制檯中顯示訊息 - Text entered: SELENIUM

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

結論

本教程到此結束,我們全面介紹了 Selenium Webdriver Action 類。我們從描述 Action 類開始,逐步講解了 Action 類的各種方法,並透過示例說明了如何執行點選、右鍵點選、雙擊、滑鼠懸停、拖放以及與 Selenium 結合的按鍵操作。這使您能夠深入瞭解 Action 類。建議您多練習所學內容,並探索其他與 Selenium 相關的知識,以加深理解並拓寬視野。

廣告

© . All rights reserved.