zoukankan      html  css  js  c++  java
  • 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来定位该元素。

    下面的例子定位了51.com首页上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. 下次自动登录                                                                                                                                               

    定位页面元素over了,下次写一下对frame的处理。

  • 相关阅读:
    【vue开发问题-解决方法】(六)axios报错问题,Cannot read property 'protocol' of undefined
    【vue开发问题-解决方法】(五)vue Element UI 日期选择器获取日期格式问题 t.getTime is not a function
    【vue开发问题-解决方法】(四)vue Element UI使用中.$scopedSlots.default is not a function 报错
    【vue开发问题-解决方法】(三)axios拦截器,post请求问题处理,异步请求封装
    【vue开发问题-解决方法】(二)element UI日期控件失效RangeError:Maximum call stack size exceeded
    【vue开发问题-解决方法】(一)在style中设置background-image时路径问题
    异常分析
    如何理解spring MVC
    解决项目不编译4大clean
    java中的生产者模式
  • 原文地址:https://www.cnblogs.com/yye2010/p/4596900.html
Copyright © 2011-2022 走看看