SeleniumWebDriver之FindElement和FindElements

文章首發於微信公衆號:軟測小生

爲什麼需要Find Element/s命令?

與Web頁面的交互需要用戶定位Web元素。FindElement命令用於唯一地標識頁面中的(單個)Web元素。然而,FindElements命令用於唯一地標識頁面中的Web元素列表。
有多種方法可以標識頁面中的Web元素,比如ID, Name, Class Name, Link Text, Partial Link Text, Tag名稱和XPath。

FindElement語法糖如下:
FindElement命令接受By對象作爲參數,並返回一個WebElement類型的對象。按對象依次可用於各種定位策略,如ID, Name, Class Name, XPath等。下面是Selenium WebDriver中的FindElement命令的語法

WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue"));

定位器策略(Locator Strategy)可以由下列值中的任何一個來執行:

  • ID
  • Name
  • Class Name
  • Tag Name
  • Link Text
  • Partial Link Text
  • XPATH

Locator值是唯一的值,可以使用它來標識Web元素。開發人員和測試人員有責任確保Web元素能夠使用特定的屬性(如id或Name)進行唯一的標識。
例如:

WebElement loginLink = driver.findElement(By.linkText("Login"));

FindElements語法糖如下:
FindElements命令同樣接受By對象作爲參數,並返回Web元素列表。如果沒有找到使用給定定位器策略和定位器值的元素,則返回一個空列表。下面是FindElements命令的語法。

List<WebElement> elementName = driver.findElements(By.LocatorStrategy("LocatorValue"));

例如:

List<WebElement> listOfElements = driver.findElements(By.xpath("//div"));

Find element Vs Find elements
下面是find element和find elements命令之間的主要區別。
在這裏插入圖片描述

Find Element Find Elements
如果發現多個Web元素具有相同的定位器,則返回第一個Web元素 返回Web元素列表
如果沒有匹配定位器策略的元素,則拋出異常NoSuchElementException 如果沒有匹配定位器策略的Web元素,則返回一個空列表
它只會找到一個Web元素 它將找到與定位器策略匹配的元素集合。
Not Applicable 每個Web元素的索引都是從數字0開始的,就像數組一樣

示例:如何使用Find Element命令
下面的應用程序用於演示練習,Demo使用主頁:http://demo.guru99.com/test/ajax.html

場景:

  1. 打開AUT
  2. 查找單選按鈕並點擊
package com.sample.stepdefinitions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class NameDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "D:\\3rdparty\\chrome\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();

driver.get("http://demo.guru99.com/test/ajax.html");

// Find the radio button for “No” using its ID and click on it
driver.findElement(By.id("no")).click();
	
//Click on Check Button
driver.findElement(By.id("buttoncheck")).click();
}
}

示例:如何使用Find Elements命令
場景:

  1. 打開測試應用程序Under的URL
  2. 找到單選按鈕的文本並將其打印到輸出控制檯
package com.sample.stepdefinitions;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class NameDemo {

public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver", "X://chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://demo.guru99.com/test/ajax.html");
    List<WebElement> elements = driver.findElements(By.name("name"));
    System.out.println("Number of elements:" +elements.size());

    for (int i=0; i<elements.size();i++){
      System.out.println("Radio button text:" + elements.get(i).getAttribute("value"));
    }
  }
}

總結

  • FindElement命令返回Web頁面一個元素(如果有多個元素的定位器相同,則返回第一個)。
  • Find Elements命令返回條件匹配的web元素列表。
  • 如果Find Element命令沒有找到匹配條件的元素,則拋出NoSuchElementException。
  • 如果沒有匹配條件的元素,Find Elements命令將返回一個空列表
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章