selenium webdriver xpath 定位页面元素

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来定位该元素。

下面的例子定位了class为"username"的li。

Java代码   收藏代码
  1. import org.openqa.selenium.WebDriver;  
  2. import org.openqa.selenium.WebElement;  
  3.   
  4. import org.openqa.selenium.By;  
  5.   
  6. public class ByClassName {  
  7.   
  8.      
  9.     public static void main(String[] args) {  
  10.          WebDriver driver = new FirefoxDriver();  
  11.          driver.get("http://www.51.com");  
  12.          WebElement element = driver.findElement(By.className("username"));  
  13.          System.out.println(element.getTagName());  
  14.   
  15.     }  
  16. }  

 输出结果:

Java代码   收藏代码
  1. li  
 
使用id属性定位

51.com首页的帐号输入框的html代码如下:

Java代码   收藏代码
  1. <input id="passport_51_user" type="text" value="" tabindex="1" title="用户名/彩虹号/邮箱"   
  2. name="passport_51_user">  
 

在下面的例子中我们用id定位这个输入框,并输出其title,借此也可以验证代码是否工作正常。

Java代码   收藏代码
  1. import org.openqa.selenium.By;  
  2. import org.openqa.selenium.WebDriver;  
  3. import org.openqa.selenium.WebElement;  
  4. import org.openqa.selenium.firefox.FirefoxDriver;  
  5.   
  6. public class ByUserId {  
  7.   
  8.     /** 
  9.      * @param args 
  10.      */  
  11.     public static void main(String[] args) {  
  12.         // TODO Auto-generated method stub  
  13.         WebDriver dr = new FirefoxDriver();  
  14.         dr.get("http://www.51.com");  
  15.           
  16.         WebElement element = dr.findElement(By.id("passport_51_user"));  
  17.         System.out.println(element.getAttribute("title"));  
  18.     }  
  19.   
  20. }  

 输出结果:

Java代码   收藏代码
  1. 用户名/彩虹号/邮箱  

 

使用name属性定位

51.com首页的帐号输入框的html代码如下:

Java代码   收藏代码
  1. <input id="passport_51_user" type="text" value="" tabindex="1" title="用户名/彩虹号/邮箱"   
  2. name="passport_51_user">  

使用name定位

Java代码   收藏代码
  1. WebElement e = dr.findElement(By.name("passport_51_user"));  

 

使用css属性定位

51.com首页的帐号输入框的html代码如下:

Java代码   收藏代码
  1. <input id="passport_51_user" type="text" value="" tabindex="1" title="用户名/彩虹号/邮箱"   
  2. name="passport_51_user">  

使用css定位

Java代码   收藏代码
  1. WebElement e1 = dr.findElement(By.cssSelector("#passport_51_user"));  
 
使用其他方式定位

在定位link元素的时候,可以使用link和link_text属性;

另外还可以使用tag_name属性定位任意元素;

 

 

定位多个元素 

上面提到findElements()方法可以返回一个符合条件的元素List组。看下面例子。

Java代码   收藏代码
  1. import java.io.File;  
  2. import java.util.List;  
  3.   
  4. import org.openqa.selenium.By;  
  5. import org.openqa.selenium.WebDriver;  
  6. import org.openqa.selenium.WebElement;  
  7. import org.openqa.selenium.firefox.FirefoxBinary;  
  8. import org.openqa.selenium.firefox.FirefoxDriver;  
  9.   
  10. public class FindElementsStudy {  
  11.   
  12.     /** 
  13.      * @author gongjf 
  14.      */  
  15.     public static void main(String[] args) {  
  16.         WebDriver  driver = new FirefoxDriver();  
  17.         driver.get("http://www.51.com");  
  18.           
  19.         //定位到所有<input>标签的元素,然后输出他们的id  
  20.         List<WebElement> element = driver.findElements(By.tagName("input"));  
  21.         for (WebElement e : element){  
  22.             System.out.println(e.getAttribute("id"));  
  23.         }  
  24.           
  25.         driver.quit();  
  26.     }  
  27. }  

输出结果:

Java代码   收藏代码
  1. passport_cookie_login  
  2. gourl  
  3. passport_login_from  
  4. passport_51_user  
  5. passport_51_password  
  6. passport_qq_login_2  
  7. btn_reg  
  8. passport_51_ishidden  
  9. passport_auto_login  
 

上面的代码返回页面上所有input对象。很简单,没什么可说的。

 

层级定位

层级定位的思想是先定位父元素,然后再从父元素中精确定位出其我们需要选取的子元素。

层级定位一般的应用场景是无法直接定位到需要选取的元素,但是其父元素比较容易定位,通过定位父元素再遍历其子元素选择需要的目标元素,或者需要定位某个元素下所有的子元素。

下面的代码演示了如何使用层级定位class为"login"的div,然后再取得它下面的所有label,并打印出他们的文本

Java代码   收藏代码
  1. import java.io.File;  
  2. import java.util.List;  
  3.   
  4. import org.openqa.selenium.By;  
  5. import org.openqa.selenium.WebDriver;  
  6. import org.openqa.selenium.WebElement;  
  7. import org.openqa.selenium.firefox.FirefoxBinary;  
  8. import org.openqa.selenium.firefox.FirefoxDriver;  
  9.   
  10. public class LayerLocator {  
  11.   
  12.     /** 
  13.      * @author gongjf 
  14.      */  
  15.     public static void main(String[] args) {  
  16.       
  17.         WebDriver  driver = new FirefoxDriver();  
  18.         driver.get("http://www.51.com");  
  19.           
  20.         //定位class为"login"的div,然后再取得它下面的所有label,并打印出他们的值  
  21.         WebElement element = driver.findElement(By.className("login"));  
  22.          List<WebElement> el = element.findElements(By.tagName("label"));  
  23.          for(WebElement e : el)  
  24.         System.out.println(e.getText());  
  25.       
  26.     }  
  27.           
  28. }  

输出结果:

Java代码   收藏代码
  1. 帐号:  
  2. 密码:  
  3. 隐身  
  4. 下次自动登录                                                                                                                                               

 

未完待续

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值