selenium-webdriver提供了强大的元素定位方法,支持以下三种方法。
<!--[if !supportLists]-->· <!--[endif]-->单个对象的定位方法
<!--[if !supportLists]-->· <!--[endif]-->多个对象的定位方法
<!--[if !supportLists]-->· <!--[endif]-->层级定位
定位单个元素
在定位单个元素时,selenium-webdriver提示了如下一些方法对元素进行定位。
<!--[if !supportLists]-->· <!--[endif]--> By.className(className))
<!--[if !supportLists]-->· <!--[endif]--> By.cssSelector(selector)
<!--[if !supportLists]-->· <!--[endif]--> By.id(id)
<!--[if !supportLists]-->· <!--[endif]--> By.linkText(linkText)
<!--[if !supportLists]-->· <!--[endif]--> By.name(name)
<!--[if !supportLists]-->· <!--[endif]--> By.partialLinkText(linkText)
<!--[if !supportLists]-->· <!--[endif]--> By.tagName(name)
<!--[if !supportLists]-->· <!--[endif]--> By.xpath(xpathExpression)
注意:selenium-webdriver通过findElement()\findElements()等find方法调用"By"对象来定位和查询元素。By类只是提供查询的方式进行分类。findElement返回一个元素对象否则抛出异常,findElements返回符合条件的元素List,如果不存在符合条件的就返回一个空的list。
使用className进行定位
当所定位的元素具有class属性的时候我们可以通过classname来定位该元素。
下面的例子定位了51.com首页上class为"username"的li。
Java代码
<!--[if !supportLists]-->1. <!--[endif]-->import org.openqa.selenium.WebDriver;
<!--[if !supportLists]-->2. <!--[endif]-->import org.openqa.selenium.WebElement;
<!--[if !supportLists]-->3. <!--[endif]-->
<!--[if !supportLists]-->4. <!--[endif]-->import org.openqa.selenium.By;
<!--[if !supportLists]-->5. <!--[endif]-->
<!--[if !supportLists]-->6. <!--[endif]-->public class ByClassName {
<!--[if !supportLists]-->7. <!--[endif]-->
<!--[if !supportLists]-->8. <!--[endif]-->
<!--[if !supportLists]-->9. <!--[endif]--> public static void main(String[] args) {
<!--[if !supportLists]-->10.<!--[endif]--> WebDriver driver = new FirefoxDriver();
<!--[if !supportLists]-->11.<!--[endif]--> driver.get("http://www.51.com");
<!--[if !supportLists]-->12.<!--[endif]--> WebElement element = driver.findElement(By.className("username"));
<!--[if !supportLists]-->13.<!--[endif]--> System.out.println(element.getTagName());
<!--[if !supportLists]-->14.<!--[endif]-->
<!--[if !supportLists]-->15.<!--[endif]--> }
<!--[if !supportLists]-->16.<!--[endif]-->}
输出结果:
Java代码
<!--[if !supportLists]-->1. <!--[endif]-->li
使用id属性定位
51.com首页的帐号输入框的html代码如下:
Java代码
<!--[if !supportLists]-->1. <!--[endif]--><input id="passport_51_user" type="text" value="" tabindex="1" title="用户名/彩虹号/邮箱"
<!--[if !supportLists]-->2. <!--[endif]-->name="passport_51_user">
在下面的例子中我们用id定位这个输入框,并输出其title,借此也可以验证代码是否工作正常。
Java代码
<!--[if !supportLists]-->1. <!--[endif]-->import org.openqa.selenium.By;
<!--[if !supportLists]-->2. <!--[endif]-->import org.openqa.selenium.WebDriver;
<!--[if !supportLists]-->3. <!--[endif]-->import org.openqa.selenium.WebElement;
<!--[if !supportLists]-->4. <!--[endif]-->import org.openqa.selenium.firefox.FirefoxDriver;
<!--[if !supportLists]-->5. <!--[endif]-->
<!--[if !supportLists]-->6. <!--[endif]-->public class ByUserId {
<!--[if !supportLists]-->7. <!--[endif]-->
<!--[if !supportLists]-->8. <!--[endif]-->
<!--[if !supportLists]-->9. <!--[endif]-->
<!--[if !supportLists]-->10.<!--[endif]-->
<!--[if !supportLists]-->11.<!--[endif]--> public static void main(String[] args) {
<!--[if !supportLists]-->12.<!--[endif]-->
<!--[if !supportLists]-->13.<!--[endif]--> WebDriver dr = new FirefoxDriver();
<!--[if !supportLists]-->14.<!--[endif]--> dr.get("http://www.51.com");
<!--[if !supportLists]-->15.<!--[endif]-->
<!--[if !supportLists]-->16.<!--[endif]--> WebElement element = dr.findElement(By.id("passport_51_user"));
<!--[if !supportLists]-->17. body <>