定位单个元素
在定位单个元素时,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来定位该元素。
下面的例子定位了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());  
 - 
  
 - 
    }  
 - 
}  
 
 
 输出结果:
 
使用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 {  
 - 
  
 - 
     
 - 
 
 - 
  
 - 
    public static void main(String[] args) {  
 - 
          
 - 
        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 {  
 - 
  
 - 
     
 - 
 
 - 
  
 - 
    public static void main(String[] args) {  
 - 
        WebDriver  driver = new FirefoxDriver();  
 - 
        driver.get("http://www.51.com");  
 - 
          
 - 
          
 - 
        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 {  
 - 
  
 - 
     
 - 
 
 - 
  
 - 
    public static void main(String[] args) {  
 - 
      
 - 
        WebDriver  driver = new FirefoxDriver();  
 - 
        driver.get("http://www.51.com");  
 - 
          
 - 
          
 - 
        WebElement element = driver.findElement(By.className("login"));  
 - 
         List<WebElement> el = element.findElements(By.tagName("label"));  
 - 
         for(WebElement e : el)  
 - 
        System.out.println(e.getText());  
 - 
      
 - 
    }  
 - 
          
 - 
}  
 
 
输出结果:
 
未完待续