×
☰ See All Chapters

How to locate/find elements in selenium WebDriver

In this tutorial you will learn to use the locators in web driver. In our previous tutorial of Locators in Selenium, we learn different types of locators in selenium and also we learn to use them in selenium IDE. Now we will use the same locators in web driver. It is Important to note that the locators we learn in our previous chapter are same here in WebDriver, Locating elements is done by using the findElement() and findElements()methods.

We have 8 types of locators in Selenium using which findElement()/findElements() methods identifies elements on the web page:

  1. id 

  2. name 

  3. tagName 

  4. className 

  5. linkText 

  6. partialLinkText 

  7. xpath 

  8. cssSelector 

We learnt to use these locators with selenium IDE.  To use these locators in webdriver we have By class having the factory methods for each of the locators. A method which returns the instance of the same class in which it is present is called as Factory method. All the locators are factory method which returns an instance of By class.

WebDriver driver = new FirefoxDriver();

driver.findElement(By.id("firstName"));

driver.findElement(By.name("lastName"));

driver.findElement(By.tagName("select"));

driver.findElement(By.class("datePicker"));

driver.findElement(By.linkText("Welcome to www.tools4testing.com"));

driver.findElement(By.partialLinkText("java"));

driver.findElement(By.xpath("//*[@id="firstname"]"));

driver.findElement(By.cssSelector("#firstName"));

 

</head>

<body>

        <form name="signup" id="form" modelAttribute="data">

                <table align="center" width=90% cellspacing="2" cellpadding="2">

                        <tr>

                                <td>First Name :</td>

                                <td><input type="text" id="firstname"></td>

                        </tr>

                        <tr>

                                <td>Last Name :</td>

                                <td><input type="text" name="lastname"></td>

                        </tr>

                        <tr>

                                <td>Date of Birth :</td>

                                <td><input type="text" class="datePicker"

                                        placeholder="dd-mm-yyyy"></td>

                        </tr>

                        <tr>

                                <td>Gender :</td>

                                <td><select>

                                                <option value=""></option>

                                                <option value="Male">Male</option>

                                                <option value="Female">Female</option>

                                                <option value="Transgender">Transgender</option>

                                </select></td>

                        </tr>

                        <tr>

                                <td><a href="#">Welcome to www.tools4testing.com</a></td>

                        </tr>

                        <tr>

                                <td><a href="#">Welcome to www.java4coding.com</a></td>

                        </tr>

                        <tr>

                                <td colspan="2">

                                  <p align="center">

                                  <input type="button" value="Submi" name="submitUpdate" onclick="onclickSubmit()" />

                                </td>

                        </tr>

                </table>

        </form>

</body>

</html>

All the methods of By class accepts a sting value, this value should be the locator value.

The “findElement()” method returns the first WebElement object based on a specified search criteria, or throws an exception, if it does not find any element matching the search criteria. The “findElements()” method returns a list of WebElements matching the search criteria. If no elements are found, it returns an empty list. The “findElement()” and “FindElements()” methods throw a “NoSuchElementFoundException” exception when they fail to find the desired element using the specified locator strategy.

Determine if an element exists without throwing an exception

The “findElement()” and “FindElements()” methods throw a “NoSuchElementFoundException” exception when they fail to find the desired element using the specified locator strategy. You must handle this exception. Sometime pages may not be loaded with desired element which we expect and execution halts due to the exception.

To get around this, you can use “findElements()”, and check that the size of the list returned is 0.

Example:

List<WebElement> elements = driver.findElements(By.Id("testElement"));

elements.size(); //If this is not zero, then element exists

Please click on the below Locator tutorial which you are interested in reading. If you have not read any of these, we suggest reading all these in the order we provide without skipping any.

Locators in Selenium

Selenium id locator

Selenium name locator

Selenium classname locator

Selenium tagname locator

Selenium css locator

Selenium link text partial link text locator

Xpath in Selenium

Absolute xpath in selenium

Relative xpath in selenium

Selenium relative xpath by attribute

Selenium relative xpath using text function

Selenium relative xpath using contains function

Selenium relative xpath using starts with function

Independent and dependent concept in xpath

Group index in xpath

Xpath axes

Xpath vs css in selenium

You can use below test pages while learning these locators.

https://www.tools4testing.com/contents/selenium/testpages/selenium-id-name-classname-tagname-locator-testpage  

https://www.tools4testing.com/contents/selenium/testpages/selenium-absolute-xpath-testpage  

https://www.tools4testing.com/contents/selenium/testpages/selenium-relative-xpath-by-attribute-testpage  

https://www.tools4testing.com/contents/selenium/testpages/selenium-relative-xpath-by-text-function-testpage  

https://www.tools4testing.com/contents/selenium/testpages/selenium-relative-xpath-by-contains-testpage

https://www.tools4testing.com/contents/selenium/testpages/selenium-relative-xpath-by-starts-with-function-testpage  

https://www.tools4testing.com/contents/selenium/testpages/independent-and-dependent-concept-in-xpath-testpage  

https://www.tools4testing.com/contents/selenium/testpages/group-index-in-xpath-testpage  

https://www.tools4testing.com/contents/selenium/testpages/xpath-axes-testpage

https://www.tools4testing.com/contents/selenium/testpages/selenium-cssselector-locator-testpage

https://www.tools4testing.com/contents/selenium/testpages/selenium-linktext-and-partiallinktext-ocator-testpage

 

 


All Chapters
Author