zoukankan      html  css  js  c++  java
  • [Selenium+Java] Find Element and Find Elements in Selenium

    Original URL: https://www.guru99.com/find-element-selenium.html

    Find Element and Find Elements in Selenium

    Why do you need Find Element/s command?

    Interaction with a web page requires a user to locate the web element. Find Element command is used to uniquely identify a (one) web element within the web page. Whereas, Find Elements command is used to uniquely identify the list of web elements within the web page. There are multiple ways to uniquely identify a web element within the web page such as ID, Name, Class Name, Link Text, Partial Link Text, Tag Name and XPATH.

    FindElement command syntax:

    Find Element command takes in the By object as the parameter and returns an object of type WebElement. By object in turn can be used with various locator strategies such as ID, Name, Class Name, XPATH etc. Below is the syntax of FindElement command in Selenium web driver.

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

    Locator Strategy can by any of the following values.

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

    Locator Value is the unique value using which a web element can be identified. It is the responsibility of developers and testers to make sure that web elements are uniquely identifiable using certain properties such as ID or name.

    Example:

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

    FindElements command syntax:

    Find Elements command takes in By object as the parameter and returns a list of web elements. It returns an empty list if there are no elements found using the given locator strategy and locator value. Below is the syntax of find elements command.

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

    Example:

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

    Find element Vs Find elements

    Below are the major differences between find element and find elements commands.

    Find ElementFind Elements
    Returns the first most web element if there are multiple web elements found with the same locator Returns a list of web elements
    Throws exception NoSuchElementException if there are no elements matching the locator strategy Returns an empty list if there are no web elements matching the locator strategy
    It will only find one web element It will find a collection of elements whose match the locator strategy.
    Not Applicable Each Web element is indexed with a number starting from 0 just like an array

    Example

    Find Element

    The following application is used for demo purpose

    http://demo.guru99.com/test/ajax.html

    Scenario:

    1. Open the AUT

    2. Find and click radio button

    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
            System.out.println(By.Name("name"));
    
        }
    
    }
    

    Find Elements command:

    Scenario:

    1. Open the URL for Application Under Test

    2. Find the text of radio buttons and print it onto the output console

    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 &lt;
            WebElement &gt;
            elements = driver.findElements(By.name("name"));
            System.out.println("Number of elements:" + elements.size());
    
            for (int i = 0; i &lt; elements.size(); i++) {
                System.out.println("Radio button text:" + elements.get(i).getAttribute("value"));
            }
        }
    }
    

    Summary:

    • Find Element command returns the web element that matches the first most element within the web page.
    • Find Elements command returns a list of web elements that match the criteria.
    • Find Element command throws NoSuchElement exception if it does not find the element matching the criteria.
    • Find Elements command returns an empty list if there are no elements matching the criteria
  • 相关阅读:
    在CMD中使用for命令对单行字符串做分割的方法
    关于CMD/DOS中的短文件名规则
    [批处理]全盘搜索批量文件
    [批处理]批量提取MKV资源
    关于CMD中延迟环境变量嵌套的实现方法
    [批处理]自动按日期重命名文件名
    [批处理]手动伪造相机自动编号
    [批处理]简易命令行RAR
    [批处理]Oracle启动助手
    [批处理]强制删除文件及文件夹
  • 原文地址:https://www.cnblogs.com/alicegu2009/p/9099105.html
Copyright © 2011-2022 走看看