您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
Selenium Webdriver元素定位的八種常用方式
作者:測試控 發(fā)布時間:[ 2016/6/30 13:34:40 ] 推薦標(biāo)簽:功能測試工具 軟件測試

  在使用selenium webdriver進(jìn)行元素定位時,通常使用findElement或findElements方法結(jié)合By類返回的元素句柄來定位元素。其中By類的常用定位方式共八種,現(xiàn)分別介紹如下。
  1. By.name()
  假設(shè)我們要測試的頁面源碼如下:
  <button id="gbqfba" aria-label="Google Search" name="btnK" class="gbqfba"><span id="gbqfsa">Google Search</span></button>
  當(dāng)我們要用name屬性來引用這個button并點擊它時,代碼如下:
  1 public class SearchButtonByName {
  2         public static void main(String[] args){
  3                WebDriver driver = new FirefoxDriver();
  4                driver.get("http://www.forexample.com");
  5                WebElement searchBox = driver.findElement(By.name("btnK"));
  6                searchBox.click();
  7         }
  8 }
  2. By.id()
  頁面源碼如下:
  1 <button id="gbqfba" aria-label="Google Search" name="btnK" class="gbqfba"><span id="gbqfsa">Google Search</span></button>
  要引用該button并點擊它時,代碼如下:
  1 public class SearchButtonById {
  2
  3         public static void main(String[] args){
  4
  5             WebDriver driver = new FirefoxDriver();
  6
  7             driver.get("http://www.forexample.com");
  8
  9             WebElement searchBox = driver.findElement(By.id("gbqfba"));
  10
  11             searchBox.click();
  12
  13         }
  14
  15 }
  3. By.tagName()
  該方法可以通過元素的標(biāo)簽名稱來查找元素。該方法跟之前兩個方法的區(qū)別是,這個方法搜索到的元素通常不止一個,所以一般建議結(jié)合使用findElements方法來使用。比如我們現(xiàn)在要查找頁面上有多少個button,可以用button這個tagName來進(jìn)行查找,代碼如下:
  
  public class SearchPageByTagName{
  public static void main(String[] args){
  WebDriver driver = new FirefoxDriver();
  driver.get("http://www.forexample.com");
  List<WebElement> buttons = driver.findElements(By.tagName("button"));
  System.out.println(buttons.size());  //打印出button的個數(shù)
  }
  }                   
  
  另外,在使用tagName方法進(jìn)行定位時,還有一個地方需要注意的是,通常有些HTML元素的tagName是相同的,如下圖(1)所示。


  圖(1)

  從圖中我們可以看到,單選框、復(fù)選框、文本框和密碼框的元素標(biāo)簽都是input,此時單靠tagName無法準(zhǔn)確地得到我們想要的元素,需要結(jié)合type屬性才能過濾出我們要的元素。示例代碼如下:
  
  1 public class SearchElementsByTagName{
  2
  3         public static void main(String[] args){
  4
  5             WebDriver driver = new FirefoxDriver();
  6
  7             driver.get("http://www.forexample.com");
  8
  9             List<WebElement> allInputs = driver.findElements(By.tagName("input"));
  10
  11             //只打印所有文本框的值
  12
  13             for(WebElement e: allInputs){
  14
  15                   if (e.getAttribute(“type”).equals(“text”)){
  16
  17                   System.out.println(e.getText().toString());  //打印出每個文本框里的值
  18
  19                   }
  20
  21             }
  22
  23        }
  24
  25 }
  
  4. By.className()
  className屬性是利用元素的css樣式表所引用的偽類名稱來進(jìn)行元素查找的方法。對于任何HTML頁面的元素來說,一般程序員或頁面設(shè)計師會給元素直接賦予一個樣式屬性或者利用css文件里的偽類來定義元素樣式,使元素在頁面上顯示時能夠更加美觀。一般css樣式表可能會長成下面這個樣子:
  
  1 .buttonStyle{
  2
  3     width: 50px;
  4
  5     height: 50px;
  6
  7     border-radius: 50%;
  8
  9     margin: 0% 2%;
  10
  11 }
  
  定義好后,可以在頁面元素中引用上述定義好的樣式,如下:
  1 <button name="sampleBtnName" id="sampleBtnId" class="buttonStyle">I'm Button</button>
  如果此時我們要通過className屬性來查找該button并操作它的話,可以使用className屬性了,代碼如下:
  
  1 public class SearchElementsByClassName{
  2
  3     public static void main(String[] args){
  4
  5         WebDriver driver = new FirefoxDriver();
  6
  7         driver.get("http://www.forexample.com");
  8
  9         WebElement searchBox =  driver.findElement(By.className("buttonStyle"));
  10
  11         searchBox.sendKeys("Hello, world");
  12
  13     }
  14
  15 }
  
  注意:使用className來進(jìn)行元素定位時,有時會碰到一個元素指定了若干個class屬性值的“復(fù)合樣式”的情況,如下面這個button:<button id="J_sidebar_login" class="btn btn_big btn_submit" type="submit">登錄</button>。這個button元素指定了三個不同的css偽類名作為它的樣式屬性值,此時必須結(jié)合后面要介紹的cssSelector方法來定位了,稍后會有詳細(xì)例子。
  5. By.linkText()
  這個方法比較直接,即通過超文本鏈接上的文字信息來定位元素,這種方式一般專門用于定位頁面上的超文本鏈接。通常一個超文本鏈接會長成這個樣子:
  1 <a href="/intl/en/about.html">About Google</a>
  我們定位這個元素時,可以使用下面的代碼進(jìn)行操作:
  
  1 public class SearchElementsByLinkText{
  2
  3     public static void main(String[] args){
  4
  5         WebDriver driver = new FirefoxDriver();
  6
  7         driver.get("http://www.forexample.com");
  8
  9         WebElement aboutLink = driver.findElement(By.linkText("About Google"));
  10
  11         aboutLink.click();
  12
  13     }
  14
  15 }
  
  6. By.partialLinkText()
  這個方法是上一個方法的擴(kuò)展。當(dāng)你不能準(zhǔn)確知道超鏈接上的文本信息或者只想通過一些關(guān)鍵字進(jìn)行匹配時,可以使用這個方法來通過部分鏈接文字進(jìn)行匹配。代碼如下:
  1 public class SearchElementsByPartialLinkText{
  2
  3     public static void main(String[] args){
  4
  5         WebDriver driver = new FirefoxDriver();
  6
  7         driver.get("http://www.forexample.com");
  8
  9         WebElement aboutLink = driver.findElement(By.partialLinkText("About"));
  10
  11         aboutLink.click();
  12
  13     }
  14
  15 }
  注意:使用這種方法進(jìn)行定位時,可能會引起的問題是,當(dāng)你的頁面中不止一個超鏈接包含About時,findElement方法只會返回第一個查找到的元素,而不會返回所有符合條件的元素。如果你要想獲得所有符合條件的元素,還是只能使用findElements方法。

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd