【selenium】鼠标事件 & 键盘事件
1.鼠标事件
常用方法如下表所示:
鼠标事件 | 方法 |
右击 | contextClick() |
鼠标悬停 | clickAndHold() |
鼠标移动 | moveToElement() |
双击 | doubleClick() |
拖动:source: 鼠标拖动的源元素;target: 鼠标释放的目标元素 | dragAndDrop(source, target) |
释放鼠标 | release() |
执行所有 Actions 中存储的行为 | perform() |
1.1 右击contextClick()
package com.test.selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
/**
*contextClick
*
*/
public class App
{
public static void main( String[] args ) throws InterruptedException
{
System.setProperty("webdriver.chrome.bin", "G:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://www.baidu.com/";
System.out.printf("访问百度:",url);
driver.get(url);
driver.manage().window().maximize();
Thread.sleep(4000);
//鼠标事件
Actions action = new Actions(driver);
WebElement eleme =driver.findElement(By.id("kw"));
//鼠标右键点击指定元素
action.contextClick(eleme).build().perform();
Thread.sleep(4000);
driver.quit();
System.out.println( "执行结束" );
}
}
1.2 鼠标悬停clickAndHold()
package com.test.selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
/**
*clickAndHold
*
*/
public class App
{
public static void main( String[] args ) throws InterruptedException
{
System.setProperty("webdriver.chrome.bin", "G:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://www.baidu.com/";
System.out.printf("访问百度:",url);
driver.get(url);
driver.manage().window().maximize();
Thread.sleep(4000);
//鼠标对象
Actions action = new Actions(driver);
//鼠标悬停
WebElement set =driver.findElement(By.id("s-usersetting-top"));
action.clickAndHold(set).perform();
Thread.sleep(2000);
Thread.sleep(4000);
driver.quit();
System.out.println( "执行结束" );
}
}
1.3 鼠标移动moveToElement()
package com.test.selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
/**
*moveToElement
*
*/
public class App
{
public static void main( String[] args ) throws InterruptedException
{
System.setProperty("webdriver.chrome.bin", "G:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://www.baidu.com/";
System.out.printf("访问百度:",url);
driver.get(url);
driver.manage().window().maximize();
Thread.sleep(4000);
//鼠标对象
Actions action = new Actions(driver);
//鼠标移动
WebElement set =driver.findElement(By.linkText("贴吧"));
action.moveToElement(set).perform();
Thread.sleep(2000);
Thread.sleep(4000);
driver.quit();
System.out.println( "执行结束" );
}
}
1.4 双击doubleClick()
package com.test.selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
/**
*doubleClick
*
*/
public class App
{
public static void main( String[] args ) throws InterruptedException
{
System.setProperty("webdriver.chrome.bin", "G:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://www.baidu.com/";
System.out.printf("访问百度:",url);
driver.get(url);
driver.manage().window().maximize();
Thread.sleep(4000);
//鼠标对象
Actions action = new Actions(driver);
//双击
WebElement set =driver.findElement(By.linkText("新闻"));
action.doubleClick(set).perform();
Thread.sleep(2000);
Thread.sleep(4000);
driver.quit();
System.out.println( "执行结束" );
}
}
1.5 拖动dragAndDrop(source, target)
dragAndDrop(source, target)在源元素上按下鼠标左键,然后移动到目标元素上释放。
package com.test.selenium;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
/**
*dragAndDrop(source,target)
*
*/
public class App
{
public static void main( String[] args ) throws InterruptedException
{
System.setProperty("webdriver.chrome.bin", "G:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://www.baidu.com/";
System.out.printf("访问百度:",url);
driver.get(url);
driver.manage().window().maximize();
Thread.sleep(4000);
//鼠标对象
Actions action = new Actions(driver);
WebElement source = driver.findElement(By.xpath("//*[@id='hotsearch-content-wrapper']/li[3]/a/span[2]"));
WebElement target = driver.findElement(By.id("kw"));
Thread.sleep(1000);
//鼠标拖动
action.dragAndDrop(source,target).perform();
Thread.sleep(5000);
driver.quit();
System.out.println( "执行结束" );
}
}
2.键盘事件
常用键盘快捷键功能如下表:
键盘事件 | 方法 |
删除键(BackSpace) | sendKeys(Keys.BACK_SPACE) |
空格键(Space) | sendKeys(Keys.SPACE) |
制表键(Tab) | sendKeys(Keys.TAB) |
回退键(Esc) | sendKeys(Keys.ESCAPE) |
回车键(Enter) | sendKeys(Keys.ENTER) |
全选(Ctrl+A) | sendKeys(Keys.CONTROL, "a" ) |
复制(Ctrl+C) | sendKeys(Keys.CONTROL, "c" ) |
剪切(Ctrl+X) | sendKeys(Keys.CONTROL, "x" ) |
粘贴(Ctrl+V) | sendKeys(Keys.CONTROL, "v" ) |
键盘 F1 | sendKeys(Keys.F1) |
…… | …… |
键盘 F12 | sendKeys(Keys.F12) |
相关代码如下:
package com.test.selenium;
import java.util.Set;
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;
/**
*例举例键盘事件
*
*/
public class App
{
public static void main( String[] args ) throws InterruptedException
{
System.setProperty("webdriver.chrome.bin", "G:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://www.baidu.com/";
System.out.printf("访问百度:",url);
driver.get(url);
driver.manage().window().maximize();
Thread.sleep(4000);
WebElement target = driver.findElement(By.id("kw"));
target.sendKeys("selenium");
Thread.sleep(1000);
//删除
target.sendKeys(Keys.BACK_SPACE);
Thread.sleep(1000);
//空格
target.sendKeys(Keys.SPACE);
Thread.sleep(1000);
//制表符
target.sendKeys(Keys.TAB);
//回车键
target.sendKeys(Keys.TAB);
Thread.sleep(2000);
//ctrl+a
target.sendKeys(Keys.CONTROL, "a");
Thread.sleep(1000);
//ctrl+c
target.sendKeys(Keys.CONTROL, "c");
Thread.sleep(1000);
//ctrl+v
target.sendKeys(Keys.CONTROL, "v");
Thread.sleep(5000);
driver.quit();
System.out.println( "执行结束" );
}
}