Selenium 简明教程
Selenium WebDriver - Handle Special Keys
可以在创建自动化测试时使用 Selenium WebDriver 来处理特殊键。这是使用 Selenium 中的 * Actions class* 和 sendKeys() 方法完成的。使用 keyUp() 和 keyDown() 方法进行的向上/向下键入等操作主要用于处理特殊键。如果我们使用 sendKeys() 方法,我们将需要将 Key.chord 作为参数传递给此方法。
Example 1 - Copy and Paste With Special Keys
下面我们来讨论复制和粘贴操作将在网页上执行的元素标识,如下面的图片所示。首先,右键单击下面的网页,然后单击 Chrome 浏览器的“检查”按钮。要调查源元素和目标元素,请单击 HTML 代码顶部的左向上箭头。
让我们以下面的页面为例,我们首先输入文本- JavaSelenium * beside the *Full Name: 标签。然后将同一文本复制并粘贴到 Last Name: 标签旁边的另一个输入框中。
代码实现
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://www.tutorialspoint.com/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();
}
}
Example 2 - Copy and Paste With Special Keys
让我们使用上面相同的示例,并在不使用 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://www.tutorialspoint.com/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();
}
}
Example 3 - Input Text in Upper Case With Special Keys
让我们再举一个例子,我们将在其中使用特殊键以大写字母输入文本 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://www.tutorialspoint.com/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();
}
}