- 浏览: 312387 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
yan372397390:
请问这几行命令是在哪里输入的
Genymotion模拟器安装Genymotion-ARM-Translation变copy的解决办法 -
littlevine:
...
"WebDriverException: Cannot find firefox binary in PATH."的解决方法 -
jujis008:
楼主, 这selenium用的是jdk自带的log,所以在% ...
PhantomJSDriver怎么设置在console里不输出运行信息 -
qi_ling2005:
OnTheRoad_lee 写道 String[] phant ...
PhantomJSDriver怎么设置在console里不输出运行信息 -
OnTheRoad_lee:
String[] phantomArgs = new Str ...
PhantomJSDriver怎么设置在console里不输出运行信息
selenium webdriver学习(四)------------定位页面元素
- 博客分类:
- 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 github doc
2015-07-21 16:26 1085selenium 官网被墙, 看不了doc, 可以去下面这个 ... -
selenium webdriver学习(二十二)------------XVFB实现selenium在linux上无界面运行安装篇
2015-03-12 12:25 13837selenium在linux上无界面 ... -
PhantomJSDriver怎么设置在console里不输出运行信息
2014-11-10 15:50 2364每次运行PhantomJSDriver都输出一堆的INFO、 ... -
selenium webdriver 常见问题
2012-07-10 18:31 0注我下面说到的webdriver就是指selenium web ... -
selenium和webdriver合并的原因
2012-07-04 10:48 3374selenium和webdriver合并的原因,传送门 -
selenium webdriver学习(二十一)------------Selenium Grid深入学习
2012-06-29 18:35 4831应网友要求写一个用Selenium Grid控制多系统多浏览器 ... -
selenium webdriver学习(二十)------------Selenium Grid
2012-06-27 18:37 5836Selenium Grid允许同时并行 ... -
selenium webdriver学习(十九)-------我们的构建文件CrazyFunBuild (译)
2012-06-14 10:50 2284原文:CrazyFunBuild ... -
selenium webdriver学习(十八)----------构建webdriver
2012-05-22 16:03 4362准备环境 对所有版 ... -
selenium webdriver学习(十七)----------把selenium项目同步到本地eclipse
2012-05-11 16:00 3981这里主要是想把selenium的整个项目同步到eclipse的 ... -
selenium webdriver学习(十六)----------用selenium webdriver实现selenium RC中的类似的方法
2012-05-11 15:35 4765最近想总结一下学习selenium webdriver的情况, ... -
selenium webdriver学习(十五)------------如何处理FirefoxProfile
2012-04-10 18:29 9058这一节主要涉及 selenium webdriver处理Fir ... -
selenium webdriver学习(十四)------------如何处理table
2012-04-07 09:15 7441以前在selenium RC 里面有一个getTable方法, ... -
selenium webdriver学习(十三)------------如何利用Actions类模拟鼠标和键盘的操作
2012-03-29 12:36 12036在selenium webdriver学习(十)------- ... -
selenium webdriver学习(十二)------------如何利用selenium-webdriver截图
2012-03-26 16:29 5878在自动化测试中常常会用到截图功能。最近用了一下selenium ... -
学习selenium-webdriver一些网站
2012-03-20 16:49 0http://www.aosabook.org/en/sele ... -
selenium webdriver学习(十一)------------如何等待页面元素加载完成
2012-03-14 18:25 15478web的自动化测试中,我们经常会遇到这样一种情况:当我们的程 ... -
selenium webdriver学习(十)------------如何把一个元素拖放到另一个元素里面
2012-03-13 13:09 5900Q群里有时候会有人问,selenium webdriver怎 ... -
selenium webdriver学习(九)------------如何操作cookies
2012-03-12 17:45 5209Web 测试中我们经常会接触到Cookies,一个Cookie ... -
selenium webdriver学习(八)------------如何操作select下拉框
2012-03-12 16:06 10197下面我们来看一下selenium webdriver是如何来处 ...
相关推荐
通过深入研究这个压缩包中的代码示例,学习者不仅可以理解Selenium WebDriver的工作原理,还能掌握如何在实际项目中应用这些知识,提升自动化测试的技能。同时,这也有助于理解和调试测试脚本,解决在自动化测试过程...
1.4 selenium webdriver学习(四)------------定位页面元素 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1.5 selenium webdriver学习(五)------------iframe的处理 . . . . . . . . . ....
在本文档中,我们学习了如何使用 Python 和 Selenium WebDriver 实现自动化测试,学习了元素定位的多种方式,包括使用浏览器工具、findElement 方法、findElements 方法等,并了解了多种定位策略。这些技术将帮助...
Selenium WebDriver 中使用 By.Xpath 快速定位页面元素 Selenium WebDriver 是一个自动化测试工具,可以模拟用户交互来对 Web 应用程序进行测试。其中,定位页面元素是自动化测试的关键步骤。By.Xpath 是一种快速...
全书共分为四个部分:第1部分基础篇主要讲解自动化测试相关的基础理论、WebDriver 环境安装、单元测试工具的使用方法以及 WebDrvier的入门使用实例,第2部分实战应用篇基于丰富的实战案例讲解页面元素的定位方法以及...
### Selenium WebDriver 学习总结——元素定位技巧详解 在自动化测试领域,Selenium WebDriver 已成为业界标准之一,尤其在 Web 应用测试方面表现突出。本文将详细探讨 WebDriver 提供的各种元素定位策略及其应用...
此外,还能处理异步加载的页面元素,等待特定元素出现或消失,确保测试的准确性。 6. **异常处理**: 在测试过程中,WebDriver 会捕获并处理可能出现的错误和异常,例如找不到元素、超时等,这有助于调试和优化...
**Selenium WebDriver API...综上所述,`selenium-API-2.12.chm`文档是学习和掌握Selenium WebDriver API的重要资源,它涵盖了从基本操作到高级特性的全面内容,对于任何进行Web自动化测试的人来说都是不可或缺的参考。
Ruby 和 Selenium-...这个脚本可能展示了如何初始化 WebDriver,定位页面元素,执行各种操作,以及验证预期结果。通过学习和理解这个脚本,你可以进一步提升在 Ruby 和 Selenium-Webdriver 结合使用时的测试能力。
4. **元素定位**:使用XPath、CSS选择器等方法智能地定位页面元素,减少因DOM结构变化导致的脚本失效问题。 5. **兼容性**:支持多种主流浏览器,如Chrome、Firefox、Edge等,确保在不同环境下测试的一致性。 6. **...
Selenium-java包含了各种元素定位策略、等待机制、页面对象模型等功能,使得编写测试代码更为便捷。虽然在某些简单的情况下,不使用Selenium-java也能运行测试(通过直接调用selenium-server-standalone.jar提供的...
#### 四、元素定位 - **3.1 id 和 name 定位**: - 使用 `find_element_by_id` 或 `find_element_by_name` 方法定位元素。 - **3.2 tagname 和 classname 定位**: - `find_element_by_tag_name` 和 `find_element_...
3. **元素定位**:讲解Selenium中用于查找网页元素的各种方法,如`find_element_by_id`、`find_element_by_name`、`find_elements_by_class_name`等,以及XPath和CSS选择器的使用。 4. **页面操作**:通过实例展示...
这些文档通常会详细解释如何初始化 WebDriver 实例,如何创建浏览器会话,如何定位页面元素,以及如何执行点击、输入文本、导航等操作。此外,还可能涵盖错误处理、异步编程、浏览器兼容性等主题。 Selenium ...
4. **WebElement** 和 **Locators**: 这些是用于定位页面元素的关键接口和类。源码中可以了解如何解析 CSS 选择器、XPath 表达式,以及如何执行点击、输入等操作。 5. **Remote WebDriver**: 支持远程执行测试,...
【Selenium WebDriver测试仓库】 Selenium WebDriver 是一个用于自动化浏览器操作的强大工具,广泛应用于Web应用程序的测试。它支持多种编程语言,包括JavaScript,这使得测试脚本可以用开发者熟悉的语言编写,提高...
例如,我们可以创建一个`WebDriver`实例来启动浏览器,然后通过`findElement`方法定位页面上的特定元素,接着使用`click`或`sendKeys`方法与这些元素进行交互。此外,还有`navigate`对象用于页面跳转,`manage`对象...