• Selenium Video Tutorials

Selenium WebDriver - 鍵盤向上/向下鍵



Selenium WebDriver 可以藉助 **Actions 類** 執行鍵盤向上和鍵盤向下操作。 使用 keyUp() 和 keyDown() 方法執行鍵盤向上和鍵盤向下操作。

複製和貼上操作可以透過在 Selenium 中使用 Keys 類來執行。 用於複製和貼上的鍵可以使用 Ctrl + CCtrl + V 分別完成。 要按下的這些鍵作為引數傳送到 sendKeys() 方法。

網頁上元素的識別

右鍵單擊網頁,然後在 Chrome 瀏覽器中單擊“檢查”按鈕。 要調查該網頁上的元素,請單擊可見 HTML 程式碼頂部的左上箭頭,如下所示。

Selenium Key Up Down 1

複製和貼上文字

在下面的頁面中,在第一個編輯框中的 Full Name 旁邊輸入文字 - Copy,然後將相同的文字複製並貼上到 Email 旁邊的另一個輸入框中。

Selenium Key Up Down 2

語法

在 MAC 機器上的語法 -

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

// Identify the second input box with xpath locator
WebElement s = driver.findElement(By.xpath("<value of xpath>"));

// Actions class methods to select text
Actions a = new Actions(driver);
a.keyDown(Keys.COMMAND);
a.sendKeys("a");
a.keyUp(Keys.COMMAND);
a.build().perform();

// Actions class methods to copy text
a.keyDown(Keys.COMMAND);
a.sendKeys("c");
a.keyUp(Keys.COMMAND);
a.build().perform();

// Action class methods to tab and reach to  the next input box
a.sendKeys(Keys.TAB);
a.build().perform();

// Actions class methods to paste text
a.keyDown(Keys.COMMAND);
a.sendKeys("v");
a.keyUp(Keys.COMMAND);
a.build().perform();

在 Windows 機器上的語法 -

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

// Identify the second input box with xpath locator
WebElement s = driver.findElement(By.xpath("<value of xpath>"));

// Actions class methods to select text
Actions a = new Actions(driver);
a.keyDown(Keys.CONTROL);
a.sendKeys("a");
a.keyUp(Keys.CONTROL);
a.build().perform();

// Actions class methods to copy text
a.keyDown(Keys.CONTROL);
a.sendKeys("c");
a.keyUp(Keys.CONTROL);
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(Keys.CONTROL);
a.sendKeys("v");
a.keyUp(Keys.CONTROL);
a.build().perform();

示例

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 CopyAndPasteAction {
   public static void main(String[] args) throws InterruptedException {

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

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

      // Opening the webpage
      driver.get("https://tutorialspoint.tw/selenium/practice/text-box.php");

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

      // enter text
      e.sendKeys("Copy");

      // select text
      Actions act = new Actions(driver);
      act.keyDown(Keys.COMMAND);
      act.sendKeys("a");
      act.keyUp(Keys.COMMAND);
      act.build().perform();

      // copy text
      act.keyDown(Keys.COMMAND);
      act.sendKeys("c");
      act.keyUp(Keys.COMMAND);
      act.build().perform();

      // tab to reach to next input box
      act.sendKeys(Keys.TAB);
      act.build().perform();
      
      // paste text
      act.keyDown(Keys.COMMAND);
      act.sendKeys("v");
      act.keyUp(Keys.COMMAND);
      act.build().perform();

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

      // Getting text in the second input box
      String text = s.getAttribute("value");
      System.out.println("Value copied and pasted: " + text);

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

輸出

Value copied and pasted: Copy

Process finished with exit code 0

在上面的示例中,我們首先在第一個編輯框中輸入了文字 Copy,然後將相同的文字複製並貼上到第二個編輯框中。 最後,我們在控制檯中檢索了第二個輸入框中輸入的文字作為訊息 - Value copied and pasted: Copy

最後,收到訊息 Process finished with exit code 0,表示程式碼成功執行。

文字大寫

讓我們再舉一個例子,我們將使用 Actions 類的鍵盤向上、鍵盤向下功能在輸入框中輸入文字 TUTORIALSPOINT(大寫字母)。 將值傳送到 sendKeys() 方法時,我們將傳遞 tutorialspoint 以及按住 SHIFT 鍵,以使輸入框中的輸出為 TUTORIALSPOINT

示例

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 17 secs
      driver.manage().timeouts().implicitlyWait(17, 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
      WebElement e = driver.findElement(By.xpath("//*[@id='fullname']"));

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

      // moving to element and clicking on it
      act.moveToElement(e).click();

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

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

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

輸出

Text entered: TUTORIALSPOINT

我們在控制檯中使用訊息獲得了大寫輸入的文字 - Text entered: TUTORIALSPOINT

結論

這總結了我們關於 Selenium WebDriver 鍵盤向上/向下鍵教程的全面概述。 我們從描述網頁上元素的識別開始,並逐步介紹瞭如何使用 Selenium WebDriver 處理鍵盤向上和鍵盤向下方法的示例。 這使您深入瞭解了 Selenium WebDriver 鍵盤向上/向下鍵。 明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並擴充套件您的視野。

廣告

© . All rights reserved.