selenium-webdriver提供了强大的元素定位方法,支持以下三种方法。

  • 单个对象的定位方法
  • 多个对象的定位方法
  • 层级定位

定位单个元素

在定位单个元素时,selenium-webdriver提示了如下一些方法对元素进行定位。

  • By.className(className))
  • By.cssSelector(selector)
  • By.id(id)
  • By.linkText(linkText)
  • By.name(name)
  • By.partialLinkText(linkText)
  • By.tagName(name)
  • By.xpath(xpathExpression)

注意:selenium-webdriver通过findElement()\findElements()等find方法调用"By"对象来定位和查询元素。By类只是提供查询的方式进行分类。findElement返回一个元素对象否则抛出异常,findElements返回符合条件的元素List,如果不存在符合条件的就返回一个空的list。

使用className进行定位

当所定位的元素具有class属性的时候我们可以通过classname来定位该元素。

下面的例子定位了51.com首页上class为"username"的li。

  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.WebElement;
  3. import org.openqa.selenium.By;
  4. public class ByClassName {
  5. public static void main(String[] args) {
  6. WebDriver driver = new FirefoxDriver();
  7. driver.get("http://www.51.com");
  8. WebElement element = driver.findElement(By.className("username"));
  9. System.out.println(element.getTagName());
  10. }
  11. }

输出结果:

  1. li

使用id属性定位

51.com首页的帐号输入框的html代码如下:

  1. <input id="passport_51_user" type="text" value="" tabindex="1" title="用户名/彩虹号/邮箱"
  2. name="passport_51_user">

在下面的例子中我们用id定位这个输入框,并输出其title,借此也可以验证代码是否工作正常。

  1. import org.openqa.selenium.By;
  2. import org.openqa.selenium.WebDriver;
  3. import org.openqa.selenium.WebElement;
  4. import org.openqa.selenium.firefox.FirefoxDriver;
  5. public class ByUserId {
  6. /**
  7. * @param args
  8. */
  9. public static void main(String[] args) {
  10. // TODO Auto-generated method stub
  11. WebDriver dr = new FirefoxDriver();
  12. dr.get("http://www.51.com");
  13. WebElement element = dr.findElement(By.id("passport_51_user"));
  14. System.out.println(element.getAttribute("title"));
  15. }
  16. }

输出结果:

  1. 用户名/彩虹号/邮箱

使用name属性定位

51.com首页的帐号输入框的html代码如下:

  1. <input id="passport_51_user" type="text" value="" tabindex="1" title="用户名/彩虹号/邮箱"
  2. name="passport_51_user">

使用name定位

  1. WebElement e = dr.findElement(By.name("passport_51_user"));

使用css属性定位

51.com首页的帐号输入框的html代码如下:

  1. <input id="passport_51_user" type="text" value="" tabindex="1" title="用户名/彩虹号/邮箱"
  2. name="passport_51_user">

使用css定位

  1. WebElement e1 = dr.findElement(By.cssSelector("#passport_51_user"));

使用其他方式定位

在定位link元素的时候,可以使用link和link_text属性;

另外还可以使用tag_name属性定位任意元素;

定位多个元素

上面提到findElements()方法可以返回一个符合条件的元素List组。看下面例子。

  1. import java.io.File;
  2. import java.util.List;
  3. import org.openqa.selenium.By;
  4. import org.openqa.selenium.WebDriver;
  5. import org.openqa.selenium.WebElement;
  6. import org.openqa.selenium.firefox.FirefoxBinary;
  7. import org.openqa.selenium.firefox.FirefoxDriver;
  8. public class FindElementsStudy {
  9. /**
  10. * @author gongjf
  11. */
  12. public static void main(String[] args) {
  13. WebDriver  driver = new FirefoxDriver();
  14. driver.get("http://www.51.com");
  15. //定位到所有<input>标签的元素,然后输出他们的id
  16. List<WebElement> element = driver.findElements(By.tagName("input"));
  17. for (WebElement e : element){
  18. System.out.println(e.getAttribute("id"));
  19. }
  20. driver.quit();
  21. }
  22. }

输出结果:

  1. passport_cookie_login
  2. gourl
  3. passport_login_from
  4. passport_51_user
  5. passport_51_password
  6. passport_qq_login_2
  7. btn_reg
  8. passport_51_ishidden
  9. passport_auto_login

上面的代码返回页面上所有input对象。很简单,没什么可说的。

层级定位

层级定位的思想是先定位父元素,然后再从父元素中精确定位出其我们需要选取的子元素。

层级定位一般的应用场景是无法直接定位到需要选取的元素,但是其父元素比较容易定位,通过定位父元素再遍历其子元素选择需要的目标元素,或者需要定位某个元素下所有的子元素。

下面的代码演示了如何使用层级定位class为"login"的div,然后再取得它下面的所有label,并打印出他们的文本

  1. import java.io.File;
  2. import java.util.List;
  3. import org.openqa.selenium.By;
  4. import org.openqa.selenium.WebDriver;
  5. import org.openqa.selenium.WebElement;
  6. import org.openqa.selenium.firefox.FirefoxBinary;
  7. import org.openqa.selenium.firefox.FirefoxDriver;
  8. public class LayerLocator {
  9. /**
  10. * @author gongjf
  11. */
  12. public static void main(String[] args) {
  13. WebDriver  driver = new FirefoxDriver();
  14. driver.get("http://www.51.com");
  15. //定位class为"login"的div,然后再取得它下面的所有label,并打印出他们的值
  16. WebElement element = driver.findElement(By.className("login"));
  17. List<WebElement> el = element.findElements(By.tagName("label"));
  18. for(WebElement e : el)
  19. System.out.println(e.getText());
  20. }
  21. }

输出结果:

  1. 帐号:
  2. 密码:
  3. 隐身
  4. 下次自动登录

定位页面元素over了,下次写一下对frame的处理。

selenium webdriver学习(四)------------定位页面元素(转)的更多相关文章

  1. selenium webdriver学习-怎么等待页面元素加载完成

    http://blog.csdn.net/aerchi/article/details/8055913 WebDriverWait类和ExpectedCondition

  2. selenium webdriver 学习笔记(二)

    selenium webdriver 一.定位一组元素: webdriver可以很方便的使用findElement 方法来定位某个物定的对象.不过有时候我们却要定位一组对象,这时候就需要使用findE ...

  3. Selenium webdriver 学习总结-元素定位

    Selenium webdriver 学习总结-元素定位 webdriver提供了丰富的API,有多种定位策略:id,name,css选择器,xpath等,其中css选择器定位元素效率相比xpath要 ...

  4. (java)selenium webdriver学习--通过id、name定位,输入内容,搜索,关闭操作、通过tagname查找元素

    selenium webdriver学习--通过id.name定位,输入内容,搜索,关闭操作:通过tagname查找元素 打开谷歌浏览器,输入不同的网站,搜索框的定位含有不同元素(有时为id,有时为n ...

  5. selenium第三课(selenium八种定位页面元素方法)

    selenium webdriver进行元素定位时,通过seleniumAPI官方介绍,获取页面元素的方式一共有以下八种方式,现按照常用→不常用的顺序分别介绍一下. 官方api地址:https://s ...

  6. (java)selenium webdriver学习---实现简单的翻页,将页面内容的标题和标题链接取出

    selenium webdriver学习---实现简单的翻页,将页面内容的标题和标题链接取出: 该情况适合能能循环page=1~n,并且每个网页随着循环可以打开的情况, 注意一定是自己拼接的url可以 ...

  7. Python+Selenium自动化-定位页面元素的八种方法

    Python+Selenium自动化-定位页面元素的八种方法   本篇文字主要学习selenium定位页面元素的集中方法,以百度首页为例子. 0.元素定位方法主要有: id定位:find_elemen ...

  8. webdriver定位页面元素时使用set_page_load_time()和JavaScript停止页面加载

    webdriver定位页面元素时使用set_page_load_time()和JavaScript停止页面加载 原文:https://my.oschina.net/u/2344787/blog/400 ...

  9. selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面(转)

    selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面 博客分类: Selenium-webdriver 元素拖放drag and drop  Q群里 ...

随机推荐

  1. @Service ,@Controller,@Component注解

    首先,在applicationContext.xml文件中加一行: <context:component-scan base-package="com.hzhi.clas"/ ...

  2. 定时任务 $ ls /etc/cron* + cat$ for user in $(cat /etc/passwd | cut -f1 -d:); do crontab -l -u $user; done

    是否有某个定时任务运行过于频繁? 是否有些用户提交了隐藏的定时任务? 在出现故障的时候,是否正好有某个备份任务在执行?

  3. PHP 学习1.4

    1.session and cookie 示列: <?phpsession_start();?><!DOCTYPE html PUBLIC "-//W3C//DTD XHT ...

  4. ylbtech-自信:自信

    ylbtech-自信:自信 自信心(confidence),在心理学中,与其最接近的是班杜拉(A.Bandura)在社会学习理论中提出的自我效能感 (self-efficacy)的概念,是指个体对自身 ...

  5. 【eclipse】解决:eclipse或STS运行maven工程出现Missing artifact jdk.tools:jdk.tools:jar:1.7问题

    eclipse或STS运行maven工程出现Missing artifact jdk.tools:jdk.tools:jar:1.7问题 最近项目中使用到大数据平台,代码中应用了hbase-clien ...

  6. WPF:数据绑定总结(1) https://segmentfault.com/a/1190000012981745

    WPF:数据绑定总结(1) visual-studio c# 1.3k 次阅读  ·  读完需要 16 分钟 0 一.概念:什么是数据绑定? WPF中的数据绑定:是在应用程序 UI 与业务逻辑之间建立 ...

  7. HTML 实体字符

    有些字符,像(<)这类的,对HTML来说是有特殊意义的,所以这些字符是不允许在文本中使用的.要在HTML中显示(<)这个字符,我们就必须使用实体字符. 实体字符 有一些字符对HTML来讲是 ...

  8. 关于chrome浏览器的帐号密码自动填充以及出现的黄色背景色填充问题

    不知道大家平时做项目的时候有木有关注这个问题,其实之前做项目遇到过类似的问题,但是因为是单独的chrome浏览器的填充,而且是样式问题稍微严重点,也就没在意.然而在近期的项目中有遇到了这个问题,最为一 ...

  9. 利用Eclipse进行远程Debug

    这项功能真的十分赞,当我不想写junit test,又想调试在实际环境中才能起作用的Java程序,远程debug真的是太好用了. 参数:java -jar -Xdebug -Xrunjdwp:tran ...

  10. CodePlus2017 12月月赛 div2可做题2

    11月的月赛错过了,来打12月月赛,由于很(zi)想(ji)拿(tai)衣(ruo)服(la),所以去打div2. T1是一个sb模拟,但是机房全卡死在这道语文题上了,基本上弄了一个半小时,T2可以秒 ...