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. OpenJudge 1806:词典find()与end()

    1806:词典 总时间限制:  3000ms  内存限制:  65536kB 描述 你旅游到了一个国外的城市.那里的人们说的外国语言你不能理解.不过幸运的是,你有一本词典可以帮助你. 输入 首先输入一 ...

  2. 状态模式(State Pattern)

    状态模式:允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类. 这个模式将状态封装成为独立的类,并将动作委托到代表当前对象的对象,这样行为就与拥有状态类解耦了. 从客户的角度来看,对象的 ...

  3. greenplum学习

    公司TM蛋疼,动不动让你学习新东西,就是不让你闲下来,本着胳膊拧不过大腿定律,忍了,这是背景. 好吧哥端起一本厚厚的<GreenPlum企业应用实战>,打开百度开始GP的学习之路: GP只 ...

  4. HTML5基础知识(一)---标签

    在HTML5中,Web页面中重新调整了页面规划,这其中新引入了几个新标记. 我们将创建一个简单的Web页面,该页面包含一个Header区.一个Navigation区.一个Article区(包含三个部分 ...

  5. 黑马程序员-------.net基础知识二

    变量 变量代表着一块内存空间,我们可以通过变量名称想内存存/取数据,有变量就不需要我们记忆复杂的内存地址. 向内存中申请一块内存空间的语法:   数据类型 变量名; 变量类型 变量类型 存储位置 自动 ...

  6. 从 IT 的角度思考 BIM(一):面向对象

    还记得那个笑话吗:要把大象放进冰箱,总共分几步?这不仅仅是一个笑话,还是一个值得我们好好分析的笑话. 如果要放进冰箱的是一个苹果,那么也就不可笑了,但换成大象,就引起了我们的兴趣和注意,为什么? 我们 ...

  7. mooc

    Coursera 课程来源 2014年前已与斯坦福.普林斯顿等近90所大学和教育机构达成合作关系. 用户类型 主要类别为学生.求职者.公司人.其中,求职者可在Coursera上获得<成就报告&g ...

  8. C++程序的构成和书写形式

    C++程序的结构和书写格式归纳如下:  (1) 一个C++程序可以由一个程序单位或多个程序单位构成.每一个程序单位作为一个文件.在程序编译时,编译系统分别对各个文件进行编译,因此,一个文件是一个编译单 ...

  9. JQuery为元素添加样式

    由于jquery支持css3,所有能很好的兼容很多浏览器,所以通过jquery来使用css样式比较好 为定义好的css样式可以调用元素的css方法添加样式 $("span").cs ...

  10. Eclipse里初次使用Maven注意问题

    在Eclipse里初次使用Maven,右键project->add dependency, 发现search不管用,这时候需要按照以下步骤操作: Goto "Preferences - ...