• Selenium Video Tutorials

Selenium WebDriver - 複製和貼上



在使用 Selenium WebDriver 開發的測試指令碼中,可以自動化複製和貼上操作。在測試應用程式時,我們經常會從一個編輯框中複製文字,然後將其貼上到另一個編輯框中。

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

現在讓我們討論一下在網頁上執行復制和貼上操作的元素的識別,如下圖所示。首先,我們需要右鍵單擊網頁,然後在 Chrome 瀏覽器中單擊“檢查”按鈕。然後,整個頁面的相應 HTML 程式碼將可見。為了檢查該網頁上的源元素和目標元素,我們需要單擊可見 HTML 程式碼頂部的左上箭頭,如下所示。

Selenium Copy 1

讓我們以以下頁面為例,我們首先在第一個輸入框(突出顯示)中輸入文字 -Selenium(在姓名旁邊),然後將相同的文字複製並貼上到另一個輸入框(突出顯示)中(在電子郵件旁邊)。

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

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

// copy text from first input box then paste to second input box
e.sendKeys(Keys.COMMAND, "a");
e.sendKeys(Keys.COMMAND, "c");

// paste to second input box
s.sendKeys(Keys.COMMAND, "v");

如果您使用的是 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("Selenium");

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

// copy text from first input box then paste to second input box
e.sendKeys(Keys.CONTROL, "a");
e.sendKeys(Keys.CONTROL, "c");

// paste to second input box
s.sendKeys(Keys.CONTROL, "v");

示例

在 CopyAndPaste.java 類檔案中實現程式碼。

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

public class CopyAndPaste {
   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 the first input box with xpath locator
      WebElement e = driver.findElement(By.xpath("//*[@id='name']"));

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

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

      // copy text from first input box then paste to second input box
      e.sendKeys(Keys.COMMAND, "a");
      e.sendKeys(Keys.COMMAND, "c");

      // paste to second input box
      s.sendKeys(Keys.COMMAND, "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: Selenium

Process finished with exit code 0

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

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

讓我們再舉一個例子,我們再次首先在一個輸入框中輸入文字,然後使用Actions類的keyUp()keyDown()sendKeys()方法將相同的文字複製並貼上到另一個輸入框中。所有這些方法都將要按下的鍵作為引數。

語法

如果您使用的是 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("Selenium");

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

// 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();

示例

在 CopyAndPasteActions.java 類檔案中實現程式碼。

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 CopyAndPasteAct {
   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 the first input box with xpath locator
      WebElement e = driver.findElement(By.xpath("//*[@id='name']"));

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

      // 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 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();

      // Identify the second input box with xpath locator
      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);

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

輸出

Value copied and pasted: Selenium

Process finished with exit code 0

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

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

因此,在本教程中,我們討論瞭如何使用 Selenium WebDriver 處理複製和貼上操作。

廣告
© . All rights reserved.