• Selenium Video Tutorials

Selenium WebDriver - 使用者互動



Selenium WebDriver 可用於各種使用者互動,例如點選連結或按鈕,在輸入框中輸入或清除文字,以及在下拉列表中選擇或取消選擇選項。

使用者互動的基本 Selenium Webdriver 命令

Selenium Webdriver 中有多個命令可以幫助對元素執行操作。它們列在下面 -

click()

此方法用於在元素的中間執行點選事件。如果元素的中間隱藏,則會丟擲異常。

右鍵點選網頁,然後在 Chrome 瀏覽器中點選“檢查”按鈕。然後,將開啟該頁面的相關 HTML 程式碼。要檢查下面頁面中的某個連結,例如無內容,請點選 HTML 程式碼頂部突出顯示的向上箭頭。

Selenium User Interactions 1

一旦我們點選並指向了“無內容”超連結的箭頭,它的 HTML 程式碼就可見了。可以使用連結文字定位器檢測連結。使用此方法,將定位具有連結文字匹配值的第一個連結。

示例

讓我們舉個例子,我們首先點選無內容連結,然後我們將獲得文字連結已使用狀態 204 和狀態文字進行響應

Selenium User Interactions 2

程式碼實現

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.concurrent.TimeUnit;

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

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

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

      // Opening the webpage where we will identify link
      driver.get("https://tutorialspoint.tw/selenium/practice/links.php");

      // Identify link with link text
      driver.findElement(By.linkText("No Content")).click();

      // identify text
      WebElement l = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[2]"));

      // Get text after clicking the link
      System.out.println("Text is: " + l.getText());
      
      // Closing browser
      driver.quit();
   }
}

輸出

Text is: Link has responded with status 204 and status text No Content

Process finished with exit code 0

在上面的示例中,點選無內容連結後獲得的文字為文字為:連結已使用狀態 204 和狀態文字無內容進行響應

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

sendKeys()

此方法用於在編輯框或任何可編輯欄位中輸入一些文字。每個輸入框都具有名為 input 的標籤名。此外,輸入框應具有 type 屬性,其值為 text。如果元素不可編輯,並且在該元素上應用了 sendKeys() 方法,則應丟擲異常。

clear()

此方法用於清除編輯框或任何可編輯欄位中的文字。如果元素不可編輯,並且在該元素上應用了 clear() 方法,則應丟擲異常。

Selenium User Interactions 3

示例

讓我們以上面頁面的一個示例為例,我們首先使用sendKeys()方法在輸入框中輸入一些文字。要輸入的值作為引數傳遞給該方法。然後,我們將使用clear()方法清除輸入的文字。

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 java.util.concurrent.TimeUnit;

public class InptBox {
   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 edit box and enter
      driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");

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

      // enter text in input box
      e.sendKeys("Selenium");

      // Get the value entered
      String text = e.getAttribute("value");
      System.out.println("Entered text is: " + text);

      // clear the text entered
      e.clear();

      // Get no text after clearing text
      String text1 = e.getAttribute("value");
      System.out.println("Get text after clearing: " + text1);

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

輸出

Entered text is: Selenium
Get text after clearing: 

Process finished with exit code 0

在上面的示例中,我們首先在輸入框中輸入了文字Selenium,並在控制檯中檢索了輸入的值,訊息為 - 輸入的文字為:Selenium。然後清除輸入的值,並且輸入框中沒有值。因此,我們在控制檯中收到以下訊息:清除後獲取文字

Select 類

Select 類中有多個方法可以幫助我們與下拉列表進行互動。每個下拉列表都由 select 標籤名標識,其選項由 option 標籤名標識。網頁上可能有兩種型別下拉列表 - 單選(允許選擇一個選項)和多選(允許選擇多個選項)。多選下拉列表還應具有一個屬性 multiple。

Selenium User Interactions 4

在上圖中,選項選擇一個標題具有屬性 selected,這意味著預設情況下已選中此選項。Select 類中有多個方法允許處理Selenium Webdriver 中的下拉列表

在下面的頁面中,我們將訪問“多選下拉列表”旁邊的多選下拉列表,選擇值電子產品和電腦運動和戶外,並執行一些驗證。

Selenium User Interactions 5

示例

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.support.ui.Select;
import java.util.List;
import java.util.concurrent.TimeUnit;

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

      // identify multiple dropdown
      WebElement dropdown = driver.findElement
         (By.xpath("//*[@id='demo-multiple-select']"));
         
      // object of Select class
      Select select = new Select(dropdown);

      // gets options of dropdown in list
      List<WebElement> options = select.getOptions();
      for (WebElement opt : options){
         System.out.println("Options are: " + opt.getText());
      }

      // return true if multi-select dropdown
      Boolean b = select.isMultiple();
      System.out.println("Boolean value for multiple dropdown: "+ b);

      // select item by value
      select.selectByValue("3");

      // select item by index
      select.selectByIndex(7);

      // get all selected options of dropdown in list
      List<WebElement> selectedOptions = select.getAllSelectedOptions();
      for (WebElement opt : selectedOptions){
         System.out.println("Selected Options are: " + opt.getText());
      }

      // get first selected option in dropdown
      WebElement f = select.getFirstSelectedOption();
      System.out.println("First selected option is: "+ f.getText());

      // deselect option by index
      select.deselectByIndex(7);
      
      // deselect all selected items
      select.deselectAll();

      // get all selected options of dropdown after deselected
      List<WebElement> delectedOptions = select.getAllSelectedOptions();
      System.out.println("No. options selected: " + delectedOptions.size());

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

輸出

Options are: Books
Options are: Movies, Music & Games
Options are: Electronics & Computers
Options are: Home, Garden & Tools
Options are: Health & Beauty
Options are: Toys, Kids & Baby
Options are: Clothing & Jewelry
Options are: Sports & Outdoors
Boolean value for multiple dropdown: true
Selected Options are: Electronics & Computers
Selected Options are: Sports & Outdoors
First selected option is: Electronics & Computers
No. options selected: 0

Process finished with exit code 0

在上面的示例中,我們使用控制檯中的訊息獲取了下拉列表的所有選項 - 選項為:書籍,選項為:電影,音樂和遊戲,選項為:家居,花園和工具,選項為:健康和美容,選項為:玩具,兒童和嬰兒,選項為:服裝和珠寶,選項為:運動和戶外。我們還使用控制檯中的訊息驗證了下拉列表是否具有多個選擇選項 - 用於檢查的布林值為:true

我們使用控制檯中的訊息檢索了下拉列表中選定的選項 - 選定的選項為:電子產品和電腦,選定的選項為:運動和戶外。我們還使用控制檯中的訊息獲取了第一個選定的選項 - 第一個選定的選項為:電子產品和電腦。最後,我們取消選擇了下拉列表中所有選定的選項,因此在控制檯中獲得了訊息 - 選定的選項數:0

滾動操作

Selenium webdriver 可以使用 JavaScriptExecutor(一個介面)在網頁上執行滾動操作。Selenium Webdriver 可以使用 JavaScriptExecutor 執行 JavaScript 命令。

閱讀更多:Selenium Webdriver 滾動操作.

結論

本教程全面介紹了 Selenium WebDriver 使用者互動。我們從描述使用者互動的基本 Selenium Webdriver 命令開始,並提供示例來說明如何在 Selenium Webdriver 中使用這些命令。這將使您深入瞭解 Selenium WebDriver 使用者互動。建議您不斷練習所學內容,並探索與 Selenium 相關的其他內容,以加深理解並拓寬視野。

廣告

© . All rights reserved.