首页 技术 正文
技术 2022年11月20日
0 收藏 924 点赞 3,974 浏览 3813 个字

selenium webdriver学习(四)————定位页面元素

博客分类:

seleniumwebdriver定位页面元素findElementBy 

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的处理。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919