selenium webdriver学习(四)------------定位页面元素(转)
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。
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.By;
- public class ByClassName {
- public static void main(String[] args) {
- WebDriver driver = new FirefoxDriver();
- driver.get("http://www.51.com");
- WebElement element = driver.findElement(By.className("username"));
- System.out.println(element.getTagName());
- }
- }
输出结果:
- li
使用id属性定位
51.com首页的帐号输入框的html代码如下:
- <input id="passport_51_user" type="text" value="" tabindex="1" title="用户名/彩虹号/邮箱"
- name="passport_51_user">
在下面的例子中我们用id定位这个输入框,并输出其title,借此也可以验证代码是否工作正常。
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.firefox.FirefoxDriver;
- public class ByUserId {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- WebDriver dr = new FirefoxDriver();
- dr.get("http://www.51.com");
- WebElement element = dr.findElement(By.id("passport_51_user"));
- System.out.println(element.getAttribute("title"));
- }
- }
输出结果:
- 用户名/彩虹号/邮箱
使用name属性定位
51.com首页的帐号输入框的html代码如下:
- <input id="passport_51_user" type="text" value="" tabindex="1" title="用户名/彩虹号/邮箱"
- name="passport_51_user">
使用name定位
- WebElement e = dr.findElement(By.name("passport_51_user"));
使用css属性定位
51.com首页的帐号输入框的html代码如下:
- <input id="passport_51_user" type="text" value="" tabindex="1" title="用户名/彩虹号/邮箱"
- name="passport_51_user">
使用css定位
- WebElement e1 = dr.findElement(By.cssSelector("#passport_51_user"));
使用其他方式定位
在定位link元素的时候,可以使用link和link_text属性;
另外还可以使用tag_name属性定位任意元素;
定位多个元素
上面提到findElements()方法可以返回一个符合条件的元素List组。看下面例子。
- import java.io.File;
- import java.util.List;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.firefox.FirefoxBinary;
- import org.openqa.selenium.firefox.FirefoxDriver;
- public class FindElementsStudy {
- /**
- * @author gongjf
- */
- public static void main(String[] args) {
- WebDriver driver = new FirefoxDriver();
- driver.get("http://www.51.com");
- //定位到所有<input>标签的元素,然后输出他们的id
- List<WebElement> element = driver.findElements(By.tagName("input"));
- for (WebElement e : element){
- System.out.println(e.getAttribute("id"));
- }
- driver.quit();
- }
- }
输出结果:
- passport_cookie_login
- gourl
- passport_login_from
- passport_51_user
- passport_51_password
- passport_qq_login_2
- btn_reg
- passport_51_ishidden
- passport_auto_login
上面的代码返回页面上所有input对象。很简单,没什么可说的。
层级定位
层级定位的思想是先定位父元素,然后再从父元素中精确定位出其我们需要选取的子元素。
层级定位一般的应用场景是无法直接定位到需要选取的元素,但是其父元素比较容易定位,通过定位父元素再遍历其子元素选择需要的目标元素,或者需要定位某个元素下所有的子元素。
下面的代码演示了如何使用层级定位class为"login"的div,然后再取得它下面的所有label,并打印出他们的文本
- import java.io.File;
- import java.util.List;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.firefox.FirefoxBinary;
- import org.openqa.selenium.firefox.FirefoxDriver;
- public class LayerLocator {
- /**
- * @author gongjf
- */
- public static void main(String[] args) {
- WebDriver driver = new FirefoxDriver();
- driver.get("http://www.51.com");
- //定位class为"login"的div,然后再取得它下面的所有label,并打印出他们的值
- WebElement element = driver.findElement(By.className("login"));
- List<WebElement> el = element.findElements(By.tagName("label"));
- for(WebElement e : el)
- System.out.println(e.getText());
- }
- }
输出结果:
- 帐号:
- 密码:
- 隐身
- 下次自动登录
定位页面元素over了,下次写一下对frame的处理。
selenium webdriver学习(四)------------定位页面元素(转)的更多相关文章
- selenium webdriver学习-怎么等待页面元素加载完成
http://blog.csdn.net/aerchi/article/details/8055913 WebDriverWait类和ExpectedCondition
- selenium webdriver 学习笔记(二)
selenium webdriver 一.定位一组元素: webdriver可以很方便的使用findElement 方法来定位某个物定的对象.不过有时候我们却要定位一组对象,这时候就需要使用findE ...
- Selenium webdriver 学习总结-元素定位
Selenium webdriver 学习总结-元素定位 webdriver提供了丰富的API,有多种定位策略:id,name,css选择器,xpath等,其中css选择器定位元素效率相比xpath要 ...
- (java)selenium webdriver学习--通过id、name定位,输入内容,搜索,关闭操作、通过tagname查找元素
selenium webdriver学习--通过id.name定位,输入内容,搜索,关闭操作:通过tagname查找元素 打开谷歌浏览器,输入不同的网站,搜索框的定位含有不同元素(有时为id,有时为n ...
- selenium第三课(selenium八种定位页面元素方法)
selenium webdriver进行元素定位时,通过seleniumAPI官方介绍,获取页面元素的方式一共有以下八种方式,现按照常用→不常用的顺序分别介绍一下. 官方api地址:https://s ...
- (java)selenium webdriver学习---实现简单的翻页,将页面内容的标题和标题链接取出
selenium webdriver学习---实现简单的翻页,将页面内容的标题和标题链接取出: 该情况适合能能循环page=1~n,并且每个网页随着循环可以打开的情况, 注意一定是自己拼接的url可以 ...
- Python+Selenium自动化-定位页面元素的八种方法
Python+Selenium自动化-定位页面元素的八种方法 本篇文字主要学习selenium定位页面元素的集中方法,以百度首页为例子. 0.元素定位方法主要有: id定位:find_elemen ...
- webdriver定位页面元素时使用set_page_load_time()和JavaScript停止页面加载
webdriver定位页面元素时使用set_page_load_time()和JavaScript停止页面加载 原文:https://my.oschina.net/u/2344787/blog/400 ...
- selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面(转)
selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面 博客分类: Selenium-webdriver 元素拖放drag and drop Q群里 ...
随机推荐
- @Service ,@Controller,@Component注解
首先,在applicationContext.xml文件中加一行: <context:component-scan base-package="com.hzhi.clas"/ ...
- 定时任务 $ ls /etc/cron* + cat$ for user in $(cat /etc/passwd | cut -f1 -d:); do crontab -l -u $user; done
是否有某个定时任务运行过于频繁? 是否有些用户提交了隐藏的定时任务? 在出现故障的时候,是否正好有某个备份任务在执行?
- PHP 学习1.4
1.session and cookie 示列: <?phpsession_start();?><!DOCTYPE html PUBLIC "-//W3C//DTD XHT ...
- ylbtech-自信:自信
ylbtech-自信:自信 自信心(confidence),在心理学中,与其最接近的是班杜拉(A.Bandura)在社会学习理论中提出的自我效能感 (self-efficacy)的概念,是指个体对自身 ...
- 【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 ...
- WPF:数据绑定总结(1) https://segmentfault.com/a/1190000012981745
WPF:数据绑定总结(1) visual-studio c# 1.3k 次阅读 · 读完需要 16 分钟 0 一.概念:什么是数据绑定? WPF中的数据绑定:是在应用程序 UI 与业务逻辑之间建立 ...
- HTML 实体字符
有些字符,像(<)这类的,对HTML来说是有特殊意义的,所以这些字符是不允许在文本中使用的.要在HTML中显示(<)这个字符,我们就必须使用实体字符. 实体字符 有一些字符对HTML来讲是 ...
- 关于chrome浏览器的帐号密码自动填充以及出现的黄色背景色填充问题
不知道大家平时做项目的时候有木有关注这个问题,其实之前做项目遇到过类似的问题,但是因为是单独的chrome浏览器的填充,而且是样式问题稍微严重点,也就没在意.然而在近期的项目中有遇到了这个问题,最为一 ...
- 利用Eclipse进行远程Debug
这项功能真的十分赞,当我不想写junit test,又想调试在实际环境中才能起作用的Java程序,远程debug真的是太好用了. 参数:java -jar -Xdebug -Xrunjdwp:tran ...
- CodePlus2017 12月月赛 div2可做题2
11月的月赛错过了,来打12月月赛,由于很(zi)想(ji)拿(tai)衣(ruo)服(la),所以去打div2. T1是一个sb模拟,但是机房全卡死在这道语文题上了,基本上弄了一个半小时,T2可以秒 ...