定位页面元素-方法汇总

http://sariyalee.iteye.com/blog/1697856

webdriver提供了强大的元素定位方法,支持以下三种方法。

单个对象的定位方法
多个对象的定位方法
层级定位

注意:
selenium-webdriver通过findElement()\findElements()等find方法调用"By"对象来定位和查询元素。By类只是提供查询的方式进行分类。findElement返回一个元素对象否则抛出异常,findElements返回符合条件的元素 List,如果不存在符合条件的就返回一个空的list。
1.定位单个对象
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)
1)使用className定位元素
定位的html文件:
Html代码   收藏代码
  1. <form action="/s" name="f">  
  2. <span class="s_ipt_wr">  
  3. <input type="text" class="s_ipt" maxlength="100" id="kw" name="wd" autocomplete="off">  
  4. </span>  
  5. <input type="hidden" value="0" name="rsv_bp"><input type="hidden" value="3" name="rsv_spt">  
  6. <span class="s_btn_wr">  
  7. <input type="submit" onmouseout="this.className='s_btn'" onmousedown="this.className='s_btn s_btn_h'" class="s_btn" id="su" value="百度一下">  
  8. </span>  
  9. <div id="sd_1350115810720" style="display: none;"></div>  
  10. </form>  

Java代码   收藏代码
  1. package selenium.test.googleSearch;  
  2.   
  3. import org.openqa.selenium.By;  
  4. import org.openqa.selenium.WebDriver;  
  5. import org.openqa.selenium.WebElement;  
  6. import org.openqa.selenium.firefox.*;  
  7. public class BaiduFirefoxDriver {  
  8.   
  9.     /** 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         // TODO Auto-generated method stub   
  14.         System.setProperty("webdriver.firefox.bin""D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  15.         WebDriver driver = new FirefoxDriver();  
  16.         //页面跳转  
  17.         driver.get("http://www.baidu.com/");  
  18.         //根据className定位百度查询输入框  
  19.         WebElement search=driver.findElement(By.className("s_ipt"));  
  20.         //输入查询条件classname  
  21.         search.sendKeys("classname");  
  22.     }  
  23.   
  24. }  

2)使用cssSelector定位元素
定位的html文件:
Html代码   收藏代码
  1. <form action="/s" name="f">  
  2. <span class="s_ipt_wr">  
  3. <input type="text" class="s_ipt" maxlength="100" id="kw" name="wd" autocomplete="off">  
  4. </span>  
  5. <input type="hidden" value="0" name="rsv_bp"><input type="hidden" value="3" name="rsv_spt">  
  6. <span class="s_btn_wr">  
  7. <input type="submit" onmouseout="this.className='s_btn'" onmousedown="this.className='s_btn s_btn_h'" class="s_btn" id="su" value="百度一下">  
  8. </span>  
  9. <div id="sd_1350115810720" style="display: none;"></div>  
  10. </form>  

Java代码   收藏代码
  1. package selenium.test.googleSearch;  
  2.   
  3. import org.openqa.selenium.By;  
  4. import org.openqa.selenium.WebDriver;  
  5. import org.openqa.selenium.WebElement;  
  6. import org.openqa.selenium.firefox.*;  
  7. public class BaiduFirefoxDriver {  
  8.   
  9.     /** 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         // TODO Auto-generated method stub   
  14.         System.setProperty("webdriver.firefox.bin""D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  15.         WebDriver driver = new FirefoxDriver();  
  16.         //页面跳转  
  17.         driver.get("http://www.baidu.com/");  
  18.         //根据classSelector定位百度查询输入框  
  19.         WebElement search=driver.findElement(By.cssSelector("#kw"));  
  20.         //输入查询条件classname  
  21.         search.sendKeys("classname");  
  22.     }  
  23.   
  24. }  

3)使用id定位元素
定位的html文件:
Html代码   收藏代码
  1. <form action="/s" name="f">  
  2. <span class="s_ipt_wr">  
  3. <input type="text" class="s_ipt" maxlength="100" id="kw" name="wd" autocomplete="off">  
  4. </span>  
  5. <input type="hidden" value="0" name="rsv_bp"><input type="hidden" value="3" name="rsv_spt">  
  6. <span class="s_btn_wr">  
  7. <input type="submit" onmouseout="this.className='s_btn'" onmousedown="this.className='s_btn s_btn_h'" class="s_btn" id="su" value="百度一下">  
  8. </span>  
  9. <div id="sd_1350115810720" style="display: none;"></div>  
  10. </form>  

Java代码   收藏代码
  1. package selenium.test.googleSearch;  
  2.   
  3. import org.openqa.selenium.By;  
  4. import org.openqa.selenium.WebDriver;  
  5. import org.openqa.selenium.WebElement;  
  6. import org.openqa.selenium.firefox.*;  
  7. public class BaiduFirefoxDriver {  
  8.   
  9.     /** 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         // TODO Auto-generated method stub   
  14.         System.setProperty("webdriver.firefox.bin""D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  15.         WebDriver driver = new FirefoxDriver();  
  16.         //页面跳转  
  17.         driver.get("http://www.baidu.com/");  
  18.         //根据id定位百度查询输入框  
  19.         WebElement search=driver.findElement(By.id("kw"));  
  20.         //输入查询条件classname  
  21.         search.sendKeys("classname");  
  22.     }  
  23.   
  24. }  

4)使用name定位元素
定位的html文件:
Html代码   收藏代码
  1. <form action="/s" name="f">  
  2. <span class="s_ipt_wr">  
  3. <input type="text" class="s_ipt" maxlength="100" id="kw" name="wd" autocomplete="off">  
  4. </span>  
  5. <input type="hidden" value="0" name="rsv_bp"><input type="hidden" value="3" name="rsv_spt">  
  6. <span class="s_btn_wr">  
  7. <input type="submit" onmouseout="this.className='s_btn'" onmousedown="this.className='s_btn s_btn_h'" class="s_btn" id="su" value="百度一下">  
  8. </span>  
  9. <div id="sd_1350115810720" style="display: none;"></div>  
  10. </form>  

Java代码   收藏代码
  1. package selenium.test.googleSearch;  
  2.   
  3. import org.openqa.selenium.By;  
  4. import org.openqa.selenium.WebDriver;  
  5. import org.openqa.selenium.WebElement;  
  6. import org.openqa.selenium.firefox.*;  
  7. public class BaiduFirefoxDriver {  
  8.   
  9.     /** 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         // TODO Auto-generated method stub   
  14.         System.setProperty("webdriver.firefox.bin""D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  15.         WebDriver driver = new FirefoxDriver();  
  16.         //页面跳转  
  17.         driver.get("http://www.baidu.com/");  
  18.         //根据name定位百度查询输入框  
  19.         WebElement search=driver.findElement(By.name("wd"));  
  20.         //输入查询条件classname  
  21.         search.sendKeys("classname");  
  22.     }  
  23.   
  24. }  

5)使用tagname定位元素
定位的html文件:
Html代码   收藏代码
  1. <form action="/s" name="f">  
  2. <span class="s_ipt_wr">  
  3. <input type="text" class="s_ipt" maxlength="100" id="kw" name="wd" autocomplete="off">  
  4. </span>  
  5. <input type="hidden" value="0" name="rsv_bp"><input type="hidden" value="3" name="rsv_spt">  
  6. <span class="s_btn_wr">  
  7. <input type="submit" onmouseout="this.className='s_btn'" onmousedown="this.className='s_btn s_btn_h'" class="s_btn" id="su" value="百度一下">  
  8. </span>  
  9. <div id="sd_1350115810720" style="display: none;"></div>  
  10. </form>  

Java代码   收藏代码
  1. package selenium.test.googleSearch;  
  2.   
  3. import org.openqa.selenium.By;  
  4. import org.openqa.selenium.WebDriver;  
  5. import org.openqa.selenium.WebElement;  
  6. import org.openqa.selenium.firefox.*;  
  7. public class BaiduFirefoxDriver {  
  8.   
  9.     /** 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         // TODO Auto-generated method stub   
  14.         System.setProperty("webdriver.firefox.bin""D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  15.         WebDriver driver = new FirefoxDriver();  
  16.         //页面跳转  
  17.         driver.get("http://www.baidu.com/");  
  18.         //根据tagName定位百度查询输入框  
  19.         WebElement search=driver.findElement(By.tagName("input"));  
  20.         //输入查询条件classname  
  21.         search.sendKeys("classname");  
  22.     }  
  23.   
  24. }  

6)使用xpath定位元素
定位的html文件:
Html代码   收藏代码
  1. <form action="/s" name="f">  
  2. <span class="s_ipt_wr">  
  3. <input type="text" class="s_ipt" maxlength="100" id="kw" name="wd" autocomplete="off">  
  4. </span>  
  5. <input type="hidden" value="0" name="rsv_bp"><input type="hidden" value="3" name="rsv_spt">  
  6. <span class="s_btn_wr">  
  7. <input type="submit" onmouseout="this.className='s_btn'" onmousedown="this.className='s_btn s_btn_h'" class="s_btn" id="su" value="百度一下">  
  8. </span>  
  9. <div id="sd_1350115810720" style="display: none;"></div>  
  10. </form>  

Java代码   收藏代码
  1. package selenium.test.googleSearch;  
  2.   
  3. import org.openqa.selenium.By;  
  4. import org.openqa.selenium.WebDriver;  
  5. import org.openqa.selenium.WebElement;  
  6. import org.openqa.selenium.firefox.*;  
  7. public class BaiduFirefoxDriver {  
  8.   
  9.     /** 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         // TODO Auto-generated method stub   
  14.         System.setProperty("webdriver.firefox.bin""D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  15.         WebDriver driver = new FirefoxDriver();  
  16.         //页面跳转  
  17.         driver.get("http://www.baidu.com/");  
  18.         //根据xpath定位百度查询输入框  
  19.         WebElement search=driver.findElement(By.xpath("//*[@id='kw']"));  
  20.         //输入查询条件classname  
  21.         search.sendKeys("classname");  
  22.     }  
  23.   
  24. }  

7)使用linkText定位元素
定位的html文件:
Html代码   收藏代码
  1. <a href="http://baike.baidu.com" name="tj_baike">百科</a>  

Java代码   收藏代码
  1. package selenium.test.googleSearch;  
  2.   
  3. import org.openqa.selenium.By;  
  4. import org.openqa.selenium.WebDriver;  
  5. import org.openqa.selenium.WebElement;  
  6. import org.openqa.selenium.firefox.*;  
  7. public class BaiduFirefoxDriver {  
  8.   
  9.     /** 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         // TODO Auto-generated method stub   
  14.         System.setProperty("webdriver.firefox.bin""D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  15.         WebDriver driver = new FirefoxDriver();  
  16.         //页面跳转  
  17.         driver.get("http://www.baidu.com/");  
  18.         //根据linkText定位  
  19.         WebElement link=driver.findElement(By.linkText("百科"));  
  20.         link.click();  
  21.     }  
  22.   
  23. }  

8)使用partialLinkText定位元素
定位的html文件:
Html代码   收藏代码
  1. <a href="http://baike.baidu.com" name="tj_baike">百科</a>  

Java代码   收藏代码
  1. package selenium.test.googleSearch;  
  2.   
  3. import org.openqa.selenium.By;  
  4. import org.openqa.selenium.WebDriver;  
  5. import org.openqa.selenium.WebElement;  
  6. import org.openqa.selenium.firefox.*;  
  7. public class BaiduFirefoxDriver {  
  8.   
  9.     /** 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         // TODO Auto-generated method stub   
  14.         System.setProperty("webdriver.firefox.bin""D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  15.         WebDriver driver = new FirefoxDriver();  
  16.         //页面跳转  
  17.         driver.get("http://www.baidu.com/");  
  18.         //根据linkText定位  
  19.         WebElement link=driver.findElement(By.linkText("科"));  
  20.         link.click();  
  21.     }  
  22.   
  23. }  

2.定位多个对象
Java代码   收藏代码
  1. package selenium.test.googleSearch;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.openqa.selenium.By;  
  6. import org.openqa.selenium.WebDriver;  
  7. import org.openqa.selenium.WebElement;  
  8. import org.openqa.selenium.firefox.*;  
  9. public class BaiduFirefoxDriver {  
  10.   
  11.     /** 
  12.      * @param args 
  13.      */  
  14.     public static void main(String[] args) {  
  15.         // TODO Auto-generated method stub   
  16.         System.setProperty("webdriver.firefox.bin""D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  17.         WebDriver driver = new FirefoxDriver();  
  18.         //页面跳转  
  19.         driver.get("http://www.baidu.com/");  
  20.         //定位百度快捷功能链接,并返回其值  
  21.         List<WebElement> links=driver.findElements(By.id("nv"));  
  22.         for(WebElement e:links){  
  23.             System.out.println(e.getText());  
  24.         }  
  25.     }  
  26.   
  27. }  

其输出结果为:
Java代码   收藏代码
  1. 新 闻 网 页 贴 吧 知 道 MP3 图 片 视 频 地 图  

3.层级定位
层级定位的思想是先定位父元素,然后再从父元素中精确定位出其我们需要选取的子元素。
层级定位一般的应用场景是无法直接定位到需要选取的元素,但是其父元素比较容易定位,通过定位父元素再遍历其子元素选择需要的目标元素,或者需要定位某个元素下所有的子元素。
定位的html文件:
Html代码   收藏代码
  1. <p id="nv">  
  2. <a href="http://news.baidu.com" name="tj_news">新&nbsp;闻</a>  
  3. <b>网&nbsp;页</b>  
  4. <a href="http://tieba.baidu.com" name="tj_tieba">贴&nbsp;吧</a>  
  5. <a href="http://zhidao.baidu.com" name="tj_zhidao">知&nbsp;道</a>  
  6. <a href="http://mp3.baidu.com" name="tj_mp3">MP3</a>  
  7. <a href="http://image.baidu.com" name="tj_img">图&nbsp;片</a>  
  8. <a href="http://video.baidu.com" name="tj_video">视&nbsp;频</a>  
  9. <a href="http://map.baidu.com" name="tj_map">地&nbsp;图</a>  
  10. </p>  

Java代码   收藏代码
  1. package selenium.test.googleSearch;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.openqa.selenium.By;  
  6. import org.openqa.selenium.WebDriver;  
  7. import org.openqa.selenium.WebElement;  
  8. import org.openqa.selenium.firefox.*;  
  9. public class BaiduFirefoxDriver {  
  10.   
  11.     /** 
  12.      * @param args 
  13.      */  
  14.     public static void main(String[] args) {  
  15.         // TODO Auto-generated method stub   
  16.         System.setProperty("webdriver.firefox.bin""D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  17.         WebDriver driver = new FirefoxDriver();  
  18.         //页面跳转  
  19.         driver.get("http://www.baidu.com/");  
  20.         //先定位父元素nv  
  21.         WebElement p=driver.findElement(By.id("nv"));  
  22.         //再根据父元素定位子元素  
  23.         WebElement link=p.findElement(By.name("tj_zhidao"));  
  24.         //打印子元素的link值  
  25.         System.out.println(link.getText());  
  26.           
  27.     }  
  28.   
  29. }  

输出结果为:
Java代码   收藏代码
  1. 知 道 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值