selenium webdriver(4)---模拟鼠标键盘操作

时间:2022-07-06 19:38:53

webdriver提供Actions来模拟鼠标悬浮、拖拽和键盘输入等操作,详细代码见org.openqa.selenium.interactions.Actions.本文通过几个实例来说明Actions的相关操作

输入数据

需求:登录安居客网站,在二手房板块输入"@@@",点击搜索,正确跳转成功反之失败,大部分情况下我们这样写

 //搜索二手房

 import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebElement; public class NewTest{
public static void main(String[] args) throws InterruptedException { System.setProperty ( "webdriver.chrome.driver" ,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.get("http://shanghai.anjuke.com");
try{ WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']"));
WebElement search=driver.findElement(By.xpath("//input[@id='btnSubmit']"));
input.sendKeys("@@@");
search.click();
if(driver.getTitle().contains("@"))
System.out.println("搜索成功,跳转到"+driver.getTitle()+"页面");
else
System.out.println("搜索失败,跳转到了"+driver.getTitle()+"页面"); }catch(Exception e){
e.printStackTrace();
}finally{
Thread.sleep(3000);
driver.quit();
}
}

使用Actions类还可以这样写

 import java.util.List;
import java.util.Set; import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.WebElement; public class NewTest{
public static void main(String[] args) throws InterruptedException { System.setProperty ( "webdriver.chrome.driver" ,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.get("http://shanghai.anjuke.com");
try{ WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']"));
WebElement search=driver.findElement(By.xpath("//input[@id='btnSubmit']")); //生成Actions实例对象
Actions actions=new Actions(driver);
//输入@@@点击搜索
actions.keyDown(input, Keys.SHIFT).sendKeys("222")
.keyUp(Keys.SHIFT).click(search).perform(); if(driver.getTitle().contains("@"))
System.out.println("搜索成功,跳转到"+driver.getTitle()+"页面");
else
System.out.println("搜索失败,跳转到了"+driver.getTitle()+"页面"); }catch(Exception e){
e.printStackTrace();
}finally{
Thread.sleep(3000);
driver.quit();
}
}

keyDown表示按下键盘,keyUp表示松开键盘。上述代码中先是执行keyDown,这时shift键按下后面的sendKeys内容也是在shift键按下的情况输入的,所以实际输入的是@@@.

鼠标悬浮

需求:登录安居客首页,切换城市到杭州

 import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.WebElement; public class NewTest{
public static void main(String[] args) throws InterruptedException { System.setProperty ( "webdriver.chrome.driver" ,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.get("http://shanghai.anjuke.com");
try{ WebElement city=driver.findElement(By.xpath("//a[@id='switch_apf_id_8']"));
WebElement citys=driver.findElement(By.xpath("//div[@id='city-panel']"));
WebElement cityOption=driver.findElement(By.xpath("//a[@title='杭州房产网']"));
//生成Actions实例对象
Actions actions=new Actions(driver); //鼠标悬浮到城市标签
actions.moveToElement(city).perform(); if(citys.isDisplayed())
System.out.println("鼠标悬浮成功,城市模块的display:"+
citys.getCssValue("display"));
else
System.out.println("鼠标悬浮失败,城市模块的display:"+
citys.getCssValue("display"));
//点击杭州
actions.click(cityOption).perform(); if(driver.getTitle().contains("杭州"))
System.out.println("切换成功,跳转到"+driver.getTitle()+"页面");
else
System.out.println("切换失败,跳转到了"+driver.getTitle()+"页面"); }catch(Exception e){
e.printStackTrace();
}finally{
Thread.sleep(3000);
driver.quit();
}
}
多选框

由于安居客网站没有这方面的例子,下面就采用YUI类库的DEMO界面来演示

 import java.util.List;
import java.util.Set; import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.WebElement; public class NewTest{
public static void main(String[] args) throws InterruptedException { System.setProperty ( "webdriver.chrome.driver" ,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.get("http://jqueryui.com/selectable/");
try{
WebElement frame=driver.findElement(By.xpath("//iframe[@class='demo-frame']"));
driver.switchTo().frame(frame);
List<WebElement> selects=driver
.findElements(By.xpath("//li[@class='ui-widget-content ui-selectee']"));
//生成Actions实例对象
Actions actions=new Actions(driver);
actions.clickAndHold(selects.get(0)).clickAndHold(selects.get(1)).click().perform(); }catch(Exception e){
e.printStackTrace();
}finally{
Thread.sleep(3000);
driver.quit();
}
}

clickAndHold是点击并维持着点击的状态,上面代码效果如下图

selenium webdriver(4)---模拟鼠标键盘操作

拖拽框体

响应式网站和OA系统这方面的需求较多,下面演示如何将拖拽对象拖拽到右下角

 import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.WebElement; public class NewTest{
public static void main(String[] args) throws InterruptedException { System.setProperty ( "webdriver.chrome.driver" ,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.get("http://jqueryui.com/draggable/");
try{
WebElement frame=driver.findElement(By.xpath("//iframe[@class='demo-frame']"));
driver.switchTo().frame(frame);
WebElement dragTable=driver.findElement(By.xpath("//div[@id='draggable']"));
//生成Actions实例对象
Actions actions=new Actions(driver);
//拖拽对象
actions.dragAndDropBy(dragTable, 270, 170).perform(); }catch(Exception e){
e.printStackTrace();
}finally{
Thread.sleep(3000);
driver.quit();
}
}

其实dragAndDropBy方法就是clickAndHold和moveByOffset方法的组合而已

  actions.dragAndDropBy(dragTable, 270, 170).perform();
  actions.clickAndHold(dragTable).moveByOffset(270, 170).click().perform();

这两行代码功能是一样的,正所谓条条大路通罗马,我们只需选择最近、最安全的一条就好了。