[跟着官方文档学Selenium][学习笔记][八][WebDriver的Actions接口]
用于向web浏览器提供虚拟设备输入的底层接口
与执行额外验证的高级元素交互不同,Actions接口提供了对输入设备的细粒度控制。
Selenium提供3种输入源:键盘设备的键位输入,鼠标、笔或触摸设备的指针输入,以及支持滚轮的滚轮输入。
键盘
用于与网页交互的任何关键输入设备的表示形式。
Keyboard代表一个键盘事件。Keyboard操作通过使用底层接口允许我们向web浏览器提供虚拟设备输入。
Keys
除了由常规Unicode表示的键外,Unicode值还分配给其他键以用于Selenium。每种语言都有自己的方法来引用这些键。
Key down
KeyDown用于模拟按下辅助按键(CONTROL,SHIFT,ALT)的动作
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
public class keyDownDemo1
{
public static void main(String[] args)
{
WebDriver webDriver = new EdgeDriver();
webDriver.get("https://www.baidu.com/");
//输入webdriver并按下回车
webDriver.findElement(By.id("kw")).sendKeys("webdriver" + Keys.ENTER);
Actions actionProvider = new Actions(webDriver);
Action keydown = actionProvider.keyDown(Keys.CONTROL).sendKeys("a").build();
keydown.perform();
}
}
Key up
keyUp用于模拟辅助按钮(CONTROL,SHIFT,ALT)弹起或释放的操作
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.interactions.Actions;
public class keyUpDemo1
{
public static void main(String[] args)
{
WebDriver webDriver = new EdgeDriver();
try
{
webDriver.get("https://www.baidu.com");
Actions actions = new Actions(webDriver);
WebElement search = webDriver.findElement(By.id("kw"));
actions.keyDown(Keys.LEFT_SHIFT).sendKeys(search,"qwerty")
.keyUp(Keys.LEFT_SHIFT).sendKeys("qwerty").perform();
}
finally
{
webDriver.quit();
}
}
}
Send keys
这是操作API中的一种便捷方法,它将keyDown和keyUp命令组合到一个操作中。执行此命令与使用element方法略有不同。
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.interactions.Actions;
public class sendKeyDemo1
{
public static void main(String[] args)
{
WebDriver webDriver = new EdgeDriver();
Actions actions = new Actions(webDriver);
webDriver.get("https://www.baidu.com");
WebElement search = webDriver.findElement(By.id("kw"));
actions.keyDown(Keys.LEFT_SHIFT).sendKeys(search,"h")
.keyUp(Keys.LEFT_SHIFT).sendKeys(search,"elloworld").perform();
webDriver.findElement(By.id("su")).click();
}
}
鼠标
Mouse表示鼠标事件。鼠标操作是通过使用底层接口执行的,其允许我们向web浏览器提供虚拟化的设备输入操作。
clickAndHold
它将移动到该元素,然后在给定元素的中间点击(不释放)
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.interactions.Actions;
public class clickAndHold
{
public static void main(String[] args)
{
WebDriver webDriver = new EdgeDriver();
try
{
webDriver.get("https://www.baidu.com");
//保存
WebElement element = webDriver.findElement(By.linkText("登录"));
String id = element.getAttribute("id");
System.out.println(id);//s-top-loginbtn
Actions actionProvider = new Actions(webDriver);
actionProvider.clickAndHold(element).build().perform();
}finally
{
webDriver.quit();
}
}
}
contextClick
此方法首先将鼠标移动到元素的位置,然后在给定元素执行上下文点击(右键单击)。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.interactions.Actions;
public class contextClick
{
public static void main(String[] args)
{
WebDriver webDriver = new EdgeDriver();
try
{
webDriver.get("https://www.baidu.com");
//存储登录按钮元素
WebElement loginBtn = webDriver.findElement(By.linkText("登录"));
Actions actionProvider = new Actions(webDriver);
//在元素上执行操作
actionProvider.contextClick(loginBtn).build().perform();
}
finally
{
webDriver.quit();
}
}
}
doubleClick
它将移动到该元素,并在给定元素的中间双击。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.interactions.Actions;
public class doubleClick
{
public static void main(String[] args)
{
WebDriver webDriver = new EdgeDriver();
try
{
webDriver.get("https://www.baidu.com");
WebElement loginBtn = webDriver.findElement(By.linkText("登录"));
Actions actionProvider = new Actions(webDriver);
actionProvider.doubleClick(loginBtn).build().perform();
}
finally
{
webDriver.quit();
}
}
}
moveToElement
此方法将鼠标移到元素的中间,执行此操作时,该元素也会滚动到视图中。
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.interactions.Actions;
public class moveToElement
{
public static void main(String[] args)
{
WebDriver webDriver = new EdgeDriver();
try
{
webDriver.manage().window().setSize(new Dimension(1024,768));
webDriver.get("http://www.example.com/");
WebElement informationLink = webDriver.findElement(By.linkText("More information..."));
Actions actionProvider = new Actions(webDriver);
actionProvider.moveToElement(informationLink).build().perform();
}
finally
{
webDriver.quit();
}
}
}
moveByOffset
此方法将鼠标从其当前位置(或0,0)移动给定的偏移量。如果坐标在视图窗口之外,则鼠标最终将在浏览器窗口之外。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.interactions.Actions;
public class moveByOffset
{
public static void main(String[] args)
{
WebDriver webDriver = new EdgeDriver();
try
{
webDriver.get("https://www.baidu.com");
WebElement loginBtn = webDriver.findElement(By.linkText("登录"));
int xOffset = loginBtn.getRect().getX();
int yOffset = loginBtn.getRect().getY();
Actions actionProvider = new Actions(webDriver);
actionProvider.moveByOffset(xOffset,yOffset).build().perform();
}
finally
{
webDriver.quit();
}
}
}
dragAndDrop
此方法首先在源元素上单击并按住,然后移动到目标元素的位置后释放鼠标。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.interactions.Actions;
public class dragAndDrop
{
public static void main(String[] args)
{
WebDriver webDriver = new EdgeDriver();
try
{
webDriver.get("https://crossbrowsertesting.github.io/drag-and-drop");
//存储'Box A'元素
WebElement sourceEle = webDriver.findElement(By.id("draggable"));
//存储'Box B'元素
WebElement targetEle = webDriver.findElement(By.id("droppable"));
Actions actionProvider = new Actions(webDriver);
//将sourceEle拖拽到targetEle
actionProvider.dragAndDrop(sourceEle,targetEle).build().perform();
}
finally
{
webDriver.quit();
}
}
}
dragAndDropBy
此方法首先在源元素上单击并按住,移至给定的偏移量后释放鼠标。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.interactions.Actions;
public class dragAndDropBy
{
public static void main(String[] args)
{
WebDriver webDriver = new EdgeDriver();
try
{
webDriver.get("https://crossbrowsertesting.github.io/drag-and-drop");
WebElement sourceEle = webDriver.findElement(By.id("draggable"));
WebElement targetEle = webDriver.findElement(By.id("droppable"));
int targetEleXOffset = targetEle.getLocation().getX();
int targetEleYOffset = targetEle.getLocation().getY();
Actions actionProvider = new Actions(webDriver);
actionProvider.dragAndDropBy(sourceEle,targetEleXOffset,targetEleYOffset).build().perform();
}
finally
{
webDriver.quit();
}
}
}
release
此操作将释放按下的鼠标左键。如果WebElement转移了,它将释放给定WebElement上按下的鼠标左键。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.interactions.Actions;
public class release
{
public static void main(String[] args)
{
WebDriver webDriver = new EdgeDriver();
try
{
webDriver.get("https://crossbrowsertesting.github.io/drag-and-drop");
WebElement sourceEle = webDriver.findElement(By.id("draggable"));
WebElement targetEle = webDriver.findElement(By.id("droppable"));
Actions actionProvider = new Actions(webDriver);
actionProvider.clickAndHold(sourceEle).moveToElement(targetEle).build().perform();
actionProvider.release().build().perform();
}
finally
{
webDriver.quit();
}
}
}
滚轮
滚轮操作将要伴随Selenium 4.2发布
- 点赞
- 收藏
- 关注作者
评论(0)