原文地址:http://www.seleniumhq.org/docs/03_webdriver.jsp#selenium-webdriver-api-commands-and-operations
(本文只针对python部分翻译)
首先申明一点:本人英语水平很烂,做此翻译仅仅用于更深入地去理解webdriver的使用方法。
一、获取页面
使用WebDriver的第一步是打开一个页面,通常调用get来做这件事:
driver.get("http://www.google.com");包括操作系统/浏览器等诸多因素,WebDriver不会自行等待页面加载。某些情况下,在页面还没有完全加载出来前,WebDriver可能已经失去控制了。为了确保稳定性,可使用“显式等待”和“隐式等待”,等待所需要定位的元素出现在页面再进行操作。
显式等待:
隐式等待:
二、定位元素
在WebDriver中定位元素有两种方式:通过创建实例或者页面元素。每一种语言都有“Find Element”和“Find Elements”两种方法。前者返回的是与查询对应的元素对象,如果该元素没有发现,则抛出异常。后者返回一个多个页面元素的列表,如果没有匹配到查询的DOM元素,该列表为空。
Find方法有一个By定位对象,下面列出By的使用方法:
1、By ID
这是定位元素最高效也是首选的方法,常见的问题是UI开发者做了不唯一或者动态变化的id,这两种方式都应当避免,为元素设置一个类都比动态id更合适。
例子如下:
HTML:
<div id="coolestWidgetEvah">...</div>Python代码:
element = driver.find_element_by_id("coolestWidgetEvah") or from selenium.webdriver.common.by import By element = driver.find_element(by=By.ID, value="coolestWidgetEvah")
2、By Class Name
本案例中,类即为DOM元素的属性,通常很多DOM元素的类名是相同的,因此如果查找一组相同类名的元素,该方法成了更合适的选择。
例子如下:
HTML:
<div class="cheese"><span>Cheddar</span></div><div class="cheese"><span>Gouda</span></divPython代码:
cheeses = driver.find_elements_by_class_name("cheese") or from selenium.webdriver.common.by import By cheeses = driver.find_elements(By.CLASS_NAME, "cheese")
3、By Tag Name
即,DOM元素的标签名
例子如下:
HTML:
<iframe src="..."></iframe>Python代码:
frame = driver.find_element_by_tag_name("iframe") or from selenium.webdriver.common.by import By frame = driver.find_element(By.TAG_NAME, "iframe")4、By Name
通过匹配元素名称属性查找元素
例子如下:
HTML:
<input name="cheese" type="text"/>Python代码:
cheese = driver.find_element_by_name("cheese") or from selenium.webdriver.common.by import By cheese = driver.find_element(By.NAME, "cheese")5、By Link Text
通过可见文本查找超链接元素
例子如下:
HTML:
<a href="http://www.google.com/search?q=cheese">cheese</a>>Python代码:
cheese = driver.find_element_by_link_text("cheese") or from selenium.webdriver.common.by import By cheese = driver.find_element(By.LINK_TEXT, "cheese")6、By Partial Link Text
通过可见文本模糊查找超链接元素
例子如下:
HTML:
<a href="http://www.google.com/search?q=cheese">search for cheese</a>>
Python代码:
cheese = driver.find_element_by_partial_link_text("cheese") or from selenium.webdriver.common.by import By cheese = driver.find_element(By.PARTIAL_LINK_TEXT, "cheese")7、By CSS
顾名思义,它是CSS的一种定位策略。通常本地浏览器都支持,请通过w3c css selectors获取常用CSS选择器的列表清单,如果浏览器不支持CSS查询,请使用Sizzle。IE 6、7和ff3.0目前使用Sizzle作为css查询引擎。
注意,每个浏览器不同,对应CSS的版本也不同。
例子如下:
HTML:
<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</Python:
cheese = driver.find_element_by_css_selector("#food span.dairy.aged") or from selenium.webdriver.common.by import By cheese = driver.find_element(By.CSS_SELECTOR, "#food span.dairy.aged")
8、By Xpath
WebDriver可以随时随地使用Xpath功能,如果浏览器不支持Xpath,而我们又这样做的话,将会导致意想不到的错误,除非你懂得不同的XPath引擎之间的差异。
标签和属性名 | 属性 | 本地是否支持Xpath | |
---|---|---|---|
HtmlUnit Driver | Lower-cased | 出现在HTML | 是 |
Internet Explorer Driver | Lower-cased | 出现在HTML | 否 |
Firefox Driver | 不区分大小写 | 出现在HTML | 是 |
HTML:
<input type="text" name="example" /> <INPUT type="text" name="other" />Python代码:
inputs = driver.find_elements_by_xpath("//input") or from selenium.webdriver.common.by import By inputs = driver.find_elements(By.XPATH, "//input")匹配到的结果(数量)如下:
XPath 表达式 | HtmlUnit Driver | Firefox Driver | Internet Explorer Driver |
---|---|---|---|
//input | 1 | 2 | 2 |
//INPUT | 0 | 2 | 0 |
说明:翻译中的css与xpath讲的很抽象,看的我糊里糊涂的,其实真实操作中并没有这么复杂