---------------------------------------

http://www.ltesting.net/ceshi/open/kygncsgj/selenium/2013/0115/205906.html

自动化过 程中,有时候我们需要给某个元素设置焦点,在selenium1.0中提供了给元素设置焦点的方法。但是在2.0中并没有该办法。如果是输入框我们可以使 用click方法,来设置焦点,但是对于link连接或者button如果通过click方法势必会跳转到另外页面或者提交了页面请求。通过尝试发现,如 果在元素上进行右击,也可以设置焦点,但是会弹出一个菜单,这个时候我们可以通过按下键盘的esc键来取消右击弹出的菜单,这样焦点就可以设置成功了。下 面我通过键盘和鼠标事件组合来实现该功能。代码如下:

  import java.awt.AWTException;

  import java.awt.Robot;

  import java.awt.event.KeyEvent;

  import org.openqa.selenium.By;

  import org.openqa.selenium.WebDriver;

  import org.openqa.selenium.WebElement;

  import org.openqa.selenium.firefox.FirefoxDriver;

  import org.openqa.selenium.firefox.FirefoxProfile;

  import org.openqa.selenium.interactions.Actions;

  import org.openqa.selenium.remote.DesiredCapabilities;

  import org.testng.annotations.AfterMethod;

  import org.testng.annotations.BeforeMethod;

  import org.testng.annotations.Test;

  public class TestActive {

  WebDriver driver = null;

  Actions action = null;

  Robot robot = null;

  @BeforeMethod

  public void setUp(){

  try {

  robot = new Robot();

  } catch (AWTException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  System.setProperty(“webdriver.firefox.bin”, “D:/Firefox/firefox.exe”);

  FirefoxProfile file = new FirefoxProfile();

  DesiredCapabilities ds = DesiredCapabilities.firefox();

  ds.setCapability(FirefoxDriver.PROFILE, file);

  driver = new FirefoxDriver(ds);

  action = new Actions(driver);

  }

  @AfterMethod

  public void tearDown(){

  }

  @Test

  public void start(){

  driver.get(“http://www.baidu.com”);

  driver.manage().window().maximize();

  //查找你需要设置焦点的元素

  WebElement button = driver.findElement(By.xpath(“//*[@id='nv']/a[5]“));

  //对该元素进行右击操作

  action.contextClick(button).perform();

  //按ESC键返回,设置焦点成功

  robot.keyPress(KeyEvent.VK_ESCAPE);

//用selenium中自带的key方法也是OK的,import org.openqa.selenium.Keys;   如下方法,也是按ESC键

button.sendKeys(Keys.chord(Keys.ESCAPE));

  }

  }

webdriver 获取元素焦点方法的更多相关文章

  1. JavaScript通过元素id和name直接获取元素的方法

    概览: 偶然的机会,我在JavaScript中直接用HTML元素的id属性来获取该元素,并设置该元素的其他属性值,竟然能够正确解析不报错!于是我去查阅相关资料,也有其他同行这么用. 虽然说这种用法不是 ...

  2. JQuery获取元素的方法总结

    JQuery获取元素的方法总结 一.说明   获取元素的方法分为两种:jQuery选择器.jQuery遍历函数. 做个总结,巩固下知识. 二.获取本身 1.只需要一种jQuery选择器   选择器 实 ...

  3. 【鬼脸原创】JQuery获取元素的方法总结

    目录 一.说明 二.获取本身 三.获取同级元素 四.获取父级元素 五.获取子级元素 一.说明   获取元素的方法分为两种:jQuery选择器.jQuery遍历函数. 做个总结,巩固下知识. 二.获取本 ...

  4. selenium获取元素信息方法(转载)

    1.获取当前页面的Url函数 方法:current_url 实例: driver.current_url 2.获取元素坐标 方法:location 解释:首先查找到你要获取元素的,然后调用locati ...

  5. webDriver定位元素的方法

    在UI层面的自动化测试开发中,元素的定位与操作是基础,也是经常遇到的困难所在.webdriver提供了8种定位: 1. id定位:find_element_by_id("id值") ...

  6. javascript获取元素的方法[xyyit]

    1. javascript默认的方法: <div id=”div_id” class=”div_class” name=”div_name”></div> //1. 根据id ...

  7. DOM获取元素的方法

    DOM:document object module 文档对象模型 DOM就是描述整个html页面中节点关系的图谱,如下图. 1,通过ID,获取页面中元素的方法:(上下文必须是document) do ...

  8. JavaScript中的获取元素的方法

    通过id获取元素 document.getElementById(id名字) 通过标签获取元素 document/元素.getElementsByTagName(标签名) 通过css选择器获取元素 d ...

  9. juery获取元素的方法

    1 从集合中通过指定的序号获取元素 html: 复制代码 代码如下: <div> <p>0</p> <p>1</p> <p>2& ...

随机推荐

  1. Python从题目中学习:random() module

    最近在给公司培训Python,布置了一道题: ----------------------------------------------------------------------------- ...

  2. CSS从大图片上截取小图标的操作

    注:图片名称(tabicons.png)每个小图标width:18px;height:18px从左上角坐标为(-0px;-0px;); 例如第一个对号的坐标为(-0px;-0px;)第二个加号的图标为 ...

  3. MessageFormat不支持{

    转自 :http://zqc-0101.iteye.com/blog/1140140 MessageFormat用来格式化一个消息,通常是一个字符串,比如: String str = "I' ...

  4. PCI Express(三) - A story of packets, stack and network

    原文出处:http://www.fpga4fun.com/PCI-Express3.html Packetized transactions PCI express is a serial bus. ...

  5. Windows:文件服务器,访问进去不能查看到完整的文件

    文件服务器,访问进去不能查看到完整的文件:别人访问却可以查看到完整的所有文件 可能是登录的帐号串掉导致,删除文件服务器帐号,重新访问: cmd: net use /delete *

  6. LINQ to SQL快速上手 step by step

    Step1:建立数据库      在使用Linq to Sql前,我们要将相应的数据库建好.在这个Demo中,使用的数据库是SQL Server Express 2005.      我们首先建立一个 ...

  7. PowerDesigner15中定义varbinary(max)列

    PowerDesigner15 概念数据模型(Entity)中要定义数据类型为varbinary(max)的特性(Attribute),应将数据类型(Data Type)选择为other,在代码(Co ...

  8. css3伸缩布局属性总结

    http://www.css88.com/book/css/properties/flex/flex-basis.htm http://c7sky.com/dive-into-flexbox.html ...

  9. IOS设计模式第一篇之MVC

    设计模式的好处:我们可以写出容易理解,重用度很高的代码.降低代码的耦合度,符合软件工程的思想. 设计模式主要分为三类:创造型的:单例和抽象工厂.结构类型的: MVC  Decorator, Adapt ...

  10. zzuliOJ 1904小火山的股票交易;

    #include <stdio.h> #include <algorithm> #include <string.h> using namespace std; # ...