<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 在 Selenium WebDriver 中查找元素和 FindElements > 原文: [https://www.guru99.com/find-element-selenium.html](https://www.guru99.com/find-element-selenium.html) ## 為什么需要“查找元素”命令? 與網頁的交互需要用戶定位網絡元素。 “查找元素”命令用于唯一標識網頁中的(一個)網絡元素。 而“查找元素”命令用于唯一標識網頁中的 Web 元素列表。 有多種方法可以唯一地標識網頁中的 Web 元素,例如 ID,名稱,類名,鏈接文本,部分鏈接文本,標記名和 XPATH。 ## FindElement 命令語法: “查找元素”命令將“按對象”作為參數,并返回 WebElement 類型的對象。 按對象依次可以與各種定位器策略一起使用,例如 ID,名稱,類名稱,XPATH 等。下面是 Selenium Web 驅動程序中 FindElement 命令的語法。 ``` WebElement elementName = driver.findElement(By.LocatorStrategy("LocatorValue")); ``` 定位器策略可以是以下任意值。 * ID * Name * 班級名稱 * 標簽名 * 連結文字 * 部分鏈接文字 * XPATH 定位器值是可以用來識別 Web 元素的唯一值。 開發人員和測試人員有責任使用某些屬性(例如 ID 或名稱)來唯一標識 Web 元素。 **示例:** ``` WebElement loginLink = driver.findElement(By.linkText("Login")); ``` ## FindElements 命令語法: “查找元素”命令將“按對象”作為參數,并返回 Web 元素列表。 如果使用給定的定位器策略和定位器值找不到任何元素,它將返回一個空列表。 下面是 find elements 命令的語法。 ``` List<WebElement> elementName = driver.findElements(By.LocatorStrategy("LocatorValue")); ``` **Example:** ``` List<WebElement> listOfElements = driver.findElements(By.xpath("//div")); ``` ## 查找元素與查找元素 以下是 find element 和 find elements 命令之間的主要區別。 ![](https://img.kancloud.cn/1e/f6/1ef61c3b96b4cc0b66c4905167662dce_835x302.png) | 查找元素 | 查找元素 | | 如果找到多個具有相同定位符的 Web 元素,則返回第一個 Web 元素 | 返回網絡元素列表 | | 如果沒有與定位器策略匹配的元素,則引發異常 NoSuchElementException | 如果沒有與定位器策略匹配的 Web 元素,則返回一個空列表 | | 只會找到一個網絡元素 | 它將找到與定位器策略匹配的元素的集合。 | | 不適用 | 每個 Web 元素都以從 0 開始的數字索引,就像數組一樣 | ### 示例:如何使用“查找元素”命令 以下應用程序用于演示目的 [http://demo.guru99.com/test/ajax.html](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(); } } ``` ### 示例:如何使用“查找元素”命令 **場景:** 1.打開被測應用程序的 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")); } } } ``` **摘要:** * “查找元素”命令返回與網頁中最前面的元素匹配的 Web 元素。 * “查找元素”命令返回符合條件的 Web 元素列表。 * 如果找不到元素與條件匹配的元素,則“查找元素”命令將引發 NoSuchElementException。 * 如果沒有符合條件的元素,“查找元素”命令將返回一個空列表
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看