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是点击并维持着点击的状态,上面代码效果如下图

拖拽框体

响应式网站和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();

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

selenium webdriver(4)---模拟鼠标键盘操作的更多相关文章

  1. 将CodedUI Test 放到控制台程序中,模拟鼠标键盘操作

    CodedUI Test是微软的自动化测试工具,在VS中非常好用.可以用来模拟鼠标点击,键盘输入.但执行的时候必须要用mstest调用,无法传入参数(当然可以写入config文件中,但每次修改十分麻烦 ...

  2. selenium webdriver模拟鼠标键盘操作

    在测试使用Selenium webdriver测试WEB系统的时候,用到了模拟鼠标.键盘的一些输入操作. 1.鼠标的左键点击.双击.拖拽.右键点击等: 2.键盘的回车.回退.空格.ctrl.alt.s ...

  3. selenium webdriver从安装到使用(python语言),显示等待和隐性等待用法,切换窗口或者frame,弹框处理,下拉菜单处理,模拟鼠标键盘操作等

    selenium的用法 selenium2.0主要包含selenium IDE 和selenium webDriver,IDE有点类似QTP和LoadRunner的录制功能,就是firefox浏览器的 ...

  4. Java+selenium之WebDriver模拟鼠标键盘操作(六)

    org.openqa.selenium.interactions.Actions类,主要定义了一些模拟用户的鼠标mouse,键盘keyboard操作.对于这些操作,使用 perform()方法进行执行 ...

  5. selenium模拟鼠标键盘操作

    简单操作: 1.点击(鼠标左键)页面按钮:click() 2.清空输入框:clear() 3.输入字符串:send_keys()submit提交表单: 1.一般情况可以点击搜索按钮来搜索 2.也可以用 ...

  6. python模拟鼠标键盘操作 GhostMouse tinytask 调用外部脚本或程序 autopy右键另存为

    0.关键实现:程序窗口前置 python 通过js控制滚动条拉取全文 通过psutil获取pid窗口句柄,通过win32gui使程序窗口前置 通过pyauto实现右键菜单和另存为操作 1.参考 aut ...

  7. selenium之 玩转鼠标键盘操作(ActionChains)

    用selenium做自动化,有时候会遇到需要模拟鼠标操作才能进行的情况,比如单击.双击.点击鼠标右键.拖拽等等.而selenium给我们提供了一个类来处理这类事件——ActionChains sele ...

  8. selenuim2模拟鼠标键盘操作

    有时候有些元素不便点击或者做其他的操作,这个时候可以借助selenium提供的Actions类,它可以模拟鼠标和键盘的一些操作,比如点击鼠标右键,左键,移动鼠标等操作.对于这些操作,使用perform ...

  9. selenium层级定位及鼠标键盘操作

    #code:utf-8 from selenium import webdriver from selenium.webdriver.common.action_chains import Actio ...

随机推荐

  1. java新手笔记3 运算符&循环

    1.包 2.运算符 public class Operator { public static void main(String[] args) { int a = 5; System.out.pri ...

  2. 解码一个加密的js文件

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. 还在在专业的blog记录生活吧!

    本来觉得自己写文章水平很烂,技术贴也不能保证分析清晰透彻,就决定在百度hi上记录生活随笔.的. 但是,在百度,随便写点啥,都要审核.申诉. 还是在博客园安家吧. 从新手做起.

  4. 【制作镜像Win*】文件准备

    mkdir /var/image-createcd /var/image-create/ 在物理机上: wget http://10.254.3.75/images/libvirt/libvirt.x ...

  5. 九度OJ 1118 数制转换

    题目地址:http://ac.jobdu.com/problem.php?pid=1118 题目描述: 求任意两个不同进制非负整数的转换(2进制-16进制),所给整数在long所能表达的范围之内.   ...

  6. Google Web Designer 测试

    这东东完全就是一个flash啊,简单测试,感觉就是个做HTML5动画的..不过暂时是beta版的, 官方安装版的半天打不开,这边有个绿色版的,需要的童鞋可以这里下载:百度网盘

  7. js获取get方式提交的参数返回json格式数据

    /** * 获取GET提交的参数 * @return JSON格式 * @author Terry */ function getArgs(){ var args = {}; var match = ...

  8. Sqlserver 原生 MD5 函数(转)

    --创建md5函数CREATE FUNCTION [dbo].[MD5](@src varchar(255) )RETURNS varchar(255)ASBEGIN    DECLARE @md5 ...

  9. WPF中的字体改善

    WPF4对字体渲染做了很大的改善,增加了TextOptions属性,该属性可以设置TextFormattingMode,TextRenderingMode,TextHintingMode 1.Text ...

  10. POJ 3687 Labeling Balls 逆向建图,拓扑排序

    题目链接: http://poj.org/problem?id=3687 要逆向建图,输入的时候要判重边,找入度为0的点的时候要从大到小循环,尽量让编号大的先入栈,输出的时候注意按编号的顺序输出重量, ...