Selenium 简明教程

Selenium Webdriver - Copy and Paste

在使用 Selenium Webdriver 开发的测试脚本中,可以自动执行复制和粘贴操作。在测试应用程序时,我们经常从一个编辑框中复制文本并将其粘贴到另一个编辑框中。

The copy and paste operations can be automated in test scripts developed with Selenium Webdriver. We would often copy a text from an edit box and paste it in another edit box while testing an application.

可以使用 Selenium 中的 Keys 类执行复制和粘贴操作。用于复制和粘贴的键可以分别使用 Ctrl + CCtrl + V 完成。这些待按下的键作为参数发送给 sendKeys() 方法。

The copy and paste operations can be performed by using the Keys class in Selenium. The keys used to copy and paste can be done using the Ctrl + C and Ctrl + V respectively. These keys to be pressed are sent as parameters to the sendKeys() method.

我们现在讨论一下 Web 页面上要执行复制和粘贴操作的元素的标识,如下面的图片所示。首先,我们需要右击 Web 页面,然后在 Chrome 浏览器中单击“检查”按钮。然后,将看到整个页面的相应 HTML 代码。为了调查该网页上的源元素和目标元素,我们需要单击显示在可见 HTML 代码上方的左上箭头,如下所示。

Let us now discuss the identification of elements where the copy and paste operations are to be performed on a web page as shown in the below image. First, we would need to right click on the web page, and then click on the Inspect button in the Chrome browser. Then, the corresponding HTML code for the whole page would be visible. For investigating both the source and destination elements on that web page, we would need to click on the left upward arrow, available to the top of the visible HTML code as highlighted below.

selenium copy 1

我们以以下页面为例,首先在第一个输入框中输入 Selenium 旁边的文本(突出显示),然后将相同文本复制并粘贴到 Email 旁边的另一个输入框中(突出显示)。

Let us take an example on the below page, where we would first enter the text - Selenium beside the Name in the first input box (highlighted) then copy and paste the same text in another input box (highlighted) beside the Email.

selenium copy 2

Syntax

如果我们使用 MAC 机器,则语法为 −

Syntax if we are using a MAC machine −

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 机器,则语法为 −

Syntax if we are using a Windows machine −

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

Example

CopyAndPaste.java 类文件中的代码实现。

Code Implementation on CopyAndPaste.java class file.

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://www.tutorialspoint.com/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();
   }
}

Output

Value copied and pasted: Selenium

Process finished with exit code 0

在以上示例中,我们首先在第一个输入框中输入文本 Selenium ,然后将文本复制并粘贴到第二个输入框中,并且还在控制台中获得了输入的文本作为消息 - Value copied and pasted: Selenium

In the above example, we had first entered the text Selenium in the first input box and then copied and pasted the text in the second input box, and also obtained the entered text as a message in the console - Value copied and pasted: Selenium.

最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

我们再看另一个例子,其中我们将再次首先在输入框中输入文本,然后使用 Actions 类的 keyUp() keyDown()sendKeys() 方法将相同的文本复制并粘贴到另一个输入框中。所有这些方法都将要按下的键作为参数。

Let us take another example, where we would again first enter text in one input box then copy and paste the same text in another input box using the keyUp(), keyDown(), and sendKeys() methods of Actions class. All these methods take the key to be pressed as a parameter.

Syntax

如果我们使用 MAC 机器,则语法为 −

Syntax if we are using a MAC machine −

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 机器,则语法为 −

Syntax if we are using a Windows machine −

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

Example

CopyAndPasteActions.java 类文件中的代码实现。

Code Implementation on CopyAndPasteActions.java class file.

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://www.tutorialspoint.com/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();
   }
}

Output

Value copied and pasted: Selenium

Process finished with exit code 0

在以上示例中,我们首先在第一个输入框中输入文本 Selenium ,然后将文本复制并粘贴到第二个输入框中,并且还在控制台中获得了输入的文本作为消息 - Value copied and pasted: Selenium

In the above example, we had first entered the text Selenium in the first input box and then copied and pasted the text in the second input box, and also obtained the entered text as a message in the console - Value copied and pasted: Selenium.

最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

因此,在本教程中,我们讨论了如何使用 Selenium Webdriver 处理复制和粘贴操作。

Thus, in this tutorial, we had discussed how to handle copy and paste operations using the Selenium Webdriver.