• Selenium Video Tutorials

Selenium WebDriver - 處理特殊按鍵



Selenium Webdriver 可用於在建立自動化測試時處理特殊按鍵。這可以透過使用Actions 類和 Selenium 中的 sendKeys() 方法來實現。使用 keyUp() 和 keyDown() 方法進行鍵上/下操作主要用於處理特殊按鍵。如果我們使用 sendKeys() 方法,則需要將 Key.chord 作為引數傳遞給此方法。

示例 1 - 使用特殊按鍵複製和貼上

現在讓我們討論如何在網頁上執行復制和貼上操作的元素識別,如下面的圖片所示。首先,右鍵單擊下面的網頁,然後單擊 Chrome 瀏覽器中的“檢查”按鈕。要檢查源元素和目標元素,請單擊 HTML 程式碼頂部的向左上箭頭。

Selenium Handle Special Keys 1

讓我們以以下頁面為例,我們首先在“全名:”標籤旁邊輸入文字 - JavaSelenium。然後將相同的文字複製並貼上到“姓氏:”標籤旁邊的另一個輸入框中。

Selenium Handle Special Keys 2

程式碼實現

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: JavaSelenium

Process finished with exit code 0

在上面的示例中,我們首先在第一個輸入框中輸入了文字JavaSelenium,然後將相同的文字複製並貼上到第二個輸入框中,然後在控制檯中獲取了第二個輸入框中的輸入文字作為訊息 - 已複製並貼上的值:JavaSelenium

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

示例 2 - 使用特殊按鍵複製和貼上

讓我們使用與上面相同的示例,並在不使用 Actions 類的情況下實現相同的操作。

package org.example;

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

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

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

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

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

      // select the whole entered text
      e.sendKeys(Keys.chord(k, "a"));

      // copy the whole entered text
      e.sendKeys(Keys.chord(k, "c"));

      // tab and reach to next input box
      e.sendKeys(Keys.TAB);

      // paste the whole entered text
      s.sendKeys(Keys.chord(k, "v"));

      // 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: JavaSelenium 

在上面的示例中,我們首先在第一個輸入框中輸入了文字JavaSelenium,然後使用 sendKeys() 和 Key.chord() 方法將相同的文字複製並貼上到第二個輸入框中。最後,我們在控制檯中獲取了第二個輸入框中的輸入文字作為訊息 - 已複製並貼上的值:JavaSelenium

示例 3 - 使用特殊按鍵輸入大寫文字

讓我們再舉一個例子,我們將使用特殊按鍵輸入大寫字母AUTOMATION。請注意,在將值傳送到 sendKeys() 方法時,我們將傳遞automation並按下 SHIFT 鍵。

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
      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("automation").keyUp(Keys.SHIFT).build().perform();

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

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

輸出

Text entered: AUTOMATION

Process finished with exit code 0

在上面的示例中,我們輸入了文字automation並按下了 SHIFT 鍵,然後在控制檯中獲取了大寫輸入文字以及訊息 - 輸入的文字:AUTOMATION

結論

這總結了我們關於 Selenium Webdriver 處理特殊按鍵教程的全面介紹。我們首先描述了一個使用特殊按鍵(如 CONTROL、SHIFT、TAB、CONTROL + A、CONTROL + V、CONTROL + C 等)複製和貼上文字的示例,並說明了如何使用 Selenium 輸入大寫文字。這使您深入瞭解了在 Selenium Webdriver 中處理特殊按鍵的方法。明智的做法是不斷練習您所學到的知識,並探索其他與 Selenium 相關的知識,以加深您的理解並擴充套件您的視野。

廣告