<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: sadhvisingh1</title>
    <description>The latest articles on DEV Community by sadhvisingh1 (@sadhvisingh1).</description>
    <link>https://dev.to/sadhvisingh1</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F129535%2Fd8e0d8e4-eeb7-4f21-a01b-ea4281446c2e.jpg</url>
      <title>DEV Community: sadhvisingh1</title>
      <link>https://dev.to/sadhvisingh1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sadhvisingh1"/>
    <language>en</language>
    <item>
      <title>Selenium Waits Tutorial: Guide to Implicit, Explicit, and Fluent Waits</title>
      <dc:creator>sadhvisingh1</dc:creator>
      <pubDate>Mon, 12 Sep 2022 07:58:06 +0000</pubDate>
      <link>https://dev.to/testmuai/selenium-waits-tutorial-guide-to-implicit-explicit-and-fluent-waits-4gfm</link>
      <guid>https://dev.to/testmuai/selenium-waits-tutorial-guide-to-implicit-explicit-and-fluent-waits-4gfm</guid>
      <description>&lt;p&gt;Selenium waits for page load play an important part in your Selenium scripts. They help to make them less flaky and more reliable. &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep12_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; provides multiple waits to provide adequate wait or pause in your script execution based on certain conditions. Thereby ensuring you don’t end up getting failed scripts as you perform &lt;a href="https://www.lambdatest.com/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep12_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automation testing with Selenium&lt;/a&gt;. In this tutorial, we will be explaining the types of Selenium waits and sleep, there real-time examples and a comparison study on them. Let us start by answering a pivotal question “Why should we use Selenium waits?”&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Do We Need Selenium Waits?
&lt;/h2&gt;

&lt;p&gt;Majority of modern application’s front-end is built on JavaScript or Ajax, using frameworks such as React, Angular, or any other which takes a certain time for the web elements to load on the page, whenever that page is loaded or refreshed. Hence, in case you tend to locate an element in your script which is yet to load on the page, selenium will throw you ‘&lt;strong&gt;ElementNotVisibleException&lt;/strong&gt;’ message.&lt;/p&gt;

&lt;p&gt;Below code snippet will help you showcase the same problem as you execute automation testing with Selenium. In this code snippet, I am using an example of easemytrip.com, where post user selects the ‘From’ and ‘To’ destination with a date of journey, the web application takes a certain time to load the required flight details. In this case, without applying wait, the user tends to book the first flight from the list. Now, since the page hasn’t loaded yet, the script failed to find the ‘book now’ button. Resulting in throwing a ‘&lt;strong&gt;NoSuchElementException&lt;/strong&gt;’. Code snippet and console output below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor;

public class NoWaitImplemented {

public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", ".\\Driver\\chromedriver.exe");

        WebDriver driver=new ChromeDriver();

        driver.manage().window().maximize();


        driver.get("[https://www.easemytrip.com/](https://www.easemytrip.com/)");

        driver.findElement(By.id("FromSector_show")).sendKeys("Delhi", Keys.ENTER);
        driver.findElement(By.id("Editbox13_show")).sendKeys("Mumbai", Keys.ENTER);
        driver.findElement(By.id("ddate")).click();
        driver.findElement(By.id("snd_4_08/08/2019")).click();
        driver.findElement(By.className("src_btn")).click();
        driver.findElement(By.xpath("//button[text()='Book Now']")).click();

}

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3154%2F0%2AX0J-m43uFx60-aN5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3154%2F0%2AX0J-m43uFx60-aN5.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have made use of XPath for locating web elements using the scripts for automation testing with Selenium.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep12_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Read More: Complete Guide For Using XPath In Selenium With Examples&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Selenium wait for a page to load helps in resolving this issue. There are different types of Selenium waits like Implicit wait and Explicit wait, that ensures the elements are loaded into the page before they are discovered by the &lt;a href="https://www.lambdatest.com/blog/how-to-use-name-locator-in-selenium-automation-scripts/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep12_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium script&lt;/a&gt; for further actions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ApZ-ediGhvnfUyj-W.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ApZ-ediGhvnfUyj-W.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This certification is for anyone who wants to stay ahead among professionals who are growing their career in Selenium automation testing.&lt;/p&gt;

&lt;p&gt;Here’s a short glimpse of the Selenium 101 certification from LambdaTest:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/qx9FPFfJm7E"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Selenium Waits For Page Load
&lt;/h2&gt;

&lt;p&gt;When performing automation testing with Selenium, we use the following types of waits as we generate our Selenium script:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Thread.Sleep() method&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implicit Wait&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Explicit Wait&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fluent Wait&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let us understand each one of these in-depth.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thread.Sleep() For Automation Testing with Selenium
&lt;/h2&gt;

&lt;p&gt;Sleep is a static method that belongs to the thread class. This method can be called using the reference of the class name i.e Thread. If you use &lt;strong&gt;Thread.sleep&lt;/strong&gt; while performing automation testing with Selenium, then this method will stop the execution of the script for the specified duration of time, irrespective of whether the element is found or not on the web page. It accepts the time duration in milliseconds. The syntax for the same is:&lt;/p&gt;

&lt;p&gt;Thread.sleep(3000);&lt;/p&gt;

&lt;p&gt;The sleep function throws InterruptedException so it should be handled using a try-catch block as below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try{
Thread.sleep(5000);
}
catch(InterruptedException ie){
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Why Using Thread.Sleep() Is Not A Good Idea?
&lt;/h2&gt;

&lt;p&gt;I will now be highlighting some disadvantages of using the thread.sleep().&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Selenium Webdriver waits for the specified time, irrespective of the element is found or not. In case the element is found much before the specified duration, the script will still wait for the time duration to elapse, thereby increasing the execution time of the script.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the element to be present does not appear after a static time and keep changing, then you will never know an estimated time needed for the sleep function. In case it takes more time than the defined time, the script shall throw an error. Which is why, if you are dealing with dynamic elements using Selenium waits then it is wise to not use Thread.sleep().&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Thread.sleep is intended only for the element it is written prior to. In case you have two to fours elements which need to wait for a certain duration to load, you need to specify Thread.sleep as many times in that case. And if you do that! Well, you will find your script filled with Thread.sleep() syntax all over the place.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Owing to the above disadvantages, using Thread.Sleep() in your script creation is considered as a bad practice.&lt;/p&gt;

&lt;p&gt;The below code snippet highlights the usage of Thread.Sleep() for automation testing with Selenium. In this example, we are using the same example of easemytrip above, where we will stop the thread execution, once the user clicks on search. The code works smoothly in this case, without throwing any error.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor;

public class ThreadWait {

public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        //setting the driver executable
        System.setProperty("webdriver.chrome.driver", ".\\Driver\\chromedriver.exe");

        //Initiating your chromedriver
        WebDriver driver=new ChromeDriver();

        driver.manage().window().maximize();


        driver.get("[https://www.easemytrip.com/](https://www.easemytrip.com/)");

        driver.findElement(By.id("FromSector_show")).sendKeys("Delhi", Keys.ENTER);
        driver.findElement(By.id("Editbox13_show")).sendKeys("Mumbai", Keys.ENTER);
        driver.findElement(By.id("ddate")).click();
        driver.findElement(By.id("snd_4_08/08/2019")).click();
        driver.findElement(By.className("src_btn")).click();
        Thread.sleep(5000);
        driver.findElement(By.xpath("//button[text()='Book Now']")).click();

}

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, what if I have another page of the same application that takes a certain time to load? In this case, I would not prefer to use, thread.sleep() multiple times in my script.&lt;/p&gt;

&lt;p&gt;You may be thinking that if not Thread.sleep(), then which Selenium wait for page load will suffice the testing requirement?&lt;/p&gt;

&lt;p&gt;This is where Implicit wait comes to rescue in such cases. Let us checkout Implicit Selenium wait in detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implicit Wait For Automation Testing with Selenium
&lt;/h2&gt;

&lt;p&gt;Selenium has overcome the problems provided by Thread.sleep() and have come up with two Selenium waits for page load. One of which is Implicit wait which allows you to halt the WebDriver for a particular period of time until the WebDriver locates a desired element on the web page.&lt;/p&gt;

&lt;p&gt;The key point to note here is, unlike Thread.sleep(), it does not wait for the complete duration of time. In case it finds the element before the duration specified, it moves on to the next line of code execution, thereby reducing the time of script execution. This is why Implicit wait is also referred to as dynamic wait. If it does not find the element in the specified duration, it throws &lt;strong&gt;ElementNotVisibleException&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Another interesting thing to note about Implicit wait is that it is applied globally, which makes it a better option than Thread.sleep(). Meaning you only need to write it one time and it gets applicable for all of the web elements specified on a script throughout the WebDriver instance. Convenient isn’t it? The syntax to achieve the same is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.manage().timeouts().implicitlyWait(Time Interval to wait for, TimeUnit.SECONDS);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The default time for Implicit wait is zero and it keeps polling for the required element after every 500 milliseconds. Let’s see the code snippet below, showcasing the use of Implicit wait. In this example, I am using the same easemytrip example. In this case, we are going a step ahead and continuing the booking process, where the page takes more time to load. Here the page load issue exists for two pages, which we are dealing using a single line of code using implicit wait rather than using Thread.sleep() multiple times.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor;

public class ImplicitWait {

public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        //setting the driver executable
        System.setProperty("webdriver.chrome.driver", ".\\Driver\\chromedriver.exe");

        //Initiating your chromedriver
        WebDriver driver=new ChromeDriver();

        driver.manage().window().maximize();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("[https://www.easemytrip.com/](https://www.easemytrip.com/)");

        driver.findElement(By.id("FromSector_show")).sendKeys("Delhi", Keys.ENTER);
        driver.findElement(By.id("Editbox13_show")).sendKeys("Mumbai", Keys.ENTER);
        driver.findElement(By.id("ddate")).click();
        driver.findElement(By.id("snd_4_08/08/2019")).click();
        driver.findElement(By.className("src_btn")).click();

driver.findElement(By.xpath("//button[text()='Book Now']")).click();

        JavascriptExecutor jse = (JavascriptExecutor)driver;
        jse.executeScript("window.scrollBy(0,750)");

        driver.findElement(By.xpath("//input[[@type](http://twitter.com/type)='email']")).sendKeys("[sadhvisingh9049@gmail.com](mailto:sadhvisingh9049@gmail.com)");

        driver.findElement(By.xpath("//span[text()='Continue Booking']")).click();

        WebElement title=driver.findElement(By.id("titleAdult0"));

        Select titleTraveller=new Select(title);

        titleTraveller.selectByVisibleText("MS");
        driver.findElement(By.xpath("//input[[@placeholder](http://twitter.com/placeholder)='Enter First Name']")).sendKeys("Sadhvi");
        driver.findElement(By.xpath("//input[[@placeholder](http://twitter.com/placeholder)='Enter Last Name']")).sendKeys("Singh");

        driver.findElement(By.xpath("//input[[@placeholder](http://twitter.com/placeholder)='Mobile Number']")).sendKeys("9958468819");

        driver.findElement(By.xpath("//div[[@class](http://twitter.com/class)='con1']/span[[@class](http://twitter.com/class)='co1']")).click();

}

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, here we are aware of the fact, that pages shall we loaded in a certain duration, but what if we do not know the element to be visible/clickable at loading time. As in the time of its appearance is dynamic and keeps on changing from time to time. In this case, Explicit wait will help you overcome this problem. Let’s investigate its detail.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AwZI5YBKLuAHeJ_M9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AwZI5YBKLuAHeJ_M9.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Explicit Wait For Automation Testing with Selenium
&lt;/h2&gt;

&lt;p&gt;The Explicit wait is another one of the dynamic Selenium waits. Explicit wait help to stop the execution of the script based on a certain condition for a specified amount of time. Once the time goes overboard, you will get the &lt;strong&gt;ElementNotVisibleException&lt;/strong&gt;. In a scenario where you do not know the amount of time to wait for, this explicit wait comes in handy. Using conditions like &lt;strong&gt;elementToBeClickable()&lt;/strong&gt; or &lt;strong&gt;textToBePresentInElement()&lt;/strong&gt;, one can wait for the specified duration. One can use these predefined methods using the combination of classes WebDriverWait and ExpectedConditions. In order to use this case, import the below packages in your class:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Post this one needs to create a reference variable for the WebDriverWait class and instantiate it using the WebDriver instance and providing the amount of Selenium wait for page load, one may need. The unit of time is in seconds. One can define it as below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WebDriverWait wait = new WebDriverWait(driver,30);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In order to use the predefined methods of the ExpectedCondition Class, we will use the wait reference variable as below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wait.until(ExpectedConditions.visibilityOfElementLocated(Reference of Element to be  located using locator));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Types of Expected Conditions:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Below are the few types of expected conditions commonly used as you perform automation testing with Selenium.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;visibilityOfElementLocated()-&lt;/strong&gt; Verifies if the given element is present or not&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;alertIsPresent()-&lt;/strong&gt; Verifies if the alert is present or not.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;elementToBeClickable()-&lt;/strong&gt; Verifies if the given element is present/clickable on the screen&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;textToBePresentInElement()-&lt;/strong&gt; Verifies the given element have the required text or not&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;titlels()-&lt;/strong&gt; Verify the condition wait for a page that has a given title&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are many more expected conditions available, which you can refer through the Selenium official GitHub page. Like Implicit wait, the explicit wait also keeps polling after every 500 milliseconds.&lt;/p&gt;

&lt;p&gt;Below is the code snippet highlighting the usage of an Explicit Selenium wait. In this example, we are using the ‘rentomojo’ application, where a modal appears at a dynamic time on the homepage. Using explicit wait, based on the element visibility, we will wait for the element and close the pop-up. Referenced code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ExplicitWait {

public static void main(String[] args) {
        // TODO Auto-generated method stub

        //setting the driver executable
                System.setProperty("webdriver.chrome.driver", ".\\Driver\\chromedriver.exe");

                //Initiating your chromedriver
                WebDriver driver=new ChromeDriver();



                driver.manage().window().maximize();

                driver.get("[https://www.rentomojo.com/](https://www.rentomojo.com/)");

                driver.findElement(By.xpath("//span[[@class](http://twitter.com/class)='rm-city__selectorBoxLoca'][contains(text(),'Delhi')]")).click();



                WebDriverWait wait=new WebDriverWait(driver, 120);


                wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//div[[@class](http://twitter.com/class)='Campaign__innerWrapper']/button"))));
                driver.findElement(By.xpath("//div[[@class](http://twitter.com/class)='Campaign__innerWrapper']/button")).click();

}

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note-&lt;/strong&gt; When implicit wait and explicit wait are given in conjunction, then they work on cumulative time, rather than on a single wait condition. For example, if the Implicit wait is given for 30 seconds and the Explicit wait is given for 10 seconds, then the explicit element it is looking for will wait for 40 seconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Difference Between Selenium Waits: Explicit Vs Implicit
&lt;/h2&gt;

&lt;p&gt;Now, since you are aware of the usage of implicit and explicit waits, let us investigate the difference between these 2 Selenium waits:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;IMPLICIT WAIT&lt;/th&gt;
&lt;th&gt;EXPLICIT WAIT&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;It is by default applied to all the elements in the script.&lt;/td&gt;
&lt;td&gt;It is applicable to only a certain element which is specific to a certain condition.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;We cannot wait based on a specified condition like element selectable/clickable unlike explicit.&lt;/td&gt;
&lt;td&gt;In explicit, we can specify the wait based on a specific condition.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;It is usually used when you are sure the element may be visible in a certain time&lt;/td&gt;
&lt;td&gt;It is usually used, when you are not aware of the time of the element visibility. It is subjected to dynamic nature.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Fluent Wait For Automation Testing with Selenium
&lt;/h2&gt;

&lt;p&gt;The Fluent wait is similar to explicit wait in terms of its functioning. In Fluent wait, you perform a Selenium wait for an element when you are not aware of the time it may take to be visible or clickable. The few differential factors that Fluent wait offers are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The polling frequency-&lt;/strong&gt; In case of Explicit wait, this polling frequency is by default 500 milliseconds. Using Fluent wait, you can change this polling frequency based on your needs, i.e you can tell your script to keep checking on an element after every ‘x’ seconds.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Ignore Exception-&lt;/strong&gt; During polling, in case you do not find an element, you can ignore any exception like ‘NoSuchElement’ exception etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Apart from these differential factors, like Explicit wait or Implicit wait, you can define the amount of time to wait for the element to be visible or actionable. The below syntax or lines of code are used to define Fluent wait in Selenium:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Wait&amp;lt;WebDriver&amp;gt; fluentWait = new FluentWait&amp;lt;WebDriver&amp;gt;(driver)
       .withTimeout(60, SECONDS) // this defines the total amount of time to wait for
       .pollingEvery(2, SECONDS) // this defines the polling frequency
       .ignoring(NoSuchElementException.class); // this defines the exception to ignore 
      WebElement foo = fluentWait.until(new Function&amp;lt;WebDriver, WebElement&amp;gt;() {
     public WebElement apply(WebDriver driver)  //in this method defined your own subjected conditions for which we need to wait for
     {  return driver.findElement(By.id("foo"));
}});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The syntax appears to be complex, but once you start using, it may become handy. Probably this is one of its biggest reason where testers opt to go for explicit wait more than Fluent wait. Also, the major difference between the Explicit wait and the Fluent wait is, that the Explicit Selenium Wait provides predefined conditions, which are applied on elements we need to wait for, whereas, in case of Fluent Selenium wait, you can define your own customized conditions within the apply method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test your mobile websites and smartphone apps using our scalable on cloud mobile &lt;a href="https://www.lambdatest.com/mobile-emulator-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep12_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;emulator online&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  My opinion on Fluent Selenium Wait?
&lt;/h2&gt;

&lt;p&gt;I personally haven’t found any useful implementation of fluent wait in a real-time example, hence would like to refrain myself from the implementation of it, as of now. I am as eager as you, for the &lt;a href="https://www.lambdatest.com/blog/what-to-expect-from-the-new-version-of-selenium-4-alpha/" rel="noopener noreferrer"&gt;launch of Selenium 4&lt;/a&gt;. I am hoping that after the launch we may get more insights on the practical advantage of using Fluent wait over the other Selenium waits. So far based on my experience, I happen to lean towards the use of Explicit Selenium wait, owing to its easier implementation of code than Fluent Selenium wait. If you feel otherwise then let me know in the comments below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Click here to know How To Use IOS &lt;a href="https://www.lambdatest.com/blog/iphone-simulators-on-windows/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep12_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Emulators for PC&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Role Of Selenium Waits for running automation Cloud Selenium Grid
&lt;/h2&gt;

&lt;p&gt;Majority of testers usually prefer to perform &lt;a href="https://www.lambdatest.com/blog/common-challenges-faced-during-website-automated-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep12_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;automated website testing&lt;/a&gt; using a cloud-based service provider for Selenium testing such as LambdaTest, a &lt;a href="https://www.lambdatest.com/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep12_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;cross browser testing&lt;/a&gt; tool on cloud. Wondering why?&lt;/p&gt;

&lt;p&gt;So what happens if you are running your &lt;a href="https://www.lambdatest.com/blog/a-complete-guide-for-your-first-testng-automation-script/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep12_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;automation scripts&lt;/a&gt; on a cloud-based Selenium Grid such as LambdaTest? How can Selenium waits contribute to an effective test result?&lt;/p&gt;

&lt;p&gt;Every cloud-based provider will offer a default time limit before throwing Selenium timeout exception. This is done to prevent over-exploitation of the cloud’s resources. At LamdaTest, you get a default timeout of 90 seconds. Now, if your loading time for web elements is more than 90 seconds then what can you do?&lt;/p&gt;

&lt;p&gt;This is where Selenium wait comes into the picture. If your test suite is a complex one, which may face a timeout issue on cloud Selenium Grid then you can use Selenium wait to pause the WebDriver for more than 90 seconds default time limit. Thereby, avoiding a timeout error as you successfully run your automation script.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Click here,  Say No To Safari VM! Perform Cross Browser Compatibility Testing On All &lt;a href="https://www.lambdatest.com/safari-browser-for-windows?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep12_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Safari for windows&lt;/a&gt; across Real Browsers And Operating Systems.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Final Verdict On Selenium Waits!
&lt;/h2&gt;

&lt;p&gt;Selenium Waits helps to make your scripts less flaky and more reliable. Whichever wait you choose to go with, make sure it does the business to help you achieve your purpose behind automation testing with Selenium. Another key thing to note is to ensure not keeping unnecessary Selenium waits in your application. Thread.sleep() may have been an option in the past, but now that we have new Selenium waits up &amp;amp; coming, I would rather recommend you to drive your Selenium automation testing away from Thread.sleep(). So, the next time, you are stuck with the elements or page loading issue, you know where to look for. Happy testing!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2All_iLuNHrplfawMj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2All_iLuNHrplfawMj.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>testing</category>
    </item>
    <item>
      <title>Handling Multiple Browser Tabs With Selenium Automation Testing</title>
      <dc:creator>sadhvisingh1</dc:creator>
      <pubDate>Mon, 05 Sep 2022 10:48:33 +0000</pubDate>
      <link>https://dev.to/testmuai/handling-multiple-browser-tabs-with-selenium-automation-testing-2djj</link>
      <guid>https://dev.to/testmuai/handling-multiple-browser-tabs-with-selenium-automation-testing-2djj</guid>
      <description>&lt;p&gt;Automation testing with &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; has been a lifeline in grooming budding automation testers into professionals. Selenium being open-source is largely adopted on a global scale. As a result of which you get huge support from the community. There are multiple frameworks for different languages that offer bindings with Selenium. So you have got everything on board for getting started with Selenium. Now, comes the phases where you run your first test script to perform Selenium. The scripts would involve basic test scenarios if you are learning Selenium automation. You may validate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A simple &lt;a href="https://www.lambdatest.com/blog/selenium-java-tutorial-how-to-test-login-process/" rel="noopener noreferrer"&gt;login form with Selenium automation testing&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/screenshots-with-selenium-webdriver/" rel="noopener noreferrer"&gt;Capture screenshots of a web page with Selenium WebDriver&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Web elements using &lt;a href="https://www.lambdatest.com/blog/locators-in-selenium-webdriver-with-examples/" rel="noopener noreferrer"&gt;CSS locators in Selenium WebDriver&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set up a &lt;a href="https://www.lambdatest.com/blog/selenium-grid-setup-tutorial/" rel="noopener noreferrer"&gt;Selenium Grid for parallel execution&lt;/a&gt; of test cases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/pytest-report-generation-for-selenium-automation-scripts/" rel="noopener noreferrer"&gt;Generating Selenium test reports&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There could be many more things, one may look to validate as one aims to perform automation testing with Selenium. Today, I am going to help you perform one of the basic and fundamental validations for test automation with Selenium. I will demonstrate how you can handle multiple browser tabs using Selenium automation testing.&lt;/p&gt;

&lt;p&gt;If you’re new to Selenium and wondering what it is then we recommend checking out our guide — What is Selenium?&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started With A Practical Scenario
&lt;/h2&gt;

&lt;p&gt;Sometimes you may come across a complex scenario where you may have to open a new tab or window and perform desired actions on the opened tab/window. Handling multiple tabs or windows may seem complex at the start but once you know how to handle them, it becomes really easy. Let’s take into account a scenario.&lt;/p&gt;

&lt;p&gt;Assuming, you open the homepage of Airbnb and wish to open the details of a homestay in another tab, perform some actions on the opened tab and then switch back to the previous tab. Then how do you do so?&lt;/p&gt;

&lt;p&gt;You may find multiple solutions on the web regarding this. Few people use sendkeys method &lt;strong&gt;‘Control + t’&lt;/strong&gt; to open a tab, post locating the body of the homepage. This approach most of the time does not work owing to sendKeys issue with the browser behavior. Hence, the best approach to open tab is using a Robot class or using JavascriptExecutor. Robot class ensure your tab is opened using the &lt;strong&gt;‘Control + t’&lt;/strong&gt; command, while through javascript executor you can simply open the tab using windows.open. Post opening the tab you can switch to the tab using either &lt;strong&gt;Action Class&lt;/strong&gt; approach or using &lt;strong&gt;Selenium WebDriver interface methods getWindowHandle &amp;amp; getWindowHandles&lt;/strong&gt;. I will be showcasing both the approaches in this article.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The below test steps need to be addressed in order to open a tab in Airbnb.&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Open Airbnb URL.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Search for ‘Goa’ location.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Store URL of any stay.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open a new tab&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Switch to the new tab and launch the desired stored URL.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;In order to open a new tab, the following Robot class code can be used:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Robot r = new Robot();        
        r.keyPress(KeyEvent.VK_CONTROL);
        r.keyPress(KeyEvent.VK_T);
        r.keyRelease(KeyEvent.VK_CONTROL);
        r.keyRelease(KeyEvent.VK_T);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above code helps to open a tab using ‘control + t’ command of the keyboard. This can be performed using sendKeys but its credibility towards working or not seems sporadic owing to the behavior of the browser with which it is used. You can use sendKeys command as below to replicate the above behavior.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.findElement(By.cssSelector(“body”)).sendKeys(Keys.CONTROL+ “t”);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Handling Tabs In Selenium Using The Window Handler Method
&lt;/h2&gt;

&lt;p&gt;Now, all we need to do is switch to this opened tab using Window Handler methods. Code snippet below for your reference:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class HandlingMultipleTabs {

public static void main(String[] args) throws InterruptedException, AWTException {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", ".\\ChromeDriver\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        //Navigating to airbnb
        driver.get("[https://www.airbnb.co.in/](https://www.airbnb.co.in/)");

        driver.manage().window().maximize();

        String currentHandle= driver.getWindowHandle();

        //locating the location, looking for homestays
        driver.findElement(By.id("Koan-magic-carpet-koan-search-bar__input")).sendKeys("Goa", Keys.ENTER);

        //Clicking on search button
        driver.findElement(By.xpath("//button[[@type](http://twitter.com/type)='submit']")).click();
        String urlToClick=driver.findElement(By.xpath("//div[text()='Luxury Three Bedroom Apartment with Pool &amp;amp; Jacuzzi']/ancestor::a")).getAttribute("href");

        //opening the new tab
        Robot r = new Robot();        
        r.keyPress(KeyEvent.VK_CONTROL);
        r.keyPress(KeyEvent.VK_T);
        r.keyRelease(KeyEvent.VK_CONTROL);
        r.keyRelease(KeyEvent.VK_T);

        //getting all the handles currently available
        Set&amp;lt;String&amp;gt; handles=driver.getWindowHandles();
        for(String actual: handles)
        {

         if(!actual.equalsIgnoreCase(currentHandle))
         {
             //switching to the opened tab
             driver.switchTo().window(actual);

             //opening the URL saved.
             driver.get(urlToClick);
         }
        }



    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Use the below command if you wish to switch back to the original tab.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.switchTo().defaultContent();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, let’s try to open the tab using JavascriptExecutor and switch to that tab for the same scenario above. Below is the referenced code snippet:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class multipltabsonce123 {

public static void main(String[] args) {
        // TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver", ".\\ChromeDriver\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        //Navigating to airbnb
        driver.get("[https://www.airbnb.co.in/](https://www.airbnb.co.in/)");

        driver.manage().window().maximize();

        String currentHandle= driver.getWindowHandle();

        //locating the location, looking for homestays
        driver.findElement(By.id("Koan-magic-carpet-koan-search-bar__input")).sendKeys("Goa", Keys.ENTER);

        //Clicking on search button
        driver.findElement(By.xpath("//button[[@type](http://twitter.com/type)='submit']")).click();
        String urlToClick=driver.findElement(By.xpath("//div[text()='Luxury Three Bedroom Apartment with Pool &amp;amp; Jacuzzi']/ancestor::a")).getAttribute("href");

        //opening the new tab
        ((JavascriptExecutor)driver).executeScript("window.open()");

        //getting all the handles currently avaialbe
        Set&amp;lt;String&amp;gt; handles=driver.getWindowHandles();
        for(String actual: handles)
        {

         if(!actual.equalsIgnoreCase(currentHandle))
         {
             //switching to the opened tab
             driver.switchTo().window(actual);

             //opening the URL saved.
             driver.get(urlToClick);
         }
        }

    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Kudos! You have successfully performed automation testing with Selenium for switching different browser tabs with the help of Windows Handler method. Now, let us go about it in a different manner.&lt;/p&gt;

&lt;p&gt;You can also refer to the below video tutorial on how to handle Windows and Frames in selenium.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/32eIE4PAbJk"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test your mobile websites and smartphone apps using our scalable on cloud mobile &lt;a href="https://www.lambdatest.com/mobile-emulator-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep05_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;emulator online&lt;/a&gt; here&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling Tabs In Selenium Using The Action Class
&lt;/h2&gt;

&lt;p&gt;As mentioned above, we can switch to tabs using both &lt;strong&gt;Window Handler&lt;/strong&gt; and &lt;strong&gt;Action Class&lt;/strong&gt;. The below code snippet showcases how to switch to tabs using Action class. Since action class also use inference of sendkeys, it may or may not work subjected to the browser under use.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class HandlingMultipleTabs {

public static void main(String[] args) throws InterruptedException, AWTException {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", ".\\ChromeDriver\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        //Navigating to airbnb
        driver.get("[https://www.airbnb.co.in/](https://www.airbnb.co.in/)");

        driver.manage().window().maximize();

        String currentHandle= driver.getWindowHandle();

        //locating the location, looking for homestays
        driver.findElement(By.id("Koan-magic-carpet-koan-search-bar__input")).sendKeys("Goa", Keys.ENTER);

        //Clicking on search button
        driver.findElement(By.xpath("//button[[@type](http://twitter.com/type)='submit']")).click();
        String urlToClick=driver.findElement(By.xpath("//div[text()='Luxury Three Bedroom Apartment with Pool &amp;amp; Jacuzzi']/ancestor::a")).getAttribute("href");

        //opening the new tab
        Robot r = new Robot();        
        r.keyPress(KeyEvent.VK_CONTROL);
        r.keyPress(KeyEvent.VK_T);
        r.keyRelease(KeyEvent.VK_CONTROL);
        r.keyRelease(KeyEvent.VK_T);



       //switch using actions class
        Actions action= new Actions(driver);
        action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform();

        //opening the URL saved.
        driver.get(urlToClick);


    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And that is it! You have handled multiple browser tabs with Selenium automation testing using both Windows Handler method &amp;amp; using the Action Class as well. Now, I will be talking about one of the most common drawbacks of using Selenium. So we know that Selenium WebDriver is a great open-source tool for automating web applications. However, the primary pain point with WebDriver is the sequential execution of test scripts.&lt;/p&gt;

&lt;p&gt;As a resolution, ThoughtWorks(Founder of Selenium) came up with Selenium Grid to help users run multiple test cases, simultaneously, in parallel. This drastically brought down the test builds execution.&lt;/p&gt;

&lt;p&gt;So we have a way to run multiple test cases in parallel as we perform automation testing with Selenium. But how scalable is it?&lt;/p&gt;

&lt;p&gt;Setting up a Selenium Grid of your own would demand a lot of CPU consumption &amp;amp; maintaining it comes as a hassle. The number of tests you wish to perform parallel execution with Selenium, the higher is the demand for computation. &lt;strong&gt;So what can you do? How can you perform automation testing with Selenium, at scale?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffjl8bywk7w9k3on7r2us.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffjl8bywk7w9k3on7r2us.jpg" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Are you looking for &lt;a href="https://www.lambdatest.com/web-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep05_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;web testing&lt;/a&gt; platforms? You can test your website on 3000+ different OS and browsers here. Get instant access to choice of web browsers, browser versions, OS, and resolutions.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Executing Automation Testing With Selenium On Cloud
&lt;/h2&gt;

&lt;p&gt;A cloud-based Selenium Grid will allow you to run your test cases without the hassle of infrastructure set up. All you would require is an internet connection. We have multiple platforms that help us provide a rich bed of browsers, versions, mobile devices, android versions etc.&lt;/p&gt;

&lt;p&gt;Selenium&lt;/p&gt;

&lt;p&gt;Let us execute the above-demonstrated test cases on LambdaTest Selenium Grid. I will be showcasing how we can open multiple tabs, on a cloud-based platform and access the required details like video, screenshots, console logs, etc. for LambdaTest.&lt;/p&gt;

&lt;p&gt;All you need to do is set up the LambdaTest URL while instantiating the remoteWebDriver. This URL is a combination of username, access key and LambdaTest hub URL. Now, all you need to do is define the platform, browser, version and the add-ons you require. Once this setup process is complete, use the same multiple tab script and run it on the LambdaTest platform. The referenced code snippet below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.net.URL;
import java.util.Arrays;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class HandlingMultipleTabs {



public RemoteWebDriver driver=null;
    public String url="[https://www.lambdatest.com/](https://www.lambdatest.com/)";
    public static final String  username= "sadhvisingh24"; // Your LambdaTest Username
    public static final String auth_key = "abcdefghi123456789"; // Your LambdaTest Access Key
    public static final String URL= "[@hub](http://twitter.com/hub).lambdatest.com/wd/hub"; //This is the hub URL for LambdaTest


    [@BeforeClass](http://twitter.com/BeforeClass)
    public void setUp()
    {
        DesiredCapabilities capabilities= new DesiredCapabilities();
        capabilities.setCapability("browserName", "chrome");
        capabilities.setCapability("version", "73.0");
        capabilities.setCapability("platform", "win10"); // If this cap isn't specified, it will just get the any available one
        capabilities.setCapability("build", "MultipleTabs_Lambdatest");
        capabilities.setCapability("name", "MultipleTabs_Lambdatest");
        capabilities.setCapability("network", true); // To enable network logs
        capabilities.setCapability("visual", true); // To enable step by step screenshot
        capabilities.setCapability("video", true); // To enable video recording
        capabilities.setCapability("console", true); // To capture console logs
        try {

         driver = new RemoteWebDriver(new URL("https://" + username + ":" + auth_key + URL), capabilities);

                } 

       catch (Exception e) {

           System.out.println("Invalid grid URL" + e.getMessage());
                }

        System.out.println("The setup process is completed");

    }


    [@Test](http://twitter.com/Test)
    public void handleMultipleTabs() throws InterruptedException, AWTException {
        // TODO Auto-generated method stub

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        //Navigating to airbnb
        driver.get("[https://www.lambdatest.com](https://www.lambdatest.com)");

        driver.manage().window().maximize();

        String currentHandle= driver.getWindowHandle();

        //locating the blog url
        String urlToClick=driver.findElement(By.xpath("//a[text()='Blog']")).getAttribute("href");


        //opening the new tab
        ((JavascriptExecutor)driver).executeScript("window.open()");

        //getting all the handles currently availabe
        Set&amp;lt;String&amp;gt; handles=driver.getWindowHandles();
        for(String actual: handles)
        {

         if(!actual.equalsIgnoreCase(currentHandle))
         {
             //switching to the opened tab
             driver.switchTo().window(actual);

             //opening the URL saved.
             driver.get(urlToClick);
         }
        }



    }

[@AfterClass](http://twitter.com/AfterClass)
    public void closeDown()
    {
        driver.quit();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above script will help you handle browser tabs in Selenium through an on-cloud Selenium Grid with zero-downtime. You can view the status of these test on the LambdaTest automation dashboard. You can view the video, screenshots, console output, and more as you perform automation testing with Selenium on LambdaTest. The referenced screenshot below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fibs6hyx2kwkbimvw6l8p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fibs6hyx2kwkbimvw6l8p.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console Output of the test:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A_WATjDSyY-WLyqzg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A_WATjDSyY-WLyqzg.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Try &lt;a href="https://www.lambdatest.com/mobile-app-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep05_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;mobile app testing&lt;/a&gt; using LambdaTest’s online real device cloud and virtual testing platform of emulators and simulators here. Reduce costs by completely eliminating your in-house device lab.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We demonstrated automation testing with Selenium to handle multiple tabs using both Action Class &amp;amp; Windows Handler method. We came to realize the pain point of running Selenium WebDriver, &amp;amp; Grid, locally. Moving to cloud-based Selenium Grid such as LambdaTest will help you scale effortlessly, so you could reduce your build times significantly &amp;amp; ship products faster.&lt;/p&gt;

&lt;p&gt;Let me know in case you have any queries regarding this topic. I will be coming up with more articles around fundamental topics of Selenium automation testing, to help you grow better as a professional automation tester. Stay tuned for more &amp;amp; happy testing! 🙂&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>testing</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Using Selenium Webdriver For Full Page Screenshots</title>
      <dc:creator>sadhvisingh1</dc:creator>
      <pubDate>Fri, 02 Sep 2022 11:52:07 +0000</pubDate>
      <link>https://dev.to/testmuai/using-selenium-webdriver-for-full-page-screenshots-511c</link>
      <guid>https://dev.to/testmuai/using-selenium-webdriver-for-full-page-screenshots-511c</guid>
      <description>&lt;p&gt;One of the most performed actions of any webpage tester is taking a screenshot of the webpage. Whenever a tester finds and reports a bug, that bug would not be taken seriously without supporting screenshots or even videos of the issue. This is equally true no matter the type of testing you are doing and that includes &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep12_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;selenium&lt;/a&gt; automation testing.&lt;/p&gt;

&lt;p&gt;In automation testing, especially where a typical test run may involve hundreds of commands and test cases, automated screenshots taken at critical assertions are important for developers and testers in making sure that every test case executed as it should. These proofs are in-turn used for debugging, to find out what went wrong and the reason for failure. For &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep02_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt;, these screenshots help to distinguish whether the failures are due to application failure or due to script failure.&lt;/p&gt;

&lt;p&gt;Now with that said, when we say screenshot we could mean capturing an image of any part of the screen including the image of an element in question or even a screenshot of the whole page. Therefore in this post, we would be looking at how you can take automated screenshots of web pages for different purposes using Selenium WebDriver automation scripts. You can refer to this WebDriver Tutorial to understand more about what WebDriver is, its features, how it works, and best practices. To start off, there are four major ways of capturing screenshot images using Selenium Webdriver. Such as :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Screenshot of Viewable area&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Screenshot of entire screen i.e. capturing screenshot of full webpage&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Screenshot of the desired webElement&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cloud based platform supporting screenshots of AUT&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can also refer to the below video tutorial on How To Take Screenshots In Selenium testing.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/7jrZn27_9FU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Click here to know more about &lt;a href="https://www.lambdatest.com/blog/debug-websites-using-safari-developer-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep02_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Developer Tools for safari&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Automated Selenium Test Scripts for Taking Screenshots of Viewable Area
&lt;/h2&gt;

&lt;p&gt;This is the most used approach of taking screenshots of applications under automation and the easiest one as well. Selenium provides an out-of-the-box capability called TakeScreenShot interface that can be used for taking screenshots of the viewable area.&lt;/p&gt;

&lt;p&gt;You can check the detail of the interface &lt;a href="https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/TakesScreenshot.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This interface provides a method known as getScreenshotAs which helps to capture the screenshot and store it in the desired location.&lt;/p&gt;

&lt;p&gt;Here’s the syntax to capture the screenshot:&lt;/p&gt;

&lt;p&gt;File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);&lt;/p&gt;

&lt;p&gt;In order to store the taken screenshot into a file, the below statement is used:&lt;br&gt;
FileUtils.copyFile(screenshotFile, new File("path of the file you want to save the screenshot to"));&lt;/p&gt;

&lt;p&gt;This is it! Just two statements and you will be able to take the screenshot. Let’s incorporate this step into a code snippet. The below example showcases Airbnb stay details page example where we are taking a screenshot of the viewable screen:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ViewableScreenshotExample {

    WebDriver driver;

    [@BeforeTest](http://twitter.com/BeforeTest)
    public void setupUrl()
    {
        System.setProperty("webdriver.chrome.driver", ".\\Driver\\chromedriver.exe");
        driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("[https://www.airbnb.co.in/s/India/](https://www.airbnb.co.in/s/India/)");


    }

    [@Test](http://twitter.com/Test)
    public void performAction() throws InterruptedException
    {
        //Scroll down the page
        JavascriptExecutor jse = (JavascriptExecutor)driver;
        jse.executeScript("window.scrollBy(0,250)", "");


        //open the stay details page
        driver.findElement(By.xpath("//div[contains(text(), 'Sea Hut Homestay with Aircon')]")).click();
        Thread.sleep(1500);

        //Switch to the required tab
        ArrayList&amp;lt;String&amp;gt; ta = new ArrayList&amp;lt;String&amp;gt; (driver.getWindowHandles());
        int i=ta.size();
        System.out.println(i);
        driver.switchTo().window(ta.get(1));
    }

    [@AfterTest](http://twitter.com/AfterTest)
    public void takeScreenshot()
    {
        //take screenshot of the page
        File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(src, new File("path of the file"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The code snippet above will take the viewable screen screenshot as below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2864%2F0%2Am2lG5EknHbzHYEa9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2864%2F0%2Am2lG5EknHbzHYEa9.png" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is all good and awesome if you have to take a screenshot of the view that your code is testing. However, if I want to take a screenshot of the entire webpage, the above mentioned code is not enough. But we have a solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Capturing Full Webpage Screenshot using Automated Selenium Test Scripts
&lt;/h2&gt;

&lt;p&gt;A need may arise to take screenshots of the entire screen rather than just the viewport of the browsers. Some browsers take a screenshot of the viewable port only whereas others take a screenshot of the entire screen. Earlier versions of Firefox used to take screenshots of the entire screen, unlike chrome and IE. But eventually even the latest versions of Firefox now only takes viewport screenshots. So in order to capture screenshots of the entire screen using selenium web driver scripts, we can make use of AShot().&lt;/p&gt;

&lt;p&gt;AShot() is a webdriver screenshot utility to capture entire page screenshot and is natively supported from Selenium 3 onwards. It provides the following features:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Helps capture entire screen and web element&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Beautify screenshot&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Provides screenshot comparison.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For more details on the utility, you can refer &lt;a href="https://github.com/pazone/ashot" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In order to take a screenshot of the entire screen, you need to add the jar into your project. You can download the jar from here &lt;a href="http://central.maven.org/maven2/ru/yandex/qatools/ashot/ashot/1.5.3/ashot-1.5.3.jar" rel="noopener noreferrer"&gt;http://central.maven.org/maven2/ru/yandex/qatools/ashot/ashot/1.5.3/ashot-1.5.3.jar&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the jars are added into the project, all you need to do is mention the below lines of code when you intend to take the full-screen screenshot:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Screenshot screenshot=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(screenshot.getImage(),"PNG",new File("path of the file"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the code below, the Ashot method shooting strategy is used by setting the viewport to full screen and taking screenshots. This code snippet below goes to Airbnb India Stays and Tours page and takes a screenshot of the full view.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;

public class EntireScreenshot {

public static void main(String[] args) {
        // TODO Auto-generated method stub

    WebDriver driver;


                System.setProperty("webdriver.chrome.driver", ".\\Driver\\chromedriver.exe");
            driver=new ChromeDriver();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.manage().window().maximize();
            driver.get("[https://www.airbnb.co.in/s/India/](https://www.airbnb.co.in/s/India/)");

//take screenshot of the entire page
            Screenshot screenshot=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
            try {
                ImageIO.write(screenshot.getImage(),"PNG",new File("path of the file"));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            driver.quit();

        }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As you run this code, note how the code automatically scrolls down the page and takes the screenshot of the entire page. Below is the example of the taken screenshot.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AS7KoYJE3NtuHP4u_.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AS7KoYJE3NtuHP4u_.png" width="621" height="1600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Taking full pages screenshots is great, but you may encounter a use case where you are concerned with the screenshot of the desired webElement only. Your only concern will be to take a screenshot of the desired element and not the whole screen. Also, if you wish to take screenshots of the logo image or other UI specific elements to check its pixilation or UI issues, all you care about is taking the webElement image rather than the whole screen image. Let dig into how to take a screenshot of a web Element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AvexLxsLJCtTweSek.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AvexLxsLJCtTweSek.jpg" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Say No To Safari VM! Perform Cross Browser Compatibility Testing On All &lt;a href="https://www.lambdatest.com/safari-browser-for-windows?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep02_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Safari for windows&lt;/a&gt; across Real Browsers And Operating Systems here&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Taking Screenshot of Desired Web Element Using Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;Taking screenshot of a desired element is also pretty easy. The main concept is to crop the full screenshot to the desired location of the webElement based on its coordinates and height-width. Here’s a code snippet below highlighting how you can take screenshot of just the logo of Amazon.com website.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.server.handler.FullscreenWindow;

public class LogoScreenShotExample {

public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub



    System.setProperty("webdriver.chrome.driver", ".\\Driver\\chromedriver.exe");
    WebDriver driver=new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.get("[https://www.amazon.in/](https://www.amazon.in/)");

    //locating amazon logo
    WebElement logo=driver.findElement(By.id("nav-logo"));

    // Get entire page screenshot
    File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

    BufferedImage  fullScreen = ImageIO.read(screenshot);

    //Find location of the webelement logo on the page
    Point location= logo.getLocation();

    //Find width and height of the located element logo

    int width= logo.getSize().getWidth();
    int height=logo.getSize().getHeight();


    //Now the main point, which is cropping the full image to get only the logo screenshot
    BufferedImage logoImage= fullScreen.getSubimage(location.getX(), location.getY(),
            width, height);

    ImageIO.write(logoImage, "png", screenshot);

    //copy the file to the desired location
    FileUtils.copyFile(screenshot, new File("path of file"));

}

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here’s the image was taken by the above code snippet of the webElement:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A4Qi3HIwSHZV64Qd5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A4Qi3HIwSHZV64Qd5.png" width="192" height="39"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is it. Isn’t it cool guys. Just find out what your test scenario wants and take the required screenshot. Nowadays we do not need to take screenshots because of so many upcoming cloud-based platforms providing support to all those screenshots and videos to your automation scripts execution.&lt;/p&gt;

&lt;p&gt;This brings me to my last approach of taking a screenshot which eventually means not taking one ? and the tool doing its job. Well, you heard it right. Let’s look into its details&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Check out this- 16 Best &lt;a href="https://www.lambdatest.com/blog/best-android-emulators/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep02_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Android Emulators&lt;/a&gt; For Windows &amp;amp; Mac In 2022&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Taking Full Page Automated Screenshots On Cloud
&lt;/h2&gt;

&lt;p&gt;Running tests locally is important however if you want to make sure that your website works on all browsers, even those you don’t have access to locally, you would need service like LambdaTest. LambdaTest is a cloud based selenium grid that you can use to run all your automated selenium test scripts online. The best part about LambdaTest grid, however, is that it takes automated screenshots of your webpage after the execution of each selenium command. In addition, LambdaTest platform takes a complete video of the test execution as well. All you need to do is run your scripts on this platform which provides you features like screenshots, videos, network logs, console logs and so on. Few points of consideration or pre-conditions to make your script running on the platform is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;LambdaTest account&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;LambdaTest Username, access key and URL to connect to&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Setting up the required properties to gain access to the features you need.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is it. Now let’s run our same above-mentioned code of Airbnb Stays detail page without the need of providing the screenshot method since it captures the whole video of the execution. In the below code snippet I will be using LambdaTest username, access key, and LambdaTest Selenium Grid URL to connect to the required browser and perform actions. Notice that changing the above code into LambdaTest compatible code required only to call remote webdriver instead of local chrome webdriver, and passing on a desired-capabilities object to define which browser you need to run tests on.:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.net.URL;

public class LambdatestWithoutScreenShotMethod {

public static final String  username= "sadhvisingh24";
    public static final String auth_key = "123456789";
    public RemoteWebDriver driver;
    public static final String URL= "[@hub](http://twitter.com/hub).lambdatest.com/wd/hub";

    [@BeforeTest](http://twitter.com/BeforeTest)
    public void setupUrl()
    {

            DesiredCapabilities capability= new DesiredCapabilities();
            capability.setPlatform(Platform.WIN8);
            capability.setBrowserName("chrome");
            capability.setVersion("75.0");
            capability.setCapability("build", "cross_browser");
            capability.setCapability("name", "cross_browser");
            capability.setCapability("network", true);//to enable network logs
            capability.setCapability("visual", true);//to enable screenshots
            capability.setCapability("video", true);//to enable video
            capability.setCapability("console", true);//to enable console logs

             try {

             driver = new RemoteWebDriver(new URL("https://" + username + ":" + auth_key + URL), capability);

                 } 

           catch (Exception e) {

               System.out.println("Invalid grid URL" + e.getMessage());
                 }

                 try
                {
                  driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
                    driver.manage().window().maximize();

driver.get("[https://www.airbnb.co.in/s/India/](https://www.airbnb.co.in/s/India/)");

}
                catch (Exception e) {
             System.out.println(e.getMessage());
                    }
        }   


    [@Test](http://twitter.com/Test)
    public void performAction() throws InterruptedException
    {
        //Scroll down the page
        JavascriptExecutor jse = (JavascriptExecutor)driver;
        jse.executeScript("window.scrollBy(0,250)", "");


        //open the stay details page
        driver.findElement(By.xpath("//div[contains(text(), 'Sea Hut Homestay with Aircon')]")).click();
        Thread.sleep(1500);

        //Switch to the required tab
        ArrayList&amp;lt;String&amp;gt; ta = new ArrayList&amp;lt;String&amp;gt; (driver.getWindowHandles());
        int i=ta.size();
        System.out.println(i);
        driver.switchTo().window(ta.get(1));
    }

    [@AfterTest](http://twitter.com/AfterTest)
    public void closeSetup()
    {
        driver.quit();
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The referenced screenshot below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A9A4MosFMl3t2ucJ8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A9A4MosFMl3t2ucJ8.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the screenshot above, LambdaTest provides support of video where you can view the whole execution flow of your web app.&lt;/p&gt;

&lt;p&gt;Apart from this LambdaTest also provides a standalone full paged automated screenshot feature, that can help you to take screenshot across browsers of your designated application and even compare them. LambdaTest calls this feature as ‘Screenshot testing’. You can access these screenshots as and when required and even share them with the required stakeholders and mail them as per needs. This feature comes in handy when you got to test your application across multiple browsers and versions and perform cross browser testing. You can take screenshots and compare them for any UI issues. Isn’t it miraculous and just imagine the amount of time you just saved.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to the LambdaTest ‘Visual UI testing’ menu header and navigate to its sub-header ‘Screenshot’ as below&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once you reach this page, all you need to do is place the URL you intend to test in the URL placeholder. Select the required browser and version you wish to take a screenshot on and just click on the button ‘capture’. Bingo, that’s it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The images to be captured will be added in the queue. Once it’s completed, you can access those images, as below:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A_22cN4NeiuouH8cn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A_22cN4NeiuouH8cn.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AbgQCQbLPkvCuD7Of.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AbgQCQbLPkvCuD7Of.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you saw above, we focused on all possible ways of taking screenshots in detail, so next time when you are stuck on how to take a screenshot, this article will come in handy. You can also refer to my other articles as part of this series. Happy testing.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>webdev</category>
      <category>web3</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Find Elements With Link Text &amp; Partial Link Text In Selenium</title>
      <dc:creator>sadhvisingh1</dc:creator>
      <pubDate>Fri, 02 Sep 2022 10:22:59 +0000</pubDate>
      <link>https://dev.to/testmuai/find-elements-with-link-text-partial-link-text-in-selenium-bal</link>
      <guid>https://dev.to/testmuai/find-elements-with-link-text-partial-link-text-in-selenium-bal</guid>
      <description>&lt;p&gt;CSS locators in Selenium are a fundamental concept that every tester who aims to perform automation testing with Selenium, should be aware of. Proficient use of CSS locators in Selenium can help you perform testing in a more efficient &amp;amp; thorough manner. I have been in the automation testing industry from 7 years now, &amp;amp; I often observe that testers when performing Selenium, tend to forget the purpose of each CSS locator. Freshers have a tough time understanding them &amp;amp; experienced tester often sticks to a couple of selectors for locating elements on a webpage as they execute automation testing with Selenium.&lt;/p&gt;

&lt;p&gt;There have been times where I see an experienced automation tester taking a longer route for finding an element as they have a habit of sticking to their favorite locator. It is why I thought I would come up with a tutorial series for CSS Selenium locators to help budding automation testers come up with strategic implementation of these locators. And to those of us who are experienced, it would be a quick and good recap.&lt;/p&gt;

&lt;p&gt;This article will represent the practical implementation of link text &amp;amp; partial link text, as we perform automation testing with Selenium. The links on any web application can help in locating an element with either exact match of the text or through a partial match. Using link text &amp;amp; partial link text in Selenium, we will be able to locate both of these matches. This is the last article of my tutorial series on CSS Locator in Selenium.&lt;/p&gt;

&lt;p&gt;You can check out other articles around different CSS locator in Selenium that helps in locating elements through various ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/making-the-move-with-id-locator-in-selenium-webdriver/" rel="noopener noreferrer"&gt;ID locator in Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/how-to-use-name-locator-in-selenium-automation-scripts/" rel="noopener noreferrer"&gt;Name locator in Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/selenium-java-tutorial-class-name-locator-in-selenium/" rel="noopener noreferrer"&gt;Class Name locator in Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/how-to-use-name-locator-in-selenium-automation-scripts/" rel="noopener noreferrer"&gt;Tagname locator in Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/how-pro-testers-use-css-selectors-in-selenium-automation-scripts/" rel="noopener noreferrer"&gt;CSS Selector in Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/" rel="noopener noreferrer"&gt;XPath in Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are an advanced or medium Selenium practitioner, then you can chuck on dedicated articles mentioned above. And go for our complete guide to help you illustrate the practical demonstration of CSS locator in Selenium.&lt;/p&gt;

&lt;p&gt;With that said, let’s find out how to leverage link text &amp;amp; partial link text in Selenium to locate elements on a web page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A0EfbhOfFJFkLSBb5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A0EfbhOfFJFkLSBb5.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This certification is for anyone who wants to stay ahead among professionals who are growing their career in Selenium automation testing.&lt;/p&gt;

&lt;p&gt;Here’s a short glimpse of the Selenium 101 certification from LambdaTest:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/qx9FPFfJm7E"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;a href="https://www.lambdatest.com/mobile-emulator-for-app-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep02_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Android emulator for ios&lt;/a&gt; - Test your mobile applications before going live here. All you need is a computer with internet connection.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Link Text In Selenium To Locate An Element
&lt;/h2&gt;

&lt;p&gt;In order to access link using link text in &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep12_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt;, the below-referenced code is used:&lt;br&gt;
driver.findElement(By.linkText("this is a link text"));&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In a scenario where, multiple links having similar text exists, it will automatically select the first one.&lt;/p&gt;

&lt;p&gt;Let’s refer the code snippet below to understand the use case. In this case, we are taking Airbnb example, where we are clicking on any one stay of Goa through the link match.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F0%2ACq2Z3V5GnQo0R0jM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2732%2F0%2ACq2Z3V5GnQo0R0jM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Referenced screenshot of the div element with the link text:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AjjgIxEQhXsqRCpMi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AjjgIxEQhXsqRCpMi.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;

public class LinkText {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.setProperty("webdriver.chrome.driver", ".\\ChromeDriver\\chromedriver.exe");
    WebDriver driver=new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    driver.manage().window().maximize();

    //Opening the air bnb Goa homestays page
    driver.get("[https://www.airbnb.co.in/s/Goa/all](https://www.airbnb.co.in/s/Goa/all)");

    //locating an element via link text in Selenium now, and clicking on that stay
    driver.findElement(By.linkText("Standard One Bedroom Suite with Pool &amp;amp;amp; Jacuzzi")).click();

driver.quit();
  }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We can locate the same element using partial link text in Selenium as well. Let’s check how!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Click here if you want to know How To Use &lt;a href="https://www.lambdatest.com/blog/iphone-simulators-on-windows/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep02_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;IOS emulator for pc&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Partial Link Text In Selenium To Locate An Element
&lt;/h2&gt;

&lt;p&gt;Partial link text in Selenium is another way of locating an element via a link. The only difference from the link text in Selenium to partial link text is that &lt;strong&gt;it does not look into the exact match of the string value but work on a partial match&lt;/strong&gt;. So, in case you are looking for a link with a bigger text length, you can get away from using only the partial link text rather than the whole link text in Selenium.&lt;/p&gt;

&lt;p&gt;Syntax of locating element by partial link text.&lt;/p&gt;

&lt;p&gt;driver.findElement(By.partialLinkText ("link text"));&lt;/p&gt;

&lt;p&gt;Referencing the above scenario, below is the code snippet for partial link text of the same stay of Airbnb:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;

public class PartialLinkText {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.setProperty("webdriver.chrome.driver", ".\\ChromeDriver\\chromedriver.exe");
    WebDriver driver=new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    driver.manage().window().maximize();

    //Opening the air bnb Goa homestays page
    driver.get("[https://www.airbnb.co.in/s/Goa/all](https://www.airbnb.co.in/s/Goa/all)");

    //locating an element via link text now and clicking on that stay
    driver.findElement(By.partialLinkText("Pool &amp;amp;amp; Jacuzzi")).click();

driver.quit();
  }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test your mobile websites and smartphone apps using our scalable on cloud &lt;a href="https://www.lambdatest.com/mobile-emulator-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep02_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;mobile emulator online&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How To Select The Right Link Text When You Have Multiple Match Results?
&lt;/h2&gt;

&lt;p&gt;The only thing to remember and to be cautious of while using partial link text in Selenium is when partial link text ends up matching with multiple link text on the page. In this case, make sure, you are clicking on the desired one.&lt;/p&gt;

&lt;p&gt;Let’s consider another scenario as we perform automation testing with Selenium, where you end up matching multiple link text and wish to target only the designated one. For the same homestays of Goa, I am trying to locate the element with partial text as ‘pool’ in it. So, the strategy would be to find out home many stays have a pool in them and click on the required stay, post that. The reference code snippet below represents how we can pick the right target out of multiple match results using the partial link text in Selenium.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.List;
import java.util.concurrent.TimeUnit;

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

public class LinkText {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.setProperty("webdriver.chrome.driver", ".\\ChromeDriver\\chromedriver.exe");
    WebDriver driver=new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    driver.manage().window().maximize();

    //Opening the air bnb Goa homestays page
    driver.get("[https://www.airbnb.co.in/s/Goa/all](https://www.airbnb.co.in/s/Goa/all)");

    //locating an element via link text now and clicking on that stay
    List&amp;amp;lt;WebElement&amp;amp;gt; poolNumber=driver.findElements(By.partialLinkText("Pool"));

    //find the number of links with the text as pool
    int numberOfStaysWithPool= poolNumber.size();

    System.out.println(numberOfStaysWithPool);

    for(int k=0; k&amp;amp;lt;numberOfStaysWithPool; k++)
    {
      //printing all those links
      System.out.println(poolNumber.get(k).getText());

    }

    //select the luxury three bedroom apartment link
    poolNumber.get(2).click();

driver.quit();
  }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3190%2F1%2AajBeUVHzbqoGuVjnPgAR_w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3190%2F1%2AajBeUVHzbqoGuVjnPgAR_w.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above code snippet, I have used &lt;strong&gt;findElements&lt;/strong&gt; since I am supposed to receive multiple web elements with partial text as ‘pool’. Now, using an index, I have navigated to one of the links, which I wish to click. Simple, isn’t it?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Both, link text &amp;amp; partial link text are cases sensitive as CSS locator in Selenium.&lt;/p&gt;

&lt;p&gt;For example, Assume that a link ‘&lt;strong&gt;Register&lt;/strong&gt;’ is present on a home page and a similar link ‘&lt;strong&gt;REGISTER&lt;/strong&gt;’ is present on the footer of the homepage. In this case, if you are locating with link text ‘&lt;strong&gt;REGISTER&lt;/strong&gt;’, it will automatically select the link in the footer and not the other one.&lt;/p&gt;

&lt;p&gt;Link text and partial link text locator in Selenium works only on links of a given web application. If you intend to locate elements other than links you cannot use link text or partial link text locator in Selenium. If you are dealing with links in your application, then this is perhaps the best locator to go with. Happy testing! 🙂&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Selenium Java Tutorial – Class Name Locator In Selenium</title>
      <dc:creator>sadhvisingh1</dc:creator>
      <pubDate>Wed, 17 Jul 2019 12:14:45 +0000</pubDate>
      <link>https://dev.to/testmuai/selenium-java-tutorial-class-name-locator-in-selenium-11fa</link>
      <guid>https://dev.to/testmuai/selenium-java-tutorial-class-name-locator-in-selenium-11fa</guid>
      <description>&lt;p&gt;CSS Locator in &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep12_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; is one of the most important aspects of writing a script. If you cannot locate an element by using any CSS locator in Selenium, then being proficient at Selenium automation will be a tough task. Selenium provides multiple ways of locating an element.&lt;/p&gt;

&lt;p&gt;You can check out other articles around different CSS locator in Selenium that helps in locating elements through various ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/using-link-text-and-partial-link-text-in-selenium/" rel="noopener noreferrer"&gt;Link Text &amp;amp; Partial Link Text In Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/making-the-move-with-id-locator-in-selenium-webdriver/" rel="noopener noreferrer"&gt;ID locator in Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/how-to-use-name-locator-in-selenium-automation-scripts/" rel="noopener noreferrer"&gt;Name locator in Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/locating-elements-by-tagname-in-selenium/" rel="noopener noreferrer"&gt;Tagname locator in Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/how-pro-testers-use-css-selectors-in-selenium-automation-scripts/" rel="noopener noreferrer"&gt;CSS Selector in Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/" rel="noopener noreferrer"&gt;XPath in Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are an advanced or medium Selenium Java practitioner, then you can chuck on dedicated articles mentioned above. I have written a complete guide to help you illustrate the practical demonstration of CSS locator in Selenium.&lt;/p&gt;

&lt;p&gt;In this Selenium Java tutorial, I will be referencing on &lt;strong&gt;Class name locator in Selenium&lt;/strong&gt; to demonstrate how to locate an element on a webpage via class name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started With Class Name Locator In Selenium With Example
&lt;/h2&gt;

&lt;p&gt;Next, in this Selenium Java tutorial, we will consider the scenario of Airbnb, where we intend to locate the ‘Where’ field in the search form of Airbnb homepage via class name. Below is a screenshot of Airbnb page where we inspect the ‘Where’ field in the form.&lt;br&gt;
image:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuap7aqrmqnnurgvcwbr8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuap7aqrmqnnurgvcwbr8.png" width="800" height="343"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In-order to use Class name locator in Selenium we need to use the below syntax:&lt;br&gt;
findElement(By.className("_up0kwni "))&lt;/p&gt;

&lt;p&gt;Now, let’s look into the code for finding elements by Class name in locators:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

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

public class ClassNameLocator {

public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", ".\\ChromeDriver\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        driver.manage().window().maximize();

        //Opening the air bnb home page
        driver.get("[https://www.airbnb.co.in/](https://www.airbnb.co.in/)");

        //Locating location field for the search  form via class name 
        driver.findElement(By.className("_up0kwni")).sendKeys("Goa", Keys.ENTER);

//Locating check-in field for the search  form via class name 
        driver.findElement(By.className("_14fdu48d")).click();

        //Locating the date 12th June for check-in field
        driver.findElement(By.className("_1wh4xpp1")).click();

        //closing the driver
        driver.quit();

}

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Pretty simple, isn’t it? In the upcoming session of this Java Selenium tutorial we will learn how to handle a common exception for class name Selenium locator.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjll0kbh6q5zqtl9p4zq9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjll0kbh6q5zqtl9p4zq9.png" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  One Of The Most Common Exceptions For Class Name Locator In Selenium
&lt;/h2&gt;

&lt;p&gt;Another interesting fact and a popular error you may come across while using Class name locator in Selenium would be something like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frc0ugl6d4z9ncrlgn365.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frc0ugl6d4z9ncrlgn365.png" width="800" height="33"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I am sure, you may have come across this error. Lets try to incorporate this scenario in the below code snippet for the Facebook sign up page. Below is the DOM structure for facebook ‘first name’ field with class name attribute highlighted below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvcwda5ec136n9umta4nw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvcwda5ec136n9umta4nw.png" width="800" height="49"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Referenced code snippet, trying to access the first name field using the class name locator in Selenium:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

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

public class ClassNameLocator {

public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", ".\\ChromeDriver\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        driver.manage().window().maximize();

        //Opening the air bnb home page
        driver.get("[https://www.facebook.com/](https://www.facebook.com/)");

        //Locating by firstname via class name 
        driver.findElement(By.className("inputtext _58mg _5dba _2ph-")).sendKeys("Sadhvi");


        //closing the driver
        driver.quit();

}

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Referenced Console error below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frvjp0g0opys00wjsbmyq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frvjp0g0opys00wjsbmyq.png" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How to handle this error? Well, Selenium considers this as a compound class, which means more than one classes marked through spaces. So, any class name with spaces in it will be considered two or three or more classes.&lt;/p&gt;

&lt;p&gt;In this case, this class name marked as ‘inputtext _58mg _5dba _2ph-‘ contains three spaces thereby making it three different classes. Hence, &lt;strong&gt;Selenium mentions the error stating it cannot find multiple classes together&lt;/strong&gt;. In this case, you can opt to locate element by &lt;strong&gt;CSS selector in Selenium&lt;/strong&gt; or by &lt;strong&gt;XPath in Selenium&lt;/strong&gt; using the &lt;strong&gt;class name attribute&lt;/strong&gt;. The referenced code snippet below:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

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

public class ClassNameLocator {

public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", ".\\ChromeDriver\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        driver.manage().window().maximize();

        //Opening the facebook home page
        driver.get("[https://www.facebook.com/](https://www.facebook.com/)");

        //Locating by firstname via class name 
        driver.findElement(By.xpath("//input[[@class](http://twitter.com/class)='inputtext _58mg _5dba _2ph-']")).sendKeys("Sadhvi");


        //closing the driver
        driver.quit();

}

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test your web and mobile apps on &lt;a href="https://www.lambdatest.com/android-emulator-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep01_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;online emulators android&lt;/a&gt; here. Ensure your apps are compatible across latest and legacy Android operating systems, devices, and browsers.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How To Locate An Element When We Have Multiple Elements Sharing The Same Class Name?
&lt;/h2&gt;

&lt;p&gt;Now you know when to use the class name and when you cannot use class names. But did you ever imagine the scenario when you have multiple elements sharing the same class name? How do you tackle that situation? This is again something, you can simply achieve by using the &lt;strong&gt;findElements keyword&lt;/strong&gt;. All you need to do is, locate all elements with that class name using the findElements keyword and iterate through the required element class name via index. Having said so, I would rather suggest looking into an alternative method of locating that element rather than this. As its tendency of breaking will be pretty high and may lead to errors.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftnd6x63sfu0krxydzbq1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftnd6x63sfu0krxydzbq1.png" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This certification is for anyone who wants to stay ahead among professionals who are growing their career in Selenium automation testing.&lt;/p&gt;

&lt;p&gt;Here’s a short glimpse of the Selenium 101 certification from LambdaTest:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/qx9FPFfJm7E"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test your web and mobile apps on &lt;a href="https://www.lambdatest.com/android-emulator-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep01_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;online Android Emulators&lt;/a&gt; here. Ensure your apps are compatible across latest and legacy Android operating systems, devices, and browsers.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Example For Class Name Locator In Selenium For Multiple Elements With A Similar Class Name
&lt;/h2&gt;

&lt;p&gt;Let’s consider the below example, highlighting the scenario above. In this case, we are considering the LinkedIn sign-up page, where all fields share the same class name. In this case, we need to note two important things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;If no, the index is defined, then by default selenium selects the first element it encounters with that class name. In the code snippet below, it locates the first element which is the first name:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ClassNameLocator {

public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", ".\\ChromeDriver\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        driver.manage().window().maximize();

        //Opening the linkedin sign up home page
        driver.get("[https://www.linkedin.com/start/join](https://www.linkedin.com/start/join)");

        //Locating by firstname via class name 
        driver.findElement(By.className("cell-body-textinput")).sendKeys("Sadhvi");

        //closing the driver
        driver.quit();

}

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: Take a look at the class name, which in this case is cell-body-textinput, since it is not marked with spaces hence it is considered as a single class.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Locating the different elements with the same class name using index. The referenced snippet below:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class ClassNameLocator {

public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", ".\\ChromeDriver\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        driver.manage().window().maximize();

        //Opening the air bnb home page
        driver.get("[https://www.linkedin.com/start/join](https://www.linkedin.com/start/join)");

        //Locating by firstname via class name 
        List signUpForm=driver.findElements(By.className("cell-body-textinput"));

        //finding the number of elments with the same class name
        int size=signUpForm.size();

        System.out.print(size);

        //locating the first name locator
        signUpForm.get(0).sendKeys("Sadhvi");

        //locating the last name locator
        signUpForm.get(1).sendKeys("Singh");

//locating the email  locator
        signUpForm.get(2).sendKeys("[sadhvisingh24@gmail.com](mailto:sadhvisingh24@gmail.com)");

        //locating the password  locator
        signUpForm.get(3).sendKeys("password");

        //closing the driver
        //driver.quit();

}

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Console output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffjhxhat9wbxkvasjuqlt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffjhxhat9wbxkvasjuqlt.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Bingo, you are good to go now. This was all about class name locator in Selenium.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test your web and mobile apps on &lt;a href="https://www.lambdatest.com/android-emulator-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep01_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Android online Emulators&lt;/a&gt; here. Ensure your apps are compatible across latest and legacy Android operating systems, devices, and browsers.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What Did We Learn About Class Name Locator In Selenium?
&lt;/h2&gt;

&lt;p&gt;Well, that was all for today’s Selenium Java tutorial for CSS locator in Selenium. I am sure by far you have developed a deep understanding on how to use the Class name locator in Selenium effectively. We came across the most common error across the implementation of Class name locator in Selenium. We also learned how to overcome complicated scenarios where multiple elements are sharing a similar class name. If you are new to Selenium. You can read this tutorial on Selenium Java Introduction. Adios, happy testing! 🙂&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>webdev</category>
      <category>techtalks</category>
      <category>testing</category>
    </item>
    <item>
      <title>Selenium Java Tutorial – How To Test Login Process?</title>
      <dc:creator>sadhvisingh1</dc:creator>
      <pubDate>Mon, 08 Jul 2019 13:41:05 +0000</pubDate>
      <link>https://dev.to/testmuai/selenium-java-tutorial-how-to-test-login-process-2987</link>
      <guid>https://dev.to/testmuai/selenium-java-tutorial-how-to-test-login-process-2987</guid>
      <description>&lt;p&gt;Automation testing at first may sound like a nightmare especially when you have been into the manual testing business for quite long. Looking at the pace with which the need for automation testing is arising, it has become inevitable for website testers to deep dive into the automation river and starts swimming. To become a pro swimmer it takes time, similar is the case with becoming a successful automation tester. It requires knowledge &amp;amp; deep understanding of numerous automation tools &amp;amp; frameworks. As a beginner in automation testing, you may be looking forward to grabbing your hands on an open-source testing framework. After doing so the question that arises is what next? How do I go about using the open-source tool, framework, or platform, &amp;amp; I am here to help you out in that regard. Today, we will be looking at one of the most renowned open-source automation testing frameworks known as &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt;. In this &lt;strong&gt;Selenium Java tutorial&lt;/strong&gt;, I will demonstrate a Selenium login example with Java to help you automate the login process.&lt;/p&gt;

&lt;p&gt;Automating login process using Selenium with Java or any other programming language is the very first baby step towards becoming a successful automation tester. Without further ado, let’s get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some Prerequisites For Selenium Java Tutorial
&lt;/h2&gt;

&lt;p&gt;Before we begin with our &lt;strong&gt;Selenium Java&lt;/strong&gt; tutorial for the login process, we need to be aware of the prerequisites. To begin with, all applications regardless of the domain they may target, 95% of the time have a login functionality flow associated with them. Be it e-commerce, banking, medical, education, etc all request user to login to the application for further usage. As the name suggests “&lt;strong&gt;Selenium Java Tutorial&lt;/strong&gt;”, this tutorial offers a basic level understanding to help beginners getting started with automation testing using Selenium &amp;amp; Java. We will be looking into a Selenium login example with Java(one of the most versatile languages used for multiple techniques and field). In order to kick-start, you need to have a basic understanding of Java. Do not get freaked out from hearing the word Java &amp;amp; Selenium. We will take it to step by step. So how do you start off? First, make sure you have all the prerequisites to start writing your first login script using Java &amp;amp; Selenium. You need to have the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Download and Install JDK(Java Development Kit) from here.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install Eclipse from the official website.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Download the &lt;strong&gt;Selenium Java&lt;/strong&gt; Client version from here.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Driver executable- depending upon the browser you wish to execute your script in, you can choose to download its Selenium executable from here. As you go down the page, various browsers like Chrome, Mozilla, Opera, Edge, etc drivers will be available for download to help you perform Selenium.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is all. Open eclipse and create your project. Add your Selenium jar in your Java build path and you are good to go. On details on how to setup project and jar file refer the below link:&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Steps For A Selenium Test Case
&lt;/h2&gt;

&lt;p&gt;Before we perform automation testing for login validation using Selenium &amp;amp; Java, there are some basic steps that need to be followed for whichever test case you intend to write, be it login, logout, profile, dashboard etc. If you follow them, you will never have incomplete test cases in your automation suite:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a Selenium WebDriver instance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Configure your browser if required(for example maximize browser, disable browser notifications etc.).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Navigate to the required URL (Web page).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Locate the HTML element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Perform action on the located HTML element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Verify and validate the action (concluded step).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Take screenshots and generate report using framework for the test cases.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you are planning to devise an automation testing strategy for your organization but are confused about where to start from? Follow my blog to start automation testing from scratch.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fip3galxzj53fdf3iqczs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fip3galxzj53fdf3iqczs.png" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This certification demonstrates your knowledge of Selenium and Java, and your expertise at automating tests for any project.&lt;/p&gt;

&lt;p&gt;Here’s a short glimpse of the Selenium Java 101 certification from LambdaTest:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/vUxfvuAI7kE"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test your mobile websites and smartphone apps using our scalable on cloud mobile &lt;a href="https://www.lambdatest.com/mobile-emulator-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep06_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;emulator online&lt;/a&gt; here&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s Automate Selenium Login With Java
&lt;/h2&gt;

&lt;p&gt;The script I will be mentioning in the article below, will be referencing these steps. We will not be considering step seven, as that would require a dedicated article on itself &amp;amp; I plan to do so in my upcoming blogs, so stay tuned! Now, let us look into those steps in detail to help us perform automation testing using Selenium for login with Java:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Create A Selenium WebDriver Instance
&lt;/h2&gt;

&lt;p&gt;Webdriver driver=new ChromeDriver();&lt;/p&gt;

&lt;p&gt;In order to launch the website in the desired browser, you need to set the system properties to the path of the driver for the required browser. In this &lt;strong&gt;Selenium Java&lt;/strong&gt; tutorial, we will use chromedriver for demonstrating Selenium login example with Java. The syntax for the same will be:&lt;br&gt;
System.setProperty(“webdriver.chrome.driver”, “File path for the Exe”);&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Configure Your Browser If Required
&lt;/h2&gt;

&lt;p&gt;Based on the needs, we can configure the browser. For example, in this &lt;a href="https://www.lambdatest.com/blog/selenium-webdriver-tutorial-with-examples/" rel="noopener noreferrer"&gt;**Selenium Java&lt;/a&gt; tutorial** regarding Selenium login with Java, a browser by default, will be minimized mode, we can set up the browser in the maximize mode. Below is the syntax used for the same.&lt;br&gt;
driver.manage().window().maximize();&lt;/p&gt;

&lt;p&gt;Other things that you can do for configuring your browser is set up different options like disabling info bars, browser notifications, adding extensions, etc. You can also use the &lt;strong&gt;capabilities class&lt;/strong&gt; to run your script on various browsers thereby helping in cross browser testing.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Navigate To The Required URL
&lt;/h2&gt;

&lt;p&gt;Pretty simple, open the browser with the desired URL. All you have to do is write the below syntax and you have your URL open in the desired instantiated browser.&lt;br&gt;
driver.get(“&lt;a href="https://www.linkedin.com/login" rel="noopener noreferrer"&gt;https://www.linkedin.com/login&lt;/a&gt;”);&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Locate The HTML Element
&lt;/h2&gt;

&lt;p&gt;This is the heart of writing a Selenium script. For this to function, you need to have a clear understanding of the different locators used to find the HTML element. You can refer my below articles that talks about the different locators available in selenium and how to locate the element with different examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/making-the-move-with-id-locator-in-selenium-webdriver/" rel="noopener noreferrer"&gt;ID locator in Selenium WebDriver&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/how-to-use-name-locator-in-selenium-automation-scripts/" rel="noopener noreferrer"&gt;Name Locator in Selenium WebDriver&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/locating-elements-by-tagname-in-selenium/" rel="noopener noreferrer"&gt;TagName Locator in Selenium WebDriver&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/how-pro-testers-use-css-selectors-in-selenium-automation-scripts/" rel="noopener noreferrer"&gt;CSS Selector in Selenium WebDriver&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/" rel="noopener noreferrer"&gt;XPath in Selenium WebDriver&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, lets try to locate the email and password field of the login form of LinkedIn&lt;/p&gt;

&lt;p&gt;Below is the DOM structure for the email input box:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuxcozbemycb3p76a8cd6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuxcozbemycb3p76a8cd6.png" width="800" height="50"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can locate it via ID locator in Selenium WebDriver as below:&lt;br&gt;
driver.findElement(By.id(“username”));&lt;/p&gt;

&lt;p&gt;Since this returns a webelement, you can store it in webelement variable as below&lt;br&gt;
WebElement username=driver.findElement(By.id(“username”));&lt;/p&gt;

&lt;p&gt;The same can be achieved for password and login button field which is&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.findElement(By.id(“password”));

WebElement password=driver.findElement(By.id(“password”));

driver.findElement(By.xpath(“//button[text()=’Sign in’]”));

WebElement login= driver.findElement(By.xpath(“//button[text()=’Sign in’]”));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  5. Perform Action On The Located HTML Element
&lt;/h2&gt;

&lt;p&gt;Once located, you need to perform the desired action which in our case is sending text to email and password field and clicking on the login button. To execute this action in Selenium login example with Java, we make use of methods as &lt;strong&gt;sendKeys&lt;/strong&gt; and &lt;strong&gt;click&lt;/strong&gt; provided by Selenium as below:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;username.sendKeys(“xyz@gmail.com”);

password.sendKeys(“exampleAboutSelenium123”);

login.click();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And guess what, you just finished writing the most important parts of the script. Now, in this &lt;strong&gt;Selenium Java&lt;/strong&gt; Tutorial, you only need to ensure these actions have successfully logged in the user, which comes to our final step of script creation for using Selenium to login with Java.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Verify &amp;amp; Validate The Action
&lt;/h2&gt;

&lt;p&gt;In order to validate the results, all you need to do is use assertion. Assertions are vital for comparing the expected results vs the actual results. Almost similar to your test cases, wherein each test case has an actual and expected behavior to it. If it matches, the test case pass, if not, then the test case fails. Assertions do exactly the same. Assertion class is provided by both JUnit and TestNG framework, you can opt to choose any. The below syntax will help to assert (validate) the outcome from actions by performing Selenium login with Java.&lt;/p&gt;

&lt;p&gt;Assert.assertEquals(String actual, String expected);&lt;/p&gt;

&lt;p&gt;So, in this case, we will save our actual url post login into a string value which is:&lt;br&gt;
String actualUrl=” &lt;a href="https://www.linkedin.com/feed/%E2%80%9D" rel="noopener noreferrer"&gt;https://www.linkedin.com/feed/”&lt;/a&gt;;&lt;/p&gt;

&lt;p&gt;And expected URL can be found from the below method:&lt;/p&gt;

&lt;p&gt;String expectedUrl= driver.getCurrentUrl();&lt;/p&gt;

&lt;p&gt;So your final assertion would become as:&lt;/p&gt;

&lt;p&gt;Assert.assertEquals(actualUrl, expectedUrl);&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In order to use assertion, you need to use the annotations of TestNG or JUnit ‘&lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;’ for assertions to function. In case, right now you don’t want to get into the hassle of going into the framework keywords, you can simply match the string using an ‘if’ statement and print the results in console accordingly, something like below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(actualUrl.equalsIgnoreCase(expectedUrl))
{
System.out.println(“Test passed”)
}
else
{
System.out.println(“Test failed”)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Bingo!! You have executed automation testing using Selenium login example with Java.&lt;/p&gt;

&lt;p&gt;If you are curious about using annotations then follow our blog on Selenium Java Tutorial On JUnit Annotations In Selenium With Examples.&lt;/p&gt;

&lt;p&gt;Below is the collective code of all the statements explained above using assertions.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class LoginUsingSelenium {

[@Test](http://twitter.com/Test)
    public void login() {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "path of driver");
        WebDriver driver=new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("[https://www.linkedin.com/login](https://www.linkedin.com/login)");

        WebElement username=driver.findElement(By.id("username"));
        WebElement password=driver.findElement(By.id("password"));
        WebElement login=driver.findElement(By.xpath("//button[text()='Sign in']"));

        username.sendKeys("[example@gmail.com](mailto:example@gmail.com)");
        password.sendKeys("password");
        login.click();

        String actualUrl="[https://www.linkedin.com/feed/](https://www.linkedin.com/feed/)";
        String expectedUrl= driver.getCurrentUrl();

        Assert.assertEquals(expectedUrl,actualUrl);


    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frs51z7jz5i3ngde49rb6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frs51z7jz5i3ngde49rb6.png" width="800" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Below is the collective code of all the statements explained above using the if statement:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

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

public class LoginUsingSelenium {

public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", " path of driver ");
        WebDriver driver=new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("[https://www.linkedin.com/login](https://www.linkedin.com/login)");

        WebElement username=driver.findElement(By.id("username"));
        WebElement password=driver.findElement(By.id("password"));
        WebElement login=driver.findElement(By.xpath("//button[text()='Sign in']"));

        username.sendKeys("[example@gmail.com](mailto:example@gmail.com)");
        password.sendKeys("password");
        login.click();

        String actualUrl="[https://www.linkedin.com/feed/](https://www.linkedin.com/feed/)";
        String expectedUrl= driver.getCurrentUrl();

        if(actualUrl.equalsIgnoreCase(expectedUrl))
        {
            System.out.println("Test passed");
        }
        else
        {
            System.out.println("Test failed");
        }



    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ao5ya6bqd9p9li6ei2v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ao5ya6bqd9p9li6ei2v.png" width="800" height="186"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test your mobile websites and smartphone apps using our scalable on cloud mobile &lt;a href="https://www.lambdatest.com/mobile-emulator-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep06_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;emulator online&lt;/a&gt; here&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Moving Selenium Tests On Cloud
&lt;/h2&gt;

&lt;p&gt;Selenium empowers automation testers to fast track their efforts &amp;amp; test cycles. However, other than the benefits of Selenium WebDriver, there comes some cons too. The most prominent ones include the sequential execution of tests which can take a while for larger automated test suites. Keeping that in mind, the Selenium Grid was introduced to help people run Selenium tests in parallel. However, there is a downside to that as well &amp;amp; that is test coverage &amp;amp; CPU consumption. You can only perform browser compatibility testing on browsers that are installed on your local machine. Installing numerous browsers isn’t a feasible consideration.&lt;/p&gt;

&lt;p&gt;This is why nowadays it’s all about running your Selenium tests on cloud through multiple browsers in parallel, to execute faster cross browser testing using tools such as LambdaTest.&lt;br&gt;
LambdaTest is a cross browser testing cloud offering a Selenium Grid of 3000+ real browsers &amp;amp; browser versions running on various operating systems. You can integrate with numerous CI/CD tools &amp;amp; extract Selenium test reports without logging into LambdaTest using our RESTful API.&lt;/p&gt;

&lt;p&gt;Check out all of the LambdaTest integrations to multiple CI/CD tools, project management tool, IM-based collaboration tool.&lt;/p&gt;

&lt;p&gt;We can run the same login script used in previous sections on LambdaTest by defining the platform, browser, and version to run this script. This helps in running your script on multiple browsers, whichever your project/product supports.&lt;/p&gt;

&lt;p&gt;In order to run your script for Selenium login with Java, all you have to do is setup the RemoteWebDriver URL through which you will connect to your LambdaTest platform. This URL is a combination of your username, access key, and the LamdaTest hub URL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpf82ebgkw960r8mr6vmo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpf82ebgkw960r8mr6vmo.png" width="800" height="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Post this using desired capabilities class, you can set the platform like Windows 8 or 10 or Mac, etc. Then define the browser on which you wish to run your tests on and its version. Name of the build and other attributes like video, screenshots, logs, etc can be also provided. You can make use of our Selenium Desired Capabilities Generator for fetching the values based on your desired capabilities. That is it! Now use your same login script as defined above to run your test on the cloud-based platform. Below is the referenced code snippet for it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class LoginUsingSelenium {

public RemoteWebDriver driver = null;
    public String url = "[https://www.lambdatest.com/](https://www.lambdatest.com/)";
    public static final String  username= "sadhvisingh24";
    public static final String auth_key = "auth key generated";
    public static final String URL = "[@hub](http://twitter.com/hub).lambdatest.com/wd/hub";
    boolean status = false;

[@Test](http://twitter.com/Test)
    public void login () {
        // TODO Auto-generated method stub
        try {

driver.manage().window().maximize();
            driver.get("[https://www.linkedin.com/login](https://www.linkedin.com/login)");

WebElement username = driver.findElement(By.id("username"));
            WebElement password = driver.findElement(By.id("password"));
            WebElement login = driver.findElement(By.xpath("//button[text()='Sign in']"));

username.sendKeys("linkedin username");
            password.sendKeys("fake password");
            login.click();

String actualUrl = "[https://www.linkedin.com/feed/](https://www.linkedin.com/feed/)";
            String expectedUrl = driver.getCurrentUrl();

if (actualUrl.equalsIgnoreCase(expectedUrl)) {
                System.out.println("Test passed");
                status = true; //Lambda status will be reflected as passed
              } else {
                System.out.println("Test failed"); //Lambda status will be reflected as passed

}
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        finally {
            tearDown();
        }

}



[@BeforeClass](http://twitter.com/BeforeClass)
    public void setUp() {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("browserName", "chrome");
        capabilities.setCapability("version", "72.0");
        capabilities.setCapability("platform", "win8"); // If this cap isn't specified, it will just get the any available one
        capabilities.setCapability("build", "TestNG_login_1");
        capabilities.setCapability("name", "TestNG_login_1");
        capabilities.setCapability("network", true); // To enable network logs
        capabilities.setCapability("visual", true); // To enable step by step screenshot
        capabilities.setCapability("video", true); // To enable video recording
        capabilities.setCapability("console", true); // To capture console logs
        try {

driver = new RemoteWebDriver(new URL("https://" + username + ":" + auth_key + URL), capabilities);

} catch (Exception e) {

System.out.println("Invalid grid URL" + e.getMessage());
        }

}
    private void tearDown () {
        if (driver != null) {
            ((JavascriptExecutor) driver).executeScript("lambda-status=" + status); //Lambda status will be reflected as either passed/ failed

driver.quit();

System.out.println("The setup process is completed");

}
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Start your Test Automation. LambdaTest provides you with the status of your test run and its corresponding video, logs, network details as well as screenshots.&lt;/p&gt;

&lt;p&gt;Below is the screenshot of our Selenium script execution on LambdaTest Selenium Grid.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9mp0pc4bes93g60cn5hh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9mp0pc4bes93g60cn5hh.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Perform Automated and Live Interactive Cross Browser &lt;a href="https://www.lambdatest.com/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep06_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Testing web&lt;/a&gt; on 3000+ Real Browsers and Operating Systems Online&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How Was That?
&lt;/h2&gt;

&lt;p&gt;Kudos! You have successfully executed automation testing for login process using Selenium &amp;amp; Java. How did you find this &lt;strong&gt;Selenium Java&lt;/strong&gt; tutorial for login process? Let me know your thoughts in the comment section below. I look forward to your replies. Happy testing! 🙂&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Complete Guide For Using XPath In Selenium With Examples</title>
      <dc:creator>sadhvisingh1</dc:creator>
      <pubDate>Fri, 28 Jun 2019 10:36:52 +0000</pubDate>
      <link>https://dev.to/testmuai/complete-guide-for-using-xpath-in-selenium-with-examples-2656</link>
      <guid>https://dev.to/testmuai/complete-guide-for-using-xpath-in-selenium-with-examples-2656</guid>
      <description>&lt;p&gt;Identifying element has always been the trickiest part and therefore require an accurate and correct approach. If you are unable to find elements by the easily available approaches like ID, class, name, link or tagname then XPath in Selenium can help rescue you. Locating dynamic elements have always been the pain area while you wish to automate the scripts, the only ray of hope to deal with such fiascos is XPath. In my current article, I will be digging into the steps of locating an element via XPath example in Selenium and its various ways. We will be looking into the below sections:&lt;br&gt;
&lt;strong&gt;1. &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid1?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062043&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;What Is XPath in Selenium?&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062042&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Types Of XPath in Selenium&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
  2.1   &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid1.1?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062042&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Absolute&lt;/a&gt;&lt;br&gt;
  2.2  &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid1.2?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062041&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Relative&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;3. &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid3?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062040&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Writing Dynamic XPath in Selenium by different ways&lt;/a&gt;:&lt;/strong&gt;&lt;br&gt;
  3.1  &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.1?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062039&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Basic XPath&lt;/a&gt;&lt;br&gt;
  3.2  &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.2?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062038&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Using ‘OR’ &amp;amp; ‘AND’&lt;/a&gt;&lt;br&gt;
  3.3  &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.3?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062037&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Using Contains()&lt;/a&gt;&lt;br&gt;
  3.4  &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.4?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062036&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Using Starts-With&lt;/a&gt;&lt;br&gt;
  3.5  &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.5?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062035&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Using Text()&lt;/a&gt;&lt;br&gt;
  3.6  &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.6?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062034&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Using Index&lt;/a&gt;&lt;br&gt;
  3.7  &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.7?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062033&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Using Chained XPath&lt;/a&gt;&lt;br&gt;
  3.8  &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062032&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;XPath Axes&lt;/a&gt;&lt;br&gt;
  3.8.1   &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8.1?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062030&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Following&lt;/a&gt;&lt;br&gt;
  3.8.1.1  &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8.1.1?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062029&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Following Sibling&lt;/a&gt;&lt;br&gt;
  3.8.2   &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8.2?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062028&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Preceding&lt;/a&gt;&lt;br&gt;
  3.8.2.1  &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8.2.1?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062027&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Preceding Sibling&lt;/a&gt;&lt;br&gt;
  3.8.3  &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8.3?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062026&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Child&lt;/a&gt;&lt;br&gt;
  3.8.4  &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8.4?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062025&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Parent&lt;/a&gt;&lt;br&gt;
  3.8.5  &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8.5?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062024&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Descendants&lt;/a&gt;&lt;br&gt;
  3.8.6  &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8.6" rel="noopener noreferrer"&gt;Ancestors&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you wish to look into other approaches of locating an element, you can view the below articles:&lt;br&gt;
&lt;strong&gt;1. &lt;a href="https://www.lambdatest.com/blog/making-the-move-with-id-locator-in-selenium-webdriver/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062023&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;How to locate element via ID&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. &lt;a href="https://www.lambdatest.com/blog/how-to-use-name-locator-in-selenium-automation-scripts/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062022&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;How to locate element via Name&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;3. &lt;a href="https://www.lambdatest.com/blog/locating-elements-by-tagname-in-selenium/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062021&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;How to locate element by Tagname&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;4. &lt;a href="https://www.lambdatest.com/blog/how-pro-testers-use-css-selectors-in-selenium-automation-scripts/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062020&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;How to locate element by CSS Selectors&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;5. &lt;a href="https://www.lambdatest.com/blog/locators-in-selenium-webdriver-with-examples/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062019&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Different locators in Selenium WebDriver&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  So, What Is XPath In Selenium?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;XPath known as the XML path is a language that helps to query the XML documents. It consists of expression for a path along with certain conditions to locate a particular element.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So let’s see how to write an XPath in Selenium. Below is the DOM structure for &lt;a href="https://accounts.lambdatest.com/register/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062019&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;LambdaTest Registration Page&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form method="POST" action="https://accounts.lambdatest.com/register"&amp;gt;
&amp;lt;input type="hidden" name="_token" value="W4D7bCygQ4abq2Xa3ckZ2rwG3Wk7f9enPXIIPeuF"&amp;gt; 
&amp;lt;div class="col-sm-12 google-sign-form"&amp;gt;&amp;lt;p class="signup-titel" style="text-align: center;"&amp;gt;SIGN UP FOR FREE&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;div class="col-sm-12 google-sign-form"&amp;gt;
&amp;lt;input type="text" placeholder="Organization/Company Name" name="organization_name" value="" class="form-control sign-up-input-2 "&amp;gt;
 &amp;lt;input type="text" placeholder="Full Name*" name="name" value="" class="form-control sign-up-input-2 "&amp;gt;
 &amp;lt;input type="email" placeholder="Work Email*" name="email" value="" class="form-control sign-up-input-2 "&amp;gt; 
&amp;lt;input type="password" placeholder="Desired Password*" name="password" class="form-control sign-up-input-2 "&amp;gt; &amp;lt;input type="phone" placeholder="Phone*" name="phone" value="" class="form-control sign-up-input-2 "&amp;gt; &amp;lt;p class="terms-cond"&amp;gt;&amp;lt;label for="terms_of_service" class="woo"&amp;gt;
&amp;lt;input type="checkbox" name="terms_of_service" id="terms_of_service" value="1" class="form-check-input" style="position: relative; margin-left: 0px;"&amp;gt; &amp;amp;nbsp; I agree to the &amp;lt;a target="_blank" href="https://www.lambdatest.com/terms-of-service"&amp;gt;Terms
                                    of Service&amp;lt;/a&amp;gt;
                                and &amp;lt;a target="_blank" href="https://www.lambdatest.com/privacy"&amp;gt;Privacy Policy&amp;lt;/a&amp;gt;&amp;lt;/label&amp;gt;&amp;lt;/p&amp;gt; &amp;lt;button type="submit" class=" btn sign-up-btn-2 btn-block"&amp;gt;Signup for Free&amp;lt;/button&amp;gt;&amp;lt;/div&amp;gt;
 &amp;lt;div class="col-sm-12 link-sect"&amp;gt;&amp;lt;p class="login-in-link test-left"&amp;gt;Already have an account? &amp;lt;a href="/login"&amp;gt;Login&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Referenced screenshot of the page:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdqdxnosmkmsdyhdwv373.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdqdxnosmkmsdyhdwv373.png" alt="Referenced screenshot" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order to visualize the above XML document, I have created the below flowchart.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj0pptq3vesdfnftmlmua.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj0pptq3vesdfnftmlmua.png" alt="flowchart" width="512" height="288"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, I will write the XPath in Selenium from the root node which is the form tag in our case, then will locate the Full name field using div 2 child and the attribute value of the full name field. The XPath created in this case will be as below:Below is a code snippet highlighting the XPath written using relative XPath for the register page of LambdaTest.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;//form/div[@class=’ col-sm-12 google-sign-form’]/input[@name=’ name’]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In this case, I have referenced one attribute of the children and grandchildren in order to differentiate from the multiple children and grandchildren of each parent. From this we can derive the general syntax of XPath as follows:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;//tagname[@attribute name= ‘value’]&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;//&lt;/strong&gt; Denotes the current node&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tagname :&lt;/strong&gt; Define the tagname you are referencing to locate the element.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Attribute Value :&lt;/strong&gt; The attribute of the define tag through which you wish to narrow down the search.&lt;/li&gt;
&lt;li&gt;Value: Represents the value of any chosen attribute. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Nowadays, thorough use of multiple plugins and tools can help one find the XPath in Selenium with relative ease. The reason I don’t encourage people to do so, the XPath provided by these tools sometimes turn out to be brittle. Also, it may lead to people forgetting the basic concepts of creating XPath locators. Using firebug and Chrome Dev Tools one can also copy the required XPath in Selenium.&lt;/p&gt;
&lt;h2&gt;
  
  
  Types Of XPath In Selenium
&lt;/h2&gt;

&lt;p&gt;There are two types of XPath&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Absolute XPath&lt;/li&gt;
&lt;li&gt;Relative XPath&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Absolute XPath Example In Selenium
&lt;/h3&gt;

&lt;p&gt;In case of absolute XPath in Selenium, the XPath expression is created using the selection from the root node. It starts with a single slash ‘/’ and traverses from the root to the whole DOM to reach to the desired element. The biggest disadvantage of using this as locating an element is, if during the course of development any changes made in the path, may lead to a failed XPath expression. For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/html/body/div[1]/section/div/div[2]/div/form/div[2]/input[3]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the above XPath example in Selenium, if any tag name like section or one of the div’s changes the whole XPath would become invalid leading to script failure.&lt;/p&gt;
&lt;h3&gt;
  
  
  Relative XPath Example In Selenium
&lt;/h3&gt;

&lt;p&gt;In case of relative XPath in Selenium, the XPath expression is generated from the middle of the DOM structure. It is represented by a double slash ‘//’ denoting the current node. In this case the search will start from the mentioned tagname and string value. It is more compact, easy to use and less prone to been broken. For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;//input[@name=’email’]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In above XPath example in Selenium, we are searching from the current node with tagname input having attribute as name with value as email.&lt;/p&gt;

&lt;p&gt;Below is a code snippet highlighting the XPath written using relative XPath for the register page of LambdaTest.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

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

public class RelativeXpathExample {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", " path to chromedriver ");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://accounts.lambdatest.com/register");
        //Relative xpath for organization field
        driver.findElement(By.xpath("//input[@name='organization_name']")).sendKeys("Lambdatest Org");
        //Relative xpath for full name field
        driver.findElement(By.xpath("//input[@name='name']")).sendKeys("Sadhvi Singh");
        driver.close();

    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Writing Complex &amp;amp; Dynamic XPath In Selenium Through Various Methods
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Using Basic XPath
&lt;/h3&gt;

&lt;p&gt;This is the common and syntactical approach of writing the XPath in Selenium which is the combination of a tagname and attribute value. Here are few basic XPath examples in Selenium:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Xpath=//input [&lt;a class="mentioned-user" href="https://dev.to/name"&gt;@name&lt;/a&gt;=’password’]&lt;/li&gt;
&lt;li&gt;Xpath=//a [&lt;a class="mentioned-user" href="https://dev.to/href"&gt;@href&lt;/a&gt;= ‘&lt;a href="https://www.lambdatest.com/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062019&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;https://www.lambdatest.com/&lt;/a&gt;’]&lt;/li&gt;
&lt;li&gt;Xpath=//*[&lt;a class="mentioned-user" href="https://dev.to/id"&gt;@id&lt;/a&gt;=’email_01’]&lt;/li&gt;
&lt;li&gt;Xpath=//input[name=’email’][&lt;a class="mentioned-user" href="https://dev.to/placeholder"&gt;@placeholder&lt;/a&gt;=’Work Email’]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first two examples in the basic XPath list seems self-explanatory. In it we have used tags as input and anchor tag with their corresponding attribute value. The last two examples are just extended version of using XPath in Selenium. In the third example we have just excluded the HTML tag and represented it with an asterisk(*). In this case, the script will search in the DOM with any HTML tag having an ID attribute with value as ‘email_01’. In case of example 4, we have created the XPath using multiple attributes for the single HTML tag.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using ‘OR’ &amp;amp; ‘AND’
&lt;/h3&gt;

&lt;p&gt;As mentioned these logical expressions are used on the attributes condition. In case of OR any one of the conditions should be true or both, whereas in the case of AND, both the conditions should be fulfilled. For example, for the below DOM structure, XPath using AND and OR can be written as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt; input type="email" placeholder="Work Email*" name="email" value="" class="form-control sign-up-input-2 " &amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Xpath= //input[@type= ‘email’ or &lt;a class="mentioned-user" href="https://dev.to/name"&gt;@name&lt;/a&gt;= ‘email’]&lt;/li&gt;
&lt;li&gt;Xpath= //input[@type= ‘email’ and &lt;a class="mentioned-user" href="https://dev.to/name"&gt;@name&lt;/a&gt;= ‘email’]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In case of OR if any of the attribute value matches, the element will be located, whereas in case of AND both the attribute value should match and met, then only the element will be located. The above defined ways have been inculcated in the code snippet below, where we are trying to input values on the &lt;a href="https://accounts.lambdatest.com/register" rel="noopener noreferrer"&gt;LambdaTest register page.&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

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

public class XpathExampleWithAndOR {

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        System.setProperty("webdriver.chrome.driver", " path to chromedriver ");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://accounts.lambdatest.com/register");

        //Finding the organization field via xpath using OR
        driver.findElement(By.xpath("//input[@name='organization_name' or @placeholder='Organization/Company Name']")).sendKeys("Lambdatest");

        //Finding the full name field via xpath using AND
        driver.findElement(By.xpath("//input[@name='name' and @placeholder='Full Name*']")).sendKeys("Lambdatest");

        //Finding the work email field via xpath using OR, where only one of the attribute defined is correct whereas the other incorrect and does not match, still this should work as one of them meets the condition.
        driver.findElement(By.xpath("//input[@name='email' or @id='not present']")).sendKeys("Lambdatest");

        //Finding the password field via xpath using AND, where only one of the attribute defined is correct whereas the other incorrect and does not match,this should NOT work as one of them does not meets the condition.
        driver.findElement(By.xpath("//input[@name='password' and @id='not present']")).sendKeys("Lambdatest");

    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is the console error showcasing as the last element not found as conditions were not met.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbb6ks4iebnm8qqm0eqsr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbb6ks4iebnm8qqm0eqsr.png" alt="error showcasing as the last element not found" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please Note: &lt;strong&gt;Both ‘and’ and ‘or’ should be case sensitive. In case you tend to use, ‘OR’ or ‘AND’, you will get an error in console stating invalid xpath expression&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Contains()
&lt;/h3&gt;

&lt;p&gt;This approach in creating XPath in Selenium comes handy when we have part of attribute values that changes dynamically. For example lets say the ID for the login field which is for instance email_01, have the ending number which keeps changing every time the page is loaded. In this case using contains helps us locate element with constant names like in this case ‘email’. Example for creating XPath with contains for the below DOM structure is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="text" placeholder="Organization/Company Name" name="organization_name" value="" class="form-control sign-up-input-2 "&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Xpath=//*[contains (&lt;a class="mentioned-user" href="https://dev.to/placeholder"&gt;@placeholder&lt;/a&gt;, ‘Organization)]&lt;/li&gt;
&lt;li&gt;Xpath= //input[contains (&lt;a class="mentioned-user" href="https://dev.to/name"&gt;@name&lt;/a&gt;, ‘organization)]&lt;/li&gt;
&lt;li&gt;Xpath=//*[contains(&lt;a class="mentioned-user" href="https://dev.to/class"&gt;@class&lt;/a&gt;, ‘sign-up-input)]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here in the above XPath example for Selenium we have used, attributes like placeholder, name etc and consider partial values to identify elements using contains keywords. Referenced below code snippet highlighting the usage of above XPath creation, in this we are clicking on the &lt;strong&gt;‘Start testing’&lt;/strong&gt; button on LambdaTest homepage. Referenced screenshot:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnj07um6i8mswtvluhj5h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnj07um6i8mswtvluhj5h.png" alt="LambdaTest homepage" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

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

public class XpathContainsExample {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "path to chromedriver");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.lambdatest.com");

        //Finding the element 'Start testing' having text as same, here we will locate element using contains through xpath
        driver.findElement(By.xpath("//a[contains(text(), 'TESTING')]")).click();

        driver.close()

    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using Starts-With()
&lt;/h3&gt;

&lt;p&gt;This is similar to the above contains method, the only difference is the comparison value here starts with an initial string value. This is useful when partly values changes for a given attribute. Like in the above XPath example, the email value changes for the latter values. Here, one can use starts-with approach to identify the element. Below XPath examples for Selenium, highlight the usage of starts-with for the below DOM structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="text" placeholder="Organization/Company Name" name="organization_name" value="" class="form-control sign-up-input-2 "&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Xpath=//input[starts-with(&lt;a class="mentioned-user" href="https://dev.to/placeholder"&gt;@placeholder&lt;/a&gt;, ‘Organization)]&lt;/li&gt;
&lt;li&gt;Xpath= =//input[starts-with(&lt;a class="mentioned-user" href="https://dev.to/name"&gt;@name&lt;/a&gt;, ‘organization)]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here, in the example above we have used two attributes with the starts-with Keyword. The script will find for the tag name and matches with the attribute value that starts with ‘Organization’. Referenced code snippet highlighting the usage of Starts-With keyword while locating element via XPath in Selenium. We will use the same example as above, the only difference is, we will locate an element using &lt;strong&gt;starts-with.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

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

public class XpathStartsWithExample {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "path to chromeDriver");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.lambdatest.com");

        //Finding the element 'Start testing' having text as same, here we will locate element using contains through xpath
        driver.findElement(By.xpath("//a[starts-with(text(), 'START')]")).click();

        driver.close();

    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using Text()
&lt;/h3&gt;

&lt;p&gt;This keyword is used to create expressions for XPath in Selenium when we have a text defined in an HTML tag and we wish to identify element via text. This comes really handy when the other attribute values change dynamically with no substantial part of the attribute value that can be used via Starts-with or Contains. Below is an XPath example in Selenium, highlighting the usage of text for the following DOM structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button type="submit" class=" btn sign-up-btn-2 btn-block"&amp;gt;Signup for Free&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Xpath= //button[text()=’ Signup for Free’]&lt;/li&gt;
&lt;li&gt;Xpath=//button[contains(text(),’ Signup’ )]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the above mentioned examples, we have use the text on the button to identify the element. Here two instances of the examples are used, one where the text is exact matched, whereas the other where the text is matched partially using contains keyword. Referenced snippet below highlighting the usage of the text keyword. In this example, we will be clicking on one of the blogs on the&lt;a href="https://www.lambdatest.com/blog/continuous-testing-using-shift-left-testing-approach/?utm_source=Dzone&amp;amp;utm_medium=DzBlog&amp;amp;utm_campaign=SS-040319-1&amp;amp;utm_term=saif" rel="noopener noreferrer"&gt;LambdaTest blog&lt;/a&gt; page. Referenced screenshot below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi0eqxwgrsakk25l2k8vu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi0eqxwgrsakk25l2k8vu.png" alt="LambdaTest blog" width="512" height="288"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

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

public class XpathWithTextExample {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub


        System.setProperty("webdriver.chrome.driver", "path to the chromedriver");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get("https://www.lambdatest.com/blog");


        //Finding the blog with text as 'complete guide on TestNG annotations' element
        driver.findElement(By.xpath("//a[text()='Complete Guide On TestNG Annotations For Selenium WebDriver']")).click();

        driver.close();



    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using Index
&lt;/h3&gt;

&lt;p&gt;This approach comes in use when you wish to specify a given tag name in terms of index value you wish to locate too. For instance, consider a DOM having multiple input tags for each different field value and you wish to input text into the 4th field. In this case you can use the index to switch to the given tag name. For example:&lt;/p&gt;

&lt;p&gt;For the given DOM structure, I wish to locate the second input tag field:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="col-sm-12 google-sign-form"&amp;gt;&amp;lt;input type="text" placeholder="Organization/Company Name" name="organization_name" value="" class="form-control sign-up-input-2 "&amp;gt; &amp;lt;input type="text" placeholder="Full Name*" name="name" value="" class="form-control sign-up-input-2 "&amp;gt; &amp;lt;input type="email" placeholder="Work Email*" name="email" value="" class="form-control sign-up-input-2 "&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Xpath= //div[&lt;a class="mentioned-user" href="https://dev.to/class"&gt;@class&lt;/a&gt;= ’ col-sm-12 google-sign-form’]/input[2]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the above example, the locator with ‘Full name’ as a field will be selected. The first input tag will be ignored whereas the other tag will be considered owing to the index mentioned. This type of approach comes in handy when dealing with data in tables. For example, when you have multiple rows, you can reference the desired row using an index. Reference code snippet using an index for a tabular form of data for the registration page of LambdaTest, where through the form class we are navigating to the ‘full name’ field using index.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fue7ofixnzorskrovau20.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fue7ofixnzorskrovau20.png" alt="lambdatest login page" width="512" height="288"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

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

public class XpathWithIndex {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\ss251550\\eclipse-workspace_automate\\Automate_Active_MQ_Process\\ChromeDriver\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://accounts.lambdatest.com/register");
        driver.manage().window().maximize();

        //finding the element through index using the reference of the form field containing the fields in it.
        driver.findElement(By.xpath("//div[@class='col-sm-12 google-sign-form']/input[2]")).sendKeys("sadhvi singh");

        driver.close();


    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using Chained XPath In Selenium
&lt;/h3&gt;

&lt;p&gt;As the name signifies, we can use multiple XPath expressions and chained them. For example, in the below DOM structure referencing the LambdaTest homepage links:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ul class="nav navbar-nav navbar-right"&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href="https://www.lambdatest.com/feature"&amp;gt;Live&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href="https://www.lambdatest.com/selenium-automation"&amp;gt;Automation&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt; &amp;lt;li&amp;gt;&amp;lt;a href="https://www.lambdatest.com/blog"&amp;gt;Blog&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
 &amp;lt;li&amp;gt;&amp;lt;a href="https://www.lambdatest.com/pricing"&amp;gt;Pricing&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;li&amp;gt;&amp;lt;a href="https://www.lambdatest.com/support/"&amp;gt;Support&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;li class="sign-in"&amp;gt;&amp;lt;a href="https://accounts.lambdatest.com/login"&amp;gt;Login&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
 &amp;lt;li class="login"&amp;gt;&amp;lt;a href="https://accounts.lambdatest.com/register"&amp;gt;Free Sign Up&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6lpq7dh09ee9ci56a0w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6lpq7dh09ee9ci56a0w.png" alt="Complete Guide For Using XPath In Selenium With Examples" width="512" height="288"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we will try to navigate to the ‘Login link’. The syntax to achieve it is.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Xpath=//ul[&lt;a class="mentioned-user" href="https://dev.to/class"&gt;@class&lt;/a&gt;=’nav navbar-nav navbar-right’]//li[&lt;a class="mentioned-user" href="https://dev.to/class"&gt;@class&lt;/a&gt;=’sign-in’]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the above XPath example in Selenium, I have located the parent class for the link tags and then navigated to the child using chaining for the ‘login’ link. The above example has been achieved using the below code snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

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

public class XpathUsingChaining {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "path of chromeDriver");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.lambdatest.com/");
        driver.manage().window().maximize();

        //locating the 'login' link using xpath chaining and clicking on it.
        driver.findElement(By.xpath("//ul[@class='nav navbar-nav navbar-right']//li[@class='sign-in']")).click();

        //Verifying the current URL on which we post clicking on it.
        String currentURL= driver.getCurrentUrl();
        System.out.println(currentURL);

        driver.close();

    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Console Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmfsjz0qkb6sq676fx7pe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmfsjz0qkb6sq676fx7pe.png" alt="XPath example in Selenium" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;RUN YOUR SELENIUM SCRIPTS ON CLOUD GRID&lt;/p&gt;

&lt;p&gt;2000+ Browsers AND OS&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062019&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;FREE SIGNUP&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  XPath axes
&lt;/h3&gt;

&lt;p&gt;XPath axes comes in handy when the exact element tagname or its attribute value are dynamic and cannot be used to locate an element. In this case locating element after traversing through child/sibling or parent will be easy approach. Some of the widely used XPath axes are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Following:&lt;/strong&gt; This XPath axes helps to locate element following the current node. Mentioned below is the DOM structure of the LambdaTest Registration Page.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="col-sm-12 google-sign-form"&amp;gt;
                              &amp;lt;input type="text" placeholder="Organization/Company Name" name="organization_name" value="" class="form-control sign-up-input-2 "&amp;gt; 
                             &amp;lt;input type="text" placeholder="Full Name*" name="name" value="" class="form-control sign-up-input-2 "&amp;gt;
                            &amp;lt;input type="email" placeholder="Work Email*" name="email" value="" class="form-control sign-up-input-2 "&amp;gt;
                           &amp;lt;input type="password" placeholder="Desired Password*" name="password" class="form-control sign-up-input-2 "&amp;gt;
                           &amp;lt;input type="phone" placeholder="Phone*" name="phone" value="" class="form-control sign-up-input-2 "&amp;gt; 
                            &amp;lt;p class="terms-cond"&amp;gt;
                          &amp;lt;label for="terms_of_service" class="woo"&amp;gt;
                         &amp;lt;input type="checkbox" name="terms_of_service" id="terms_of_service" value="1" class="form-check-input" style="position: relative; margin-left: 0px;"&amp;gt; &amp;amp;nbsp; I agree to the 
                         &amp;lt;a target="_blank" href="https://www.lambdatest.com/terms-of-service"&amp;gt;Terms
                                    of Service&amp;lt;/a&amp;gt;
                                and 
                      &amp;lt;a target="_blank" href="https://www.lambdatest.com/privacy"&amp;gt;Privacy Policy&amp;lt;/a&amp;gt;
&amp;lt;/label&amp;gt;
&amp;lt;/p&amp;gt; 
&amp;lt;button type="submit" class=" btn sign-up-btn-2 btn-block"&amp;gt;Signup for Free&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Referenced screenshot highlighting the two input elements, wherein we will try to locate the element &lt;strong&gt;‘Full name’&lt;/strong&gt; field through the element &lt;strong&gt;‘Organization’&lt;/strong&gt; field&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F02clmvryqz1m0me84ec1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F02clmvryqz1m0me84ec1.png" alt="Complete Guide For Using XPath In Selenium With Examples" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Xpath=//input[&lt;a class="mentioned-user" href="https://dev.to/name"&gt;@name&lt;/a&gt;=’ organization_name’]//following::input[1]&lt;/li&gt;
&lt;li&gt;Xpath= //input[&lt;a class="mentioned-user" href="https://dev.to/name"&gt;@name&lt;/a&gt;=’ organization_name’]//following::input&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the above-mentioned examples, the first one will select the input following the organization element, whereas in the second example, it will select all elements following the organization element having input tag. These are useful when we intent to locate element in a table or when we do not have any whereabouts of the following element from the current node. Mentioned below code snippet displays the elements located via following. In this example, we will use the Lambatest homepage, where we will traverse through the menu header options using the single header class. Referenced screenshot below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fta4b1o5yj6ymt97reelm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fta4b1o5yj6ymt97reelm.png" alt="Complete Guide For Using XPath In Selenium With Examples" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

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

public class XpathUsingFollowing {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", " path of chromedriver ");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.lambdatest.com/");
        driver.manage().window().maximize();

        //Locate element with the link blog using following axes
        driver.findElement(By.xpath("//ul[@class='nav navbar-nav navbar-right']//following::li[3]")).click();

        driver.close();

    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.&lt;strong&gt;Following sibling:&lt;/strong&gt; This is one concept that people tend to get confused with. All you get to clear yourself with this concept, is siblings. In case of following all nodes under the current node are the targeted ones irrespective they are under(children) the context node or not but follows below the context node. In case of following siblings, all following nodes of the context node, that shares the same parent, are applicable. In this case all siblings are referred to as children of the parent node. So if you are referencing one of the children and wish to navigate to other children of the same parent that follows it, following sibling does the business.&lt;/p&gt;

&lt;p&gt;For example, using the LambdaTest homepage links, in the DOM structure below, one of the children has been referenced, and from it, we will navigate to its siblings.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ul class="nav navbar-nav navbar-right"&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href="https://www.lambdatest.com/feature"&amp;gt;Live&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href="https://www.lambdatest.com/selenium-automation"&amp;gt;Automation&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt; &amp;lt;li&amp;gt;&amp;lt;a href="https://www.lambdatest.com/blog"&amp;gt;Blog&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
 &amp;lt;li&amp;gt;&amp;lt;a href="https://www.lambdatest.com/pricing"&amp;gt;Pricing&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;li&amp;gt;&amp;lt;a href="https://www.lambdatest.com/support/"&amp;gt;Support&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;li class="sign-in"&amp;gt;&amp;lt;a href="https://accounts.lambdatest.com/login"&amp;gt;Login&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
 &amp;lt;li class="login"&amp;gt;&amp;lt;a href="https://accounts.lambdatest.com/register"&amp;gt;Free Sign Up&amp;lt;/a&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the parent is,its children are the different li, here we will locate the sign in link and from their locate the login using following sibling&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Xpath= //li[&lt;a class="mentioned-user" href="https://dev.to/class"&gt;@class&lt;/a&gt;=’sign-in’]//following-sibling::li&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Below is a code snippet incorporating the XPath example in Selenium.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package ChromeDriver;

import java.util.concurrent.TimeUnit;

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

public class XpathWithFollowingSibling {

    public static void main(String[] args) {
        // TODO Auto-generated method stub'


        System.setProperty("webdriver.chrome.driver", "path of chromeDriver");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.lambdatest.com/");
        driver.manage().window().maximize();

        //locating the 'sign-up' link using xpath following sibling and clicking on it.
        driver.findElement(By.xpath("//li[@class='sign-in']//following-sibling::li")).click();

        //Verifying the current URL on which we post clicking on it.
        String currentURL= driver.getCurrentUrl();
        System.out.println(currentURL);

        driver.close();


    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Console Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3gn63xz136yiuq1321v8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3gn63xz136yiuq1321v8.png" alt="Complete Guide For Using XPath In Selenium With Examples" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3.&lt;strong&gt;Preceding:&lt;/strong&gt; This method helps in locating element before the current node, as in the preceding element from the current node with XPath in Selenium. Below is a demonstration of the DOM structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="text" placeholder="Organization/Company Name" name="organization_name" value="" class="form-control sign-up-input-2 "&amp;gt;
&amp;lt;input type="text" placeholder="Full Name*" name="name" value="" class="form-control sign-up-input-2 "&amp;gt;
&amp;lt;input type="email" placeholder="Work Email*" name="email" value="" class="form-control sign-up-input-2 "&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Referenced screenshot below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fof8frj6zbzcnm65y9vq3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fof8frj6zbzcnm65y9vq3.png" alt="Complete Guide For Using XPath In Selenium With Examples" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Xpath=//input[&lt;a class="mentioned-user" href="https://dev.to/name"&gt;@name&lt;/a&gt;=’password’]//preceding::input[1]&lt;/li&gt;
&lt;li&gt;Xpath=//input[&lt;a class="mentioned-user" href="https://dev.to/name"&gt;@name&lt;/a&gt;=’password’]//preceding::input&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the above-mentioned example, the first one will locate element with the field as email whereas the other one will locate all elements before the current node i.e. password field. This is also useful in locating elements that cannot be located by any means and can be traversed through. For example, in the case of &lt;a href="https://www.lambdatest.com/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062019&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;cross browser testing&lt;/a&gt;, sometimes few elements cannot be recognized on IE browsers, legacy browser versions. In this case traversing to these elements using precedence or following could be helpful. Referenced example below with code snippet, highlighting how using the blog link on the LambdaTest homepage, we will circulate through the&lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062019&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Automation tab&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F60be0sxfxhzrs0mq0zy2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F60be0sxfxhzrs0mq0zy2.png" alt="Complete Guide For Using XPath In Selenium With Examples" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

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

public class XpathUsingPreceeding {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "path of chromedriver");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.lambdatest.com/");
        driver.manage().window().maximize();

        //Fnding the automation link using the blog link
        driver.findElement(By.xpath("//a[text()='Blog']//preceding::li[1]")).click();

        driver.close();

    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.&lt;strong&gt;Preceding-Sibling:&lt;/strong&gt; This is a concept similar to following sibling, the only difference in functionality is that of preceding. So, in this case, you can switch among the siblings, but in this, you will switch from the context node been a child and move to the preceding node, you wish to locate. Both the child will share the same parent. Using the same example as mentioned in the following sibling, we will now move from the sign-up link to the login link using preceding-sibling. The syntax for the same is&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Xpath= //li[&lt;a class="mentioned-user" href="https://dev.to/class"&gt;@class&lt;/a&gt;=’login’]//preceding-sibling::li[1]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In case you do not specify the index of the sibling you wish to navigate it may choose to select any. For specific selection, you need to mention the index. In case of a single sibling from the context node, you need not specify the index. Below is the code snippet for the same XPath example in Selenium.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package ChromeDriver;

import java.util.concurrent.TimeUnit;

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

public class XpathWithPrecedingSibling {

    public static void main(String[] args) {
        // TODO Auto-generated method stub'


        System.setProperty("webdriver.chrome.driver", "path of chromedriver");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.lambdatest.com/");
        driver.manage().window().maximize();

        //locating the 'login' link using xpath preceding sibling and clicking on it.
        driver.findElement(By.xpath("//li[@class='login']//preceding-sibling::li[1]")).click();

        //Verifying the current URL on which we post clicking on it.
        String currentURL= driver.getCurrentUrl();
        System.out.println(currentURL);

        driver.close();

    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Console Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7gcrfc60sio5p4s63047.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7gcrfc60sio5p4s63047.png" alt="output" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;5.&lt;strong&gt;Child:&lt;/strong&gt; As the name specified this approach is used when we intent to locate all child elements of the current node. A basic use case of this approach could be when we wish to circulate through all the data in a table through the rows, in this case we can find all the child of a particular table. For example, using the below referenced DOM structure, we can create an XPath in Selenium as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="col-sm-12 google-sign-form"&amp;gt;&amp;lt;input type="text" placeholder="Organization/Company Name" name="organization_name" value="" class="form-control sign-up-input-2 "&amp;gt; &amp;lt;input type="text" placeholder="Full Name*" name="name" value="" class="form-control sign-up-input-2 "&amp;gt; &amp;lt;input type="email" placeholder="Work Email*" name="email" value="" class="form-control sign-up-input-2 "&amp;gt; &amp;lt;input type="password" placeholder="Desired Password*" name="password" class="form-control sign-up-input-2 "&amp;gt; &amp;lt;input type="phone" placeholder="Phone*" name="phone" value="" class="form-control sign-up-input-2 "&amp;gt; &amp;lt;p class="terms-cond"&amp;gt;&amp;lt;label for="terms_of_service" class="woo"&amp;gt;&amp;lt;input type="checkbox" name="terms_of_service" id="terms_of_service" value="1" class="form-check-input" style="position: relative; margin-left: 0px;"&amp;gt; &amp;amp;nbsp; I agree to the &amp;lt;a target="_blank" href="https://www.lambdatest.com/terms-of-service"&amp;gt;Terms
                                    of Service&amp;lt;/a&amp;gt;
                                and &amp;lt;a target="_blank" href="https://www.lambdatest.com/privacy"&amp;gt;Privacy Policy&amp;lt;/a&amp;gt;&amp;lt;/label&amp;gt;&amp;lt;/p&amp;gt; &amp;lt;button type="submit" class=" btn sign-up-btn-2 btn-block"&amp;gt;Signup for Free&amp;lt;/button&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Referenced screenshot of the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F32h0nr6r0v1168r4ane3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F32h0nr6r0v1168r4ane3.png" alt="Complete Guide For Using XPath In Selenium With Examples" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Xpath= //div[&lt;a class="mentioned-user" href="https://dev.to/class"&gt;@class&lt;/a&gt;=’ col-sm-12 google-sign-form’]/child::input&lt;/li&gt;
&lt;li&gt;Xpath= //div[&lt;a class="mentioned-user" href="https://dev.to/class"&gt;@class&lt;/a&gt;=’ col-sm-12 google-sign-form’]/child::input[1]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the above-mentioned example, the first will locate all the input fields of the div for which are organization field, name field, email field, password field and phone number field, where as the other is used to locate element with the organization field only.&lt;/p&gt;

&lt;p&gt;Below is the code snippet highlighting the use of child axes on the same. We are using the same register example of &lt;a href="https://accounts.lambdatest.com/register/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062019&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;LambdaTest register page&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

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

public class XpathUsingChild {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "path of chrome driver");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://accounts.lambdatest.com/register");
        driver.manage().window().maximize();

        //Finding the work email filed using the child locator xpath axes
        driver.findElement(By.xpath("//div[@class='col-sm-12 google-sign-form']/child::input[3]")).sendKeys("sadhvi singh");;

        driver.close();

    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;6.&lt;strong&gt;Parent:&lt;/strong&gt; This method is used to select the parent node of the current node. For example, for the below referenced DOM structure, the parent node is located through XPath.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="col-sm-12 google-sign-form"&amp;gt;&amp;lt;input type="text" placeholder="Organization/Company Name" name="organization_name" value="" class="form-control sign-up-input-2 "&amp;gt; &amp;lt;input type="text" placeholder="Full Name*" name="name" value="" class="form-control sign-up-input-2 "&amp;gt; &amp;lt;input type="email" placeholder="Work Email*" name="email" value="" class="form-control sign-up-input-2 "&amp;gt; &amp;lt;input type="password" placeholder="Desired Password*" name="password" class="form-control sign-up-input-2 "&amp;gt; &amp;lt;input type="phone" placeholder="Phone*" name="phone" value="" class="form-control sign-up-input-2 "&amp;gt; &amp;lt;p class="terms-cond"&amp;gt;&amp;lt;label for="terms_of_service" class="woo"&amp;gt;&amp;lt;input type="checkbox" name="terms_of_service" id="terms_of_service" value="1" class="form-check-input" style="position: relative; margin-left: 0px;"&amp;gt; &amp;amp;nbsp; I agree to the &amp;lt;a target="_blank" href="https://www.lambdatest.com/terms-of-service"&amp;gt;Terms
                                    of Service&amp;lt;/a&amp;gt;
                                and &amp;lt;a target="_blank" href="https://www.lambdatest.com/privacy"&amp;gt;Privacy Policy&amp;lt;/a&amp;gt;&amp;lt;/label&amp;gt;&amp;lt;/p&amp;gt; &amp;lt; button type="submit" class=" btn sign-up-btn-2 btn-block"&amp;gt;Signup for Free&amp;lt; /button &amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Referenced screenshot:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8887kaejiog41223tcly.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8887kaejiog41223tcly.png" alt="Complete Guide For Using XPath In Selenium With Examples" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Xpath= //input[&lt;a class="mentioned-user" href="https://dev.to/name"&gt;@name&lt;/a&gt;=’email’]//parent::div&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the above example, the parent element is located through its child element. From the email field we are traversing to its parent node. Please note in case of child XPath, only the immediate children of the current node are considered and not the grandchildren. In case if the current node is the root node, then its parent will be empty and hence it will have no parent aces. Below is the code snippet for the same:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

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

public class XpathUsingParent {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "path of chromedriver");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
               driver.get("https://accounts.lambdatest.com/register");
               driver.manage().window().maximize();

        //Finding the parent form usnig the password field as the current node.
        WebElement parentDiv=driver.findElement(By.xpath("//input[@name='password']//parent::div"));
        System.out.print(parentDiv.getAttribute("class"));

        driver.close();

    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Console Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe0ron79k2z702p36pnbe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe0ron79k2z702p36pnbe.png" alt="Complete Guide For Using XPath In Selenium With Examples" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;7.&lt;strong&gt;Descendants:&lt;/strong&gt; This approach is used to locate element via XPath for all the children and sub children of the current node. For the below DOM structure, the descendants for XPath in Selenium are located as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="col-lg-3 col-md-4 col-sm-6 sign-form"&amp;gt;&amp;lt;a href="https://www.lambdatest.com"&amp;gt;&amp;lt;img height="38" src="/images/logo.svg" class="img-responsive mb-4 mx-auto d-block"&amp;gt;&amp;lt;/a&amp;gt; &amp;lt;h1 class="title text-center font-weight-light"&amp;gt;Hello! Welcome back&amp;lt;/h1&amp;gt; &amp;lt;input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg"&amp;gt; &amp;lt;input type="password" name="password" placeholder="Password" class="form-control mt-3 form-control-lg"&amp;gt; &amp;lt;button type="submit" class="btn btn-primary btn-lg btn-block mt-3"&amp;gt;LOGIN&amp;lt;/button&amp;gt; &amp;lt;div class="form-group form-check mt-2"&amp;gt;&amp;lt;input type="checkbox" name="remember" id="remember" class="form-check-input"&amp;gt; &amp;lt;label for="remember" class="form-check-label"&amp;gt;Remember Me&amp;lt;/label&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;div class="row mt-2"&amp;gt;&amp;lt;div class="col pr-2 text-pass"&amp;gt;&amp;lt;a href="https://accounts.lambdatest.com/password/reset"&amp;gt;Forgot Password?&amp;lt;/a&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;div class="col pl-2 text-pass text-right"&amp;gt;No account? &amp;lt;a href="/register"&amp;gt;Sign up&amp;lt;/a&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Referenced Screenshot:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgyiff198ekob1ustrz9k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgyiff198ekob1ustrz9k.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Xpath= //div[&lt;a class="mentioned-user" href="https://dev.to/class"&gt;@class&lt;/a&gt;=’ col-lg-3 col-md-4 col-sm-6 sign-form’]//descendant:: input&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the example above all the elements like email field, password field and button will be selected. One can also select one of the mentioned fields using index. Below is a code snippet highlighting the use of it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.List;
import java.util.concurrent.TimeUnit;

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

public class XpathUsingDescendants {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "path of chromedriver");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://accounts.lambdatest.com/login");
        driver.manage().window().maximize();

        //Finding the remember me grandchildren of the login form with field value as remember me.
        WebElement rememberMe=driver.findElement(By.xpath("//div[@class='col-lg-3 col-md-4 col-sm-6 sign-form']//descendant::label"));
        rememberMe.click();
        System.out.print(rememberMe.getText());

        driver.close();


    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Console Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy4xn3dt32uy860eapnol.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy4xn3dt32uy860eapnol.png" alt="Complete Guide For Using XPath In Selenium With Examples" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;8.&lt;strong&gt;Ancestors:&lt;/strong&gt; In this method the context node, parent or its grandparents are selected via the Ancestors axes. For example, for the below DOM structure the ancestor will be defined as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;form method="POST" action="https://accounts.lambdatest.com/login"&amp;gt;&amp;lt;input type="hidden" name="_token" value="W4D7bCygQ4abq2Xa3ckZ2rwG3Wk7f9enPXIIPeuF"&amp;gt; &amp;lt;div class="row justify-content-sm-center"&amp;gt;&amp;lt;div class="col-lg-3 col-md-4 col-sm-6 sign-form"&amp;gt;&amp;lt;a href="https://www.lambdatest.com"&amp;gt;&amp;lt;img height="38" src="/images/logo.svg" class="img-responsive mb-4 mx-auto d-block"&amp;gt;&amp;lt;/a&amp;gt; &amp;lt;h1 class="title text-center font-weight-light"&amp;gt;Hello! Welcome back&amp;lt;/h1&amp;gt; &amp;lt;input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg"&amp;gt; &amp;lt;input type="password" name="password" placeholder="Password" class="form-control mt-3 form-control-lg"&amp;gt; &amp;lt;button type="submit" class="btn btn-primary btn-lg btn-block mt-3"&amp;gt;LOGIN&amp;lt;/button&amp;gt; &amp;lt;div class="form-group form-check mt-2"&amp;gt;&amp;lt;input type="checkbox" name="remember" id="remember" class="form-check-input"&amp;gt; &amp;lt;label for="remember" class="form-check-label"&amp;gt;Remember Me&amp;lt;/label&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;div class="row mt-2"&amp;gt;&amp;lt;div class="col pr-2 text-pass"&amp;gt;&amp;lt;a href="https://accounts.lambdatest.com/password/reset"&amp;gt;Forgot Password?&amp;lt;/a&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;div class="col pl-2 text-pass text-right"&amp;gt;No account? &amp;lt;a href="/register"&amp;gt;Sign up&amp;lt;/a&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Referenced screenshot:&lt;br&gt;&lt;br&gt;
 &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F77xis8xezz6832fs12ao.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F77xis8xezz6832fs12ao.png" alt="output" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Xpath= //input[&lt;a class="mentioned-user" href="https://dev.to/name"&gt;@name&lt;/a&gt;=’email]//ancestor::div[1]&lt;/li&gt;
&lt;li&gt;Xpath= //input[&lt;a class="mentioned-user" href="https://dev.to/name"&gt;@name&lt;/a&gt;=’email]//ancestor::form&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the example above, the first one will point to the parent node of the login fields where the other will point to its grandparent node which is the tag form in this case. Below code snippet highlighting the use of the same.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.concurrent.TimeUnit;

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

public class XpathUsingAncestor {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver", "path of chromedriver");
        WebDriver driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://accounts.lambdatest.com/login");
        driver.manage().window().maximize();

        //Finding the parent div using the password field of the login page.
        WebElement parentDiv=driver.findElement(By.xpath("//input[@name='password']//ancestor::div[1]"));
        System.out.println(parentDiv.getAttribute("class"));

        WebElement parentForm=driver.findElement(By.xpath("//input[@name='password']//ancestor::form"));
        System.out.println(parentForm.getAttribute("action"));


        driver.close();


    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Console Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fovdxgbj0thbs3ajcqrlg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fovdxgbj0thbs3ajcqrlg.png" alt="Console Output" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;XPath in Selenium helps an individual find out solutions in locating elements when the standard processes do not work. When to use which is important when it comes to different complexities of the DOM structure and the functional needs. As defined earlier in my article, cross browser testing could be one of the areas where you can explore the creation of XPath in Selenium, due to different browser behaviors towards DOM element. Make sure whichever method you opt in case of XPath are less prone to failure and make the scripts more robust, neater and easier to maintain. Cheers!&lt;/p&gt;

&lt;p&gt;Origanally Published: &lt;strong&gt;&lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062044&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;LambdaTest&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://accounts.lambdatest.com/register/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062019&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0e5vtqzcoqm2zr3cb7la.jpg" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Related Articles&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.lambdatest.com/blog/using-link-text-and-partial-link-text-in-selenium/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062045&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Find Elements With Link Text &amp;amp; Partial Link Text In Selenium&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.lambdatest.com/blog/selenium-java-tutorial-class-name-locator-in-selenium/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=Sadhvi-28062046&amp;amp;utm_term=Sadhvi" rel="noopener noreferrer"&gt;Selenium Java Tutorial – Class Name Locator In Selenium&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>java</category>
      <category>testing</category>
    </item>
    <item>
      <title>Complete Guide On TestNG Annotations For Selenium WebDriver</title>
      <dc:creator>sadhvisingh1</dc:creator>
      <pubDate>Wed, 01 May 2019 10:15:02 +0000</pubDate>
      <link>https://dev.to/testmuai/complete-guide-on-testng-annotations-for-selenium-webdriver-126c</link>
      <guid>https://dev.to/testmuai/complete-guide-on-testng-annotations-for-selenium-webdriver-126c</guid>
      <description>&lt;p&gt;&lt;strong&gt;TestNG is a testing framework created by Cédric Beust&lt;/strong&gt; and helps to cater a lot of our testing needs. It is widely used in Selenium. Wondering on what NG stands for? Well, it refers to &lt;strong&gt;‘Next Generation’&lt;/strong&gt;. TestNG is similar to Junit but is more powerful to it when it comes to controlling the execution flow of your program. As the nature of framework, we tend to make our tests more structured and provides better validation points through the use of TestNG Annotations.&lt;/p&gt;

&lt;p&gt;Some of the noteworthy features of TestNG are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Powerful and wide variety of TestNG annotations to support your test cases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Helps to perform parallel testing, dependent method testing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Flexibility of running your tests through multiple sets of data through TestNG.xml file or via data-provider concept.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Test cases can be grouped and prioritized as per need basis.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Provides access to HTML reports and can be customized through various plugins.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Test logs can be generated across tests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Can be easily integrated with eclipse, Maven, Jenkins etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A basic process flow of a TestNG programs involves the following steps:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foulttm4y5m13z8r1dvlx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foulttm4y5m13z8r1dvlx.png" width="800" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, before jumping onto the annotations in &lt;a href="https://www.lambdatest.com/selenium-automation-testing-with-testng?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep07_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;TestNG for Selenium automation&lt;/a&gt;, it would be better to refer the prerequisites are required to setup TestNG.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Java Development Kit&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Setup Eclipse or any other IDE.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install TestNG in Eclipse or any other IDE.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: Annotations can be used only with Java version 1.5 or higher.&lt;/p&gt;

&lt;p&gt;If you are new to TestNG framework then follow our guide to run your &lt;a href="https://www.lambdatest.com/blog/a-complete-guide-for-your-first-testng-automation-script/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep07_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;first automation script with TestNG.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you’re new to Selenium and wondering what it is then we recommend checking out our guide — &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep07_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;What is Selenium&lt;/a&gt;?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Check out this, How To Debug Websites Using &lt;a href="https://www.lambdatest.com/blog/debug-websites-using-safari-developer-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep07_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Developer Tools for Safari&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  So, What Is An Annotation?
&lt;/h2&gt;

&lt;p&gt;An annotation is a tag that provides additional information about the class or method. It is represented by **‘@’ **prefix. TestNG use these annotations to help in making a robust framework. Let us have a look at these annotations of TestNG for &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep07_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The most important TestNG annotations framework where the main business logic resides.&lt;/strong&gt; All functionalities to be automated are kept inside the &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; annotation method. It has various attributes based on which the method can be reformed and executed.&lt;/p&gt;

&lt;p&gt;Example of a code snippet below validating the url :&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@Test](http://twitter.com/Test)
    public void testCurrentUrl() throws InterruptedException
    {
        driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='app']/header/aside/ul/li[4]/a")).click();

        String currentUrl= driver.getCurrentUrl();
        assertEquals(current_url, "[https://automation.lambdatest.com/timeline/?viewType=build&amp;amp;page=1](https://automation.lambdatest.com/timeline/?viewType=build&amp;amp;page=1)", "url did not matched");
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  @BeforeTest
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;This annotation is run before your first &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; TestNG Annotations method in your class.&lt;/strong&gt; You can use this annotation in TestNG for &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep09_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; to setup your browser profile preferences, for example auto opening your browser in maximize mode, setting up your own customized profile for your browser etc.&lt;/p&gt;

&lt;p&gt;Below is the code snippet for BeforeTest method ensuring the browser opens in maximize mode:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@BeforeTest](http://twitter.com/BeforeTest)
    public void profileSetup()
    {
        driver.manage().window().maximize();


    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  @AfterTest
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;This TestNG Annotations runs after all your test methods belonging to your class have run.&lt;/strong&gt; This is a useful annotation which comes handy in terms of reporting your automation results to your stakeholders. You can use this annotation to generate report of your tests and share it to your stakeholders via email.&lt;/p&gt;

&lt;p&gt;Example of the code snippet below:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@AfterTest](http://twitter.com/AfterTest)
    public void reportReady()
    {
        System.out.println("Report is ready to be shared, with screenshots of tests");
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  @BeforeMethod
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;This annotation in TestNG runs before every &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; annotated method.&lt;/strong&gt; You can use it to check out for the database connections before executing your tests or lets say different functionality been tested in your &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; annotated method which requires user login in a certain class. In this case also you can put your login code in the @BeforeMethod annotation method.&lt;/p&gt;

&lt;p&gt;Below code snippet is an example, displaying login functionality of the LambdaTest platform:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@BeforeMethod](http://twitter.com/BeforeMethod)
    public void checkLogin()
    {
          driver.get("[https://accounts.lambdatest.com/login](https://accounts.lambdatest.com/login)");
          driver.findElement(By.xpath("//input[[@name](http://twitter.com/name)='email']")).sendKeys("[sadhvisingh24@gmail.com](mailto:sadhvisingh24@gmail.com)");
          driver.findElement(By.xpath("//input[[@name](http://twitter.com/name)='password']")).sendKeys("XXXXX");
          driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='app']/section/form/div/div/button")).click();

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  @AfterMethod
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;This annotation runs after every &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; annotated method.&lt;/strong&gt; This annotation can be used to take screenshots of every test method ran against test runs .&lt;/p&gt;

&lt;p&gt;Below code snippet indicating screenshot taken in the @AfterTest TestNG Annotations for Selenium:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@AfterMethod](http://twitter.com/AfterMethod)
    public void screenShot() throws IOException
    {
        TakesScreenshot scr= ((TakesScreenshot)driver);
        File file1= scr.getScreenshotAs(OutputType.FILE);

       FileUtils.copyFile(file1, new File("C:\\Users\\navyug\\workspace\\QAPractise\\test-output\\test1.PNG"));

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/90F7dePW1vo"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  @BeforeClass
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;This annotation runs before the first test method in the current class.&lt;/strong&gt;This annotation can be used to setup your browser properties, initialize your driver, opening your browser with the desired URL etc.&lt;/p&gt;

&lt;p&gt;Below is the code snippet for BeforeClass:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@BeforeClass](http://twitter.com/BeforeClass)
    public void appSetup()
    {
        driver.get(url);

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  @AfterClass
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;This TestNG Annotations runs after the last test method in the current class.&lt;/strong&gt; This annotation in TestNG can be used to perform clean up activities during your tests like closing your driver etc&lt;/p&gt;

&lt;p&gt;Below is the example of code snippet showing closing activities performed:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@AfterClass](http://twitter.com/AfterClass)
    public void closeUp()
    {
        driver.close();
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  @BeforeSuite
&lt;/h2&gt;

&lt;p&gt;A suite can consist of multiple classes, this TestNG Annotations runs before all the tests methods of all the classes. &lt;strong&gt;This annotation marks the entry point of execution.&lt;/strong&gt; @BeforeSuite annotation in TestNG can be used to perform the needed and generic functions like setting up and starting Selenium drivers or remote web drivers etc.&lt;/p&gt;

&lt;p&gt;Example of @BeforeSuite annotation in TestNG, code snippet showcasing setting up of driver:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@BeforeSuite](http://twitter.com/BeforeSuite)
    public void setUp()
    {
    System.setProperty("webdriver.chrome.driver", "path to chrome driver");
    driver=new ChromeDriver();
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Explore what WebDriver is, its features, how it works, best practices, and more in this WebDriver Tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  @AfterSuite
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;This TestNG Annotations runs post all the test methods of all the classes have run.&lt;/strong&gt; This annotation can be used to clean up the processes before completing off your tests when you have multiple classes in functioning, for example closing the drivers etc.&lt;/p&gt;

&lt;p&gt;Below is the code snippet for @AfterSuite annotation in TestNG for Selenium:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@AfterSuite](http://twitter.com/AfterSuite)
    public void cleanUp()
    {

        System.out.println("All close up activities completed");
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  @BeforeGroups
&lt;/h2&gt;

&lt;p&gt;TestNG helps testers create a bunch of tests into groups through the attribute group used in the &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; TestNG Annotations. For example, if you wish all similar functionalities related to user management to be clubbed together, you can mark all tests like Dashboard, profile, transactions etc. into a single group as ‘user_management’. This @BeforeGroups annotation in TestNG helps to run the defined test first before the specified group. This annotation can be used if the group focuses on a single functionality like stated in the above example. The BeforeGroup annotation can contain the login feature which is required to run before any other methods like user dashboard, user profile etc.&lt;/p&gt;

&lt;p&gt;Example of the Code snippet for @BeforeGroups annotation in TestNG for Selenium:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@BeforeGroups](http://twitter.com/BeforeGroups)("urlValidation")
    public void setUpSecurity() {
        System.out.println("url validation test starting");
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  @AfterGroups
&lt;/h2&gt;

&lt;p&gt;This TestNG Annotations runs after all the test methods of the specified group are executed.&lt;br&gt;
Example of Code snippet for @AfterGroups annotation in TestNG for Selenium:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@AfterGroups](http://twitter.com/AfterGroups)("urlValidation")
    public void tearDownSecurity() {
        System.out.println("url validation test finished");
    }

The below code displays an example of all annotations used along with TestNG report respectively:

import static org.testng.Assert.assertEquals;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class AnnotationsTestNG {

public WebDriver driver;
        public String url="[https://www.lambdatest.com/](https://www.lambdatest.com/)";

    [@BeforeSuite](http://twitter.com/BeforeSuite)
    public void setUp()
    {   
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\navyug\\workspace\\QAPractise\\src\\ChromeDriver\\chromedriver.exe");
        driver=new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        System.out.println("The setup process is completed");
    }

    [@BeforeTest](http://twitter.com/BeforeTest)
    public void profileSetup()
    {
        driver.manage().window().maximize();
        System.out.println("The profile setup process is completed");

    }

    [@BeforeClass](http://twitter.com/BeforeClass)
    public void appSetup()
    {
        driver.get(url);
        System.out.println("The app setup process is completed");
    }

    [@BeforeMethod](http://twitter.com/BeforeMethod)
    public void checkLogin()
    {
          driver.get("[https://accounts.lambdatest.com/login](https://accounts.lambdatest.com/login)");
          driver.findElement(By.xpath("//input[[@name](http://twitter.com/name)='email']")).sendKeys("[sadhvisingh24@gmail.com](mailto:sadhvisingh24@gmail.com)");
          driver.findElement(By.xpath("//input[[@name](http://twitter.com/name)='password']")).sendKeys("activa9049");
          driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='app']/section/form/div/div/button")).click();
          System.out.println("The login process on lamdatest is completed");
    }

    [@Test](http://twitter.com/Test)(groups="urlValidation")
    public void testCurrentUrl() throws InterruptedException
    {
        driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='app']/header/aside/ul/li[4]/a")).click();
        Thread.sleep(6000);
        String currentUrl= driver.getCurrentUrl();
        assertEquals(currentUrl, "[https://automation.lambdatest.com/timeline/?viewType=build&amp;amp;page=1](https://automation.lambdatest.com/timeline/?viewType=build&amp;amp;page=1)", "url did not matched");
        System.out.println("The url validation test is completed");
    }

    [@AfterMethod](http://twitter.com/AfterMethod)
    public void screenShot() throws IOException
    {
        TakesScreenshot scr= ((TakesScreenshot)driver);
        File file1= scr.getScreenshotAs(OutputType.FILE);

        FileUtils.copyFile(file1, new File("C:\\Users\\navyug\\workspace\\QAPractise\\test-output\\test1.PNG"));
        System.out.println("Screenshot of the test is taken");
    }

    [@AfterClass](http://twitter.com/AfterClass)
    public void closeUp()
    {
        driver.close();
        System.out.println("The close_up process is completed");
    }

    [@AfterTest](http://twitter.com/AfterTest)
    public void reportReady()
    {
        System.out.println("Report is ready to be shared, with screenshots of tests");
    }

    [@AfterSuite](http://twitter.com/AfterSuite)
    public void cleanUp()
    {

        System.out.println("All close up activities completed");
    }

    [@BeforeGroups](http://twitter.com/BeforeGroups)("urlValidation")
    public void setUpSecurity() {
        System.out.println("url validation test starting");
    }

    [@AfterGroups](http://twitter.com/AfterGroups)("urlValidation")
    public void tearDownSecurity() {
        System.out.println("url validation test finished");
    }



}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fah0cq017g882gylnwns5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fah0cq017g882gylnwns5.png" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With TestNG certification, you can challenge your skills in performing automated testing with TestNG and take your career to the next level.&lt;/p&gt;

&lt;p&gt;Here’s a short glimpse of the TestNG certification from LambdaTest:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/dzXX2hJhuCY"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Check out this, How To Debug Websites Using &lt;a href="https://www.lambdatest.com/blog/debug-websites-using-safari-developer-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep07_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Dev Tools in Safari&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  TestNG Report:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgpn6w1j7ldekzdcqs62v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgpn6w1j7ldekzdcqs62v.png" width="800" height="429"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Console Output:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhjwhfxk1f5jew75ambqa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhjwhfxk1f5jew75ambqa.png" width="800" height="431"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Watch this video to learn how to set up and use TestNG with Selenium to automate your testing process. We will also introduce the TestNG Priority method, which allows you to write easy-to-read and maintainable tests.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/X3HdVgjafpA"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Execution Sequence Of Annotations In TestNG For Selenium
&lt;/h2&gt;

&lt;p&gt;All TestNG Annotations described above are executed on runtime in the following order:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;BeforeSuite&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;BeforeTest&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;BeforeClass&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;BeforeGroups&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;BeforeMethod&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Test&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;AfterMethod&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;AfterGroups&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;AfterClass&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;AfterTest&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;AfterSuite&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an image of the basic workflow of these annotations:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc9pyxdtnsihjeuelqyq4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc9pyxdtnsihjeuelqyq4.png" width="800" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Attributes Used With TestNG Annotations
&lt;/h2&gt;

&lt;p&gt;These test annotations in TestNG have multiple attributes that can be used for our test method. The attributes further help in defining our tests and help in providing clarity in terms of execution flow of the different test’s method used in the TestNG class. Listing them below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Description:&lt;/strong&gt; It defines the test method. One can define what a method does via the description. For example, &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;(description=”this test validates the login functionality”).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;alwaysRun:&lt;/strong&gt; this attribute when used with a test method ensures it always run irrespective of the fact even if the parameters on which the method depends fails. When the value is set to true this method will always execute. For example, &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;(alwaysRun= true).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;dataProvider:&lt;/strong&gt; This attribute is set to provide data from the dataProvider annotated test to the test provided with this attribute. For example, let’s say you intent to run your tests on multiple cross browsers, where a dataProvider annotated test is written which contains multiple inputs of browsers and their corresponding versions. In this case the test containing this attribute will use those inputs of data to run you tests on multiple browsers. Syntax for the same is, &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;(dataProvider=”cross browser testing”).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;dependsOnMethods:&lt;/strong&gt; This attribute provides details to the execution flow, wherein the test is executed only if its dependent method mentioned in the attribute is executed. In case the test on which the method depends on is failed or not executed, the test is skipped from the execution. For example, &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;(dependsOnmethod=”Login”).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;groups:&lt;/strong&gt; This attribute helps to groups your test methods focusing onto a single functionality into one group. For example, &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;(groups=”Payment_Module”). This attribute also helps in the longer run when one can choose to ignore few groups during the execution cycle and chose over the other groups. All one need to do is mention the included groups in the TestNG.xml file within the include tag whereas the excluded groups can be defined using the exclude tag in the xml file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;dependsOnGroups:&lt;/strong&gt; this attribute performs the above two attributes functions in collation i.e. it defines the test method with the attribute ‘dependsOn’ the defined groups. Once that group of tests are run, only post that this annotated method would execute. For example, &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;(dependsOnMethods = "Payment_Module" ).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;priority:&lt;/strong&gt; This attribute helps us to defined priority of the test’s methods. When TestNG executes the &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; annotated method, it may do so in random order. In a scenario where you wish that your &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt; annotated method runs in a desired sequence you can use the priority attribute. The default priority of all test methods is 0. Priorities in ascending order are scheduled first for execution for example, &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;(priority=1), &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;(priority=2), in this case test with priority equal to one will be executed first then the test with priority as 2.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;enabled:&lt;/strong&gt; This attribute comes into picture, when you have an intent to ignore a particular test method and don’t want to execute it. All you need to do is set this attribute to false. For example, &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;(enabled= false).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;timeout:&lt;/strong&gt; This attribute helps to define the time a particular test should take to execute, in case it exceeds the time defined by the attribute, the test method would terminate and will fail with an exception marked as org.testng.internal.thread.ThreadTimeoutException. For example, &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;(timeOut= 500). &lt;strong&gt;Please note the time specified is in milliseconds.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;InvocationCount:&lt;/strong&gt; This attribute works exactly like the loop. Based on the attribute set across the test method, it would execute that method those number of times. For example, &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;(invocationCount = 5), this would execute the test 5 times.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;InvocationTimeOut:&lt;/strong&gt; this attribute is used in unison with the above invocationCount attribute. Based on the set value of this attribute along with the invocationCount, this ensures the test runs the number of times specified as per the invocationCount in the defined time set by the invocationTimeOut attribute. For example, &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;(invocationCount =5,invocationTimeOut = 20 ).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**expectedExceptions: **this attribute helps to handle the exception the test method is expected to throw. In case the one defined in the attribute is set and thrown by the test method it is passed else any other exception not stated in the attribute and thrown by the test method, would make the test method fail. For example, &lt;a class="mentioned-user" href="https://dev.to/test"&gt;@test&lt;/a&gt;(expectedExceptions = {ArithmeticException.class }).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The above defined are the attributes used with the annotations in TestNG for Selenium. Below is the code snippet showcasing the use of the above attributes:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import static org.testng.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class AnnotationsTest {

public WebDriver driver;
    public String url="[https://www.lambdatest.com/](https://www.lambdatest.com/)";

    [@BeforeSuite](http://twitter.com/BeforeSuite)
    public void setUp()
    {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\navyug\\workspace\\QAPractise\\src\\ChromeDriver\\chromedriver.exe");
        driver=new ChromeDriver();
        System.out.println("The setup process is completed");
    }

    [@BeforeTest](http://twitter.com/BeforeTest)
    public void profileSetup()
    {
        driver.manage().window().maximize();
        System.out.println("The profile setup process is completed");

    }

    [@BeforeClass](http://twitter.com/BeforeClass)
    public void appSetup()
    {
        driver.get(url);
        System.out.println("The app setup process is completed");
    }


    [@Test](http://twitter.com/Test)(priority=2)
    public void checkLogin()
    {
          driver.get("[https://accounts.lambdatest.com/login](https://accounts.lambdatest.com/login)");
          driver.findElement(By.xpath("//input[[@name](http://twitter.com/name)='email']")).sendKeys("[sadhvisingh24@gmail.com](mailto:sadhvisingh24@gmail.com)");
          driver.findElement(By.xpath("//input[[@name](http://twitter.com/name)='password']")).sendKeys("xxxxx");
          driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='app']/section/form/div/div/button")).click();
          System.out.println("The login process on lamdatest is completed");
    }

    [@Test](http://twitter.com/Test)(priority=0 ,description= "this test validates the sign-up test")
    public void signUp() throws InterruptedException
    {
        WebElement link= driver.findElement(By.xpath("//a[text()='Free Sign Up']"));
        link.click();
        WebElement organization=driver.findElement(By.xpath("//input[[@name](http://twitter.com/name)='organization_name']"));
        organization.sendKeys("LambdaTest");
        WebElement firstName=driver.findElement(By.xpath("//input[[@name](http://twitter.com/name)='name']"));
        firstName.sendKeys("Test");
        WebElement email=driver.findElement(By.xpath("//input[[@name](http://twitter.com/name)='email']"));
        email.sendKeys("[User622@gmail.com](mailto:User622@gmail.com)");
        WebElement password=driver.findElement(By.xpath("//input[[@name](http://twitter.com/name)='password']"));
        password.sendKeys("TestUser123");
        WebElement phoneNumber=driver.findElement(By.xpath("//input[[@name](http://twitter.com/name)='phone']"));
        phoneNumber.sendKeys("9412262090");
        WebElement termsOfService=driver.findElement(By.xpath("//input[[@name](http://twitter.com/name)='terms_of_service']"));
        termsOfService.click();
        WebElement button=driver.findElement(By.xpath("//button[text()='Signup']"));
        button.click();




    }

    [@Test](http://twitter.com/Test)(priority=3, alwaysRun= true, dependsOnMethods="check_login", description="this test validates the URL post logging in" , groups="url_validation")
    public void testCurrentUrl() throws InterruptedException
    {
        driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='app']/header/aside/ul/li[4]/a")).click();
        String currentUrl= driver.getCurrentUrl();
        assertEquals(current_url, "[https://automation.lambdatest.com/timeline/?viewType=build&amp;amp;page=1](https://automation.lambdatest.com/timeline/?viewType=build&amp;amp;page=1)", "url did not matched");
        System.out.println("The url validation test is completed");
    }

    [@Test](http://twitter.com/Test)(priority=1, description = "this test validates the logout functionality" ,timeOut= 25000)
    public void logout() throws InterruptedException
    {
        Thread.sleep(6500);
         driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='userName']")).click();
         driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='navbarSupportedContent']/ul[2]/li/div/a[5]")).click();   
    }

    [@Test](http://twitter.com/Test)(enabled=false)
    public void skipMethod()
    {
        System.out.println("this method will be skipped from the test run using the attribute enabled=false");
    }

    [@Test](http://twitter.com/Test)(priority=6,invocationCount =5,invocationTimeOut = 20)
    public void invocationcountShowCaseMethod()
    {
        System.out.println("this method will be executed by 5 times");
    }



    [@AfterMethod](http://twitter.com/AfterMethod)()
    public void screenshot() throws IOException
    {
        TakesScreenshot scr= ((TakesScreenshot)driver);
        File file1= scr.getScreenshotAs(OutputType.FILE);

       FileUtils.copyFile(file1, new File("C:\\Users\\navyug\\workspace\\QAPractise\\test-output\\test1.PNG"));
       System.out.println("Screenshot of the test is taken");
    }

    [@AfterClass](http://twitter.com/AfterClass)
    public void closeUp()
    {
        driver.close();
        System.out.println("The close_up process is completed");
    }

    [@AfterTest](http://twitter.com/AfterTest)
    public void reportReady()
    {
        System.out.println("Report is ready to be shared, with screenshots of tests");
    }

    [@AfterSuite](http://twitter.com/AfterSuite)
    public void cleanUp()
    {

        System.out.println("All close up activities completed");
    }

    [@BeforeGroups](http://twitter.com/BeforeGroups)("urlValidation")
    public void setUpSecurity() {
        System.out.println("url validation test starting");
    }

    [@AfterGroups](http://twitter.com/AfterGroups)("urlValidation")
    public void tearDownSecurity() {
        System.out.println("url validation test finished");
    }



}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Console Output:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2rpj3cvuw2voenhdikox.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2rpj3cvuw2voenhdikox.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  TestNG Report:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frx174f5xlou6tibipbrh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frx174f5xlou6tibipbrh.png" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Watch this video to learn about the TestNG Annotations and how they help provide better structure and readability to the code.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/zI7Xbaj4RIs"&gt;
&lt;/iframe&gt;
&lt;/p&gt;
&lt;h2&gt;
  
  
  Annotations In TestNG For Desired Purpose
&lt;/h2&gt;

&lt;p&gt;There are more annotations than the ones defined above, which are used for desired purpose only.&lt;/p&gt;
&lt;h2&gt;
  
  
  @DataProvider
&lt;/h2&gt;

&lt;p&gt;This annotated method is used for supplying data to the test method in which the dataProvider attribute is defined. This annotated method helps in creating a data driven framework where multiple sets of input values can be given which returns a 2D array or object. &lt;strong&gt;@DataProvider annotation in TestNG comes with two attributes.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;name-&lt;/strong&gt; this attribute is used to provide name to the dataprovider. If not set it defaults to the name of the method provided.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;parallel-&lt;/strong&gt;this is one attribute that helps in running your tests in parallel with different variation of data. This attribute is one of the reasons to make TestNG more powerful to Junit. Its default value is false.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Below is the code snippet indicating the use of @DataProvider annotation with name and parallel attribute set to it.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@DataProvider](http://twitter.com/DataProvider)(name="SetEnvironment", parallel=true)
public Object[][] getData(){

Object[][] browserProperty = new Object[][]{

{Platform.WIN8, "chrome", "70.0"},
{Platform.WIN8, "chrome", "71.0"}
};
return browserProperty;

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  @Factory
&lt;/h2&gt;

&lt;p&gt;This annotation helps to run multiple test classes through a single test class. It basically defines and create tests dynamically.&lt;/p&gt;

&lt;p&gt;The below code snippet indicates the use of @Factory annotation that helps calls the test method class.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package ChromeDriver;

import org.testng.annotations.Test;

public class FactorySimplyTest1 {

[@Test](http://twitter.com/Test)
    public void testMethod1() {
        System.out.println("This is to test for method 1 for Factor Annotation");

}}

package ChromeDriver;

import org.testng.annotations.Test;

public class FactorySimpleTest2 {

[@Test](http://twitter.com/Test)
    public void testMethod2() {
        System.out.println("This is to test for method 2 for Factor Annotation");


}
}

package ChromeDriver;

import org.testng.annotations.Factory;
import org.testng.annotations.Test;

public class FactoryAnnotation {

[@Factory](http://twitter.com/Factory)()
    [@Test](http://twitter.com/Test)
    public Object[] getTestFactoryMethod() {
        Object[] factoryTest = new Object[2];
        factoryTest[0] = new FactorySimplyTest1();
        factoryTest[1] = new FactorySimpleTest2();
        return factoryTest;
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Console Output:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fye3gnn1a0sc08106v30e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fye3gnn1a0sc08106v30e.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  @Parameters
&lt;/h2&gt;

&lt;p&gt;This annotation helps you pass parameters to your tests directly via the testNG.xml file. Usually this is preferred when you have limited data sets to try on your tests. In case of complicated and large data sets @dataProvider annotation is preferred or excel.&lt;/p&gt;

&lt;p&gt;The below code snippet showcase the same:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[@Parameters](http://twitter.com/Parameters)({ "username", "password"})
    [@Test](http://twitter.com/Test)()
    public void checkLogin(String username, String password)
    {
          driver.get("[https://accounts.lambdatest.com/login](https://accounts.lambdatest.com/login)");
          driver.findElement(By.xpath("//input[[@name](http://twitter.com/name)='email']")).sendKeys(username);
          driver.findElement(By.xpath("//input[[@name](http://twitter.com/name)='password']")).sendKeys(password);
          driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='app']/section/form/div/div/button")).click();
          System.out.println("The login process on lamdatest is completed");
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The parameter values are defined in the TestNG.xml file as below:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br&gt;
&amp;lt;!DOCTYPE suite SYSTEM "&lt;a href="http://testng.org/testng-1.0.dtd" rel="noopener noreferrer"&gt;http://testng.org/testng-1.0.dtd&lt;/a&gt;"&amp;gt;&lt;br&gt;
&amp;lt;suite name="Suite"&amp;gt;&lt;br&gt;
  &amp;lt;test thread-count="5" name="Annotations"&amp;gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    &amp;amp;lt;parameter name="username"  value="[sadhvisingh24@gmail.com](mailto:sadhvisingh24@gmail.com)" /&amp;amp;gt;
    &amp;amp;lt;parameter name="password"  value="XXXXX" /&amp;amp;gt;

    &amp;amp;lt;classes&amp;amp;gt;
        &amp;amp;lt;class name="Parameter_annotation"/&amp;amp;gt;
    &amp;amp;lt;/classes&amp;amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;&amp;lt;/test&amp;gt; &amp;lt;!-- Annotations --&amp;gt;&lt;br&gt;
&amp;lt;/suite&amp;gt; &amp;lt;!-- Suite --&amp;gt;&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  @Listener&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;This annotation helps in logging and reporting. We have multiple listeners like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;IExecutionListener&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;IAnnotationTransformer&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ISuiteListener&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ITestListener&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But to go in depth with these listeners and their uses would be a talk for another blog. I will be writing one soon, so stay tuned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Check out this, How To Test &lt;a href="https://www.lambdatest.com/blog/test-internet-explorer-for-mac/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=sep07_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Internet Explorer on Mac&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  That Was All!
&lt;/h2&gt;

&lt;p&gt;The key point to note while working with all these annotations and attributes is your system should have java 1.5 version or higher as these annotations are not supported for all lower versions of java and you may tend to receive error for them.&lt;/p&gt;

&lt;p&gt;All the above mentioned TestNG Annotations and attributes of TestNG helps to provide better structuring and readability to the code. It helps provide detailed reports that makes status reporting part even easier and useful. Use of these annotations in TestNG for Selenium completely depend on your business requirements. Hence, choosing the right ones for the right use is important. Try out these annotations in TestNG on LambdaTest Selenium Grid now!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Locating Elements by TagName In Selenium</title>
      <dc:creator>sadhvisingh1</dc:creator>
      <pubDate>Mon, 29 Apr 2019 09:11:21 +0000</pubDate>
      <link>https://dev.to/testmuai/locating-elements-by-tagname-in-selenium-35ll</link>
      <guid>https://dev.to/testmuai/locating-elements-by-tagname-in-selenium-35ll</guid>
      <description>&lt;p&gt;Selenium locators are your key when dealing with locating elements on a web page. From the list of locators like ID, Name, Class, tag name, XPath, CSS selector etc, one can choose any of these as per needs and locate the web element on a web page. Since ID’s, name, XPath or CSS selectors are more frequently used as compared to tag name or linktext, people majorly have less idea or no working experience of the latter locators. In this article, I will be detailing out the usage and real-time examples of How to Get Element by Tag Name locators In Selenium.&lt;/p&gt;

&lt;p&gt;So, what is a tag name locator in Selenium?&lt;/p&gt;

&lt;p&gt;A tag name is a part of a DOM structure where every element on a page is been defined via tag like input tag, button tag or anchor tag etc. Each tag has multiple attributes like ID, name, value class etc. As far as other locators in Selenium are concerned, we used these attributes values of the tag to locate elements. In the case of tag name Selenium locator, we will simply use the tag name to identify an element.&lt;/p&gt;

&lt;p&gt;Below is the DOM structure of LambdaTest login page where I have highlighted the tag names:&lt;/p&gt;

&lt;p&gt;Email Field: &amp;lt; input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg"&amp;gt;&lt;/p&gt;

&lt;p&gt;Password Field: &amp;lt; input type="password" name="password" placeholder="Password" class="form-control mt-3 form-control-lg" &amp;gt;&lt;/p&gt;

&lt;p&gt;Login Button: &amp;lt; button type="submit" class="btn btn-primary btn-lg btn-block mt-3"&amp;gt;LOGIN&amp;lt; /button &amp;gt;&lt;/p&gt;

&lt;p&gt;Forgot Password Link: &amp;lt; button type="submit" class="btn btn-primary btn-lg btn-block mt-3"&amp;gt;LOGIN&amp;lt; /button &amp;gt;&lt;/p&gt;

&lt;p&gt;Now the question that arises in one’s mind is, when do I use this tag name locator in Selenium? Well, in a scenario where you do not have attribute values like ID, class or name and you tend to locate an element, you may have to rely on using the tag name locator in Selenium. For example, in case you wish to retrieve data from a table, you may use &amp;lt; td &amp;gt; tag or &amp;lt; tr &amp;gt; tag to retrieve data.&lt;/p&gt;

&lt;p&gt;Similarly, in a scenario where you wish to verify the number of links and validating whether they are working or not, you can choose to locate all such links through the &lt;strong&gt;anchor tag&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Please note:&lt;/strong&gt; In a simple basic scenario where an element is located just via tag, it may lead to a lot of values being identified and may cause issues. In this case, Selenium will select or locate the first tag that matches the one provided from your end. So, refrain yourself from using tag name locator in Selenium if you intend to locate a single element.&lt;/p&gt;

&lt;p&gt;The command to identify an element via tag name in Selenium is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.findElement(By.tagName("input"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foglkudydx1mijyzjkava.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foglkudydx1mijyzjkava.png" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This certification is for anyone who wants to stay ahead among professionals who are growing their career in Selenium automation testing.&lt;/p&gt;

&lt;p&gt;Here’s a short glimpse of the Selenium 101 certification from LambdaTest:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/qx9FPFfJm7E"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Test your mobile websites and smartphone apps using our scalable on cloud mobile &lt;a href="https://www.lambdatest.com/mobile-emulator-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug31_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;emulator online&lt;/a&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Real Time Scenarios Highlighting How to Get Element by Tag Name In Selenium
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Scenario 1
&lt;/h2&gt;

&lt;p&gt;A basic example, where we are locating the image avatar on ‘my profile’ section of LambdaTest:&lt;/p&gt;

&lt;p&gt;Reference is the DOM structure of the avatar:&lt;/p&gt;

&lt;p&gt;&amp;lt; img src="&lt;a href="https://www.gravatar.com/avatar/daf7dc69b0d19124ed3f9bab946051f6.jpg?s=200&amp;amp;d=mm&amp;amp;r=g" rel="noopener noreferrer"&gt;https://www.gravatar.com/avatar/daf7dc69b0d19124ed3f9bab946051f6.jpg?s=200&amp;amp;d=mm&amp;amp;r=g&lt;/a&gt;" alt="sadhvi" class="img-thumbnail" &amp;gt;&lt;/p&gt;

&lt;p&gt;Let’s look into the code snippet now:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package Chromedriver;

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

    public class Locator_By_Tagname {

        public static void main(String[] args) throws InterruptedException {
            // TODO Auto-generated method stub

            //Setting up chrome using chromedriver by setting its property
                    System.setProperty("webdriver.chrome.driver", " Path of chromeDriver "); 

                    //Opening browser
                    WebDriver driver= new ChromeDriver() ;

                    //Opening window tab in maximize mode
                    driver.manage().window().maximize();

                    //Opening application
                    driver.get("[https://accounts.lambdatest.com/login](https://accounts.lambdatest.com/login)");

                    //Locating the email field element via Name tag and storing it in the webelement
                    WebElement email_field=driver.findElement(By.name("email"));

                    //Entering text into the email field
                    email_field.sendKeys("[sadhvisingh24@gmail.com](mailto:sadhvisingh24@gmail.com)");

                    //Locating the password field element via Name tag and storing it in the webelement
                    WebElement password_field=driver.findElement(By.name("password"));

                    //Entering text into the password field
                    password_field.sendKeys("New1life");

                    //Clicking on the login button to login to the application
                    WebElement login_button=driver.findElement(By.xpath("//button[text()='LOGIN']"));

                    //Clicking on the 'login' button
                    login_button.click();

                   //Clicking on the Settings option
                    driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='app']/header/aside/ul/li[8]/a")).click();

                    //Waiting for the profile option to appear
                    Thread.sleep(3500);

                    //*[[@id](http://twitter.com/id)="app"]/header/aside/ul/li[8]/ul/li[1]/a
                    //Clicking on the profile link
                    driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='app']/header/aside/ul/li[8]/ul/li[1]/a")).click();

                    //Locating the element via img tag for the profile picture and storing it in the webelement
                    WebElement image= driver.findElement(By.tagName("img"));

                    //Printing text of Image alt attribute which is sadhvi
                    System.out.println(image.getAttribute("alt"));

        }

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Scenario 2
&lt;/h2&gt;

&lt;p&gt;In this example, we will be verifying the number of links on the LambdaTest homepage and printing those link-texts:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package Chromedriver;


    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 Tagname_linktest {

        public static void main(String[] args) {
          // TODO Auto-generated method stub


            //Setting up chrome using chromedriver by setting its property
                    System.setProperty("webdriver.chrome.driver", " Path of chromeDriver "); 

                    //Opening browser
                    WebDriver driver= new ChromeDriver() ;

                    //Opening window tab in maximize mode
                    driver.manage().window().maximize();

                    //Opening application
                    driver.get("[https://www.lambdatest.com](https://www.lambdatest.com)");

                    //storing the number of links in list
                    List&amp;lt;WebElement&amp;gt; links= driver.findElements(By.tagName("a"));

                    //storing the size of the links
                    int i= links.size();

                    //Printing the size of the string
                    System.out.println(i);

                    for(int j=0; j&amp;lt;i; j++)
                    {
                        //Printing the links
                        System.out.println(links.get(j).getText());
                    }

                    //Closing the browser
                    driver.close();



        }

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Below is a screenshot of the console:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3cc59faqmp807e2kcktt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3cc59faqmp807e2kcktt.png" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;a href="https://www.lambdatest.com/blog/iphone-simulators-on-windows/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug31_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;IOS emulators for PC&lt;/a&gt;- Looking to perform Android and iOS app testing on Real Device Cloud, check out this&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario 3
&lt;/h2&gt;

&lt;p&gt;In this example, I will showcase when one wants to identify the number of rows in a table as during runtime this information can be dynamic and hence, we need to evaluate beforehand on the number of rows and then retrieve or validate the information.&lt;/p&gt;

&lt;p&gt;Below is the DOM structure of the table:&lt;/p&gt;

&lt;p&gt;&amp;lt; tbody class="yui3-datatable-data" &amp;gt;&amp;lt; tr id="yui_patched_v3_18_1_1_1554473988939_130" data-yui3-record="model_1" class="yui3-datatable-even" &amp;gt;&lt;/p&gt;

&lt;p&gt;&amp;lt; tr id="yui_patched_v3_18_1_1_1554473988939_130" data-yui3-record="model_1" class="yui3-datatable-even"&amp;gt;&amp;lt; td class="yui3-datatable-col-name yui3-datatable-cell "&amp;gt;John A. Smith&amp;lt; /td &amp;gt;&lt;/p&gt;

&lt;p&gt;1236 Some Street San Francisco CA&amp;lt; /td &amp;gt;&amp;lt; /tr &amp;gt;&lt;/p&gt;

&lt;p&gt;//……further rows continue//&lt;/p&gt;

&lt;p&gt;Now let’s look into its code snippet:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package Chromedriver;

    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 Tagname_Row_data {

        public static void main(String[] args) {
            // TODO Auto-generated method stub


            //Setting up chrome using chromedriver by setting its property
            System.setProperty("webdriver.chrome.driver", "Path of chromeDriver"); 

            //Opening browser
            WebDriver driver= new ChromeDriver() ;

            //Opening window tab in maximize mode
            driver.manage().window().maximize();

            //Opening application
            driver.get("[https://alloyui.com/examples/datatable](https://alloyui.com/examples/datatable)");

            //locating the number of rows of the table
            List&amp;lt;WebElement&amp;gt; rows= driver.findElements(By.tagName("tr"));

            //Printing the size of the rows
            System.out.print(rows.size());

            //Closing the driver
            driver.close();




        }

    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Snapshot of Console output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcacp8v2jgrhxnivqeyoh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcacp8v2jgrhxnivqeyoh.png" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;&lt;a href="https://www.lambdatest.com/mobile-emulator-for-app-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug31_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Android Emulator For IOS&lt;/a&gt;- Test your mobile applications before going live. All you need is a computer with internet connection&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As you can see, how I have used the tag name locator in Selenium on different scenarios. You can also use the tag name locator in combination with attribute value using XPath or CSS selectors. When it comes to other scenarios of locating elements, I may not suggest you using the tag name locator in Selenium but of-course scenarios mentioned above can really come in handy. Usage of tag name locator in Selenium may be limited, however, if you wish to be a proficient automation tester, then understanding how to use tag name locator and when to use it becomes very critical.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>webdev</category>
      <category>devops</category>
      <category>testing</category>
    </item>
    <item>
      <title>Making The Move With ID Locator In Selenium WebDriver</title>
      <dc:creator>sadhvisingh1</dc:creator>
      <pubDate>Fri, 26 Apr 2019 11:36:49 +0000</pubDate>
      <link>https://dev.to/testmuai/making-the-move-with-id-locator-in-selenium-webdriver-2g25</link>
      <guid>https://dev.to/testmuai/making-the-move-with-id-locator-in-selenium-webdriver-2g25</guid>
      <description>&lt;p&gt;If you are beginning with Selenium, you may be unsure about what to do and how to do it? In my opinion, I find locators to be the best step to start brushing or learning Selenium. Locators are the foundation of building your Selenium Script. With the use of locators, one can locate the element on the web page. Not just locating an element is important but making sure it’s fast and accurate is equally important. One such locator is the ID locator in Selenium WebDriver that does this business.&lt;/p&gt;

&lt;p&gt;Check out what WebDriver is, its features, how it works, best practices, and more in this WebDriver tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  When To Opt For ID Locator In Selenium WebDriver?
&lt;/h2&gt;

&lt;p&gt;Using ID Locator in Selenium WebDriver is the fastest and the most reliable among all the locators. ID’s are supposed to be unique to each element, making the ID locator as a dependable choice. Since browsers do not make it mandatory for ID to be unique thereby making developers take leverage of it and may lead to ID’s either not present as part of an attribute or autogenerated, or not unique to a page. Due to the mentioned issues, it may require switching to other locators.&lt;/p&gt;

&lt;p&gt;You can check out other articles around different CSS locator in Selenium that helps in locating elements through various ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/using-link-text-and-partial-link-text-in-selenium/" rel="noopener noreferrer"&gt;Link Text &amp;amp; Partial Link Text In Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/how-to-use-name-locator-in-selenium-automation-scripts/" rel="noopener noreferrer"&gt;Name locator in Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/selenium-java-tutorial-class-name-locator-in-selenium/" rel="noopener noreferrer"&gt;Class Name locator in Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/how-to-use-name-locator-in-selenium-automation-scripts/" rel="noopener noreferrer"&gt;Tagname locator in Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/how-pro-testers-use-css-selectors-in-selenium-automation-scripts/" rel="noopener noreferrer"&gt;CSS Selector in Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/" rel="noopener noreferrer"&gt;XPath in Selenium&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are an advanced or medium Selenium practitioner, then you can chuck on dedicated articles mentioned above. And go for our complete guide to help you illustrate the practical demonstration of CSS locator in Selenium.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using ID Locator in Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;Here I will be sighting an example of LambdaTest login page example for &lt;strong&gt;‘Remember me’&lt;/strong&gt; functionality. Below is the screenshot of the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2d8d0pqxwiz6nyj2e39j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2d8d0pqxwiz6nyj2e39j.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Referencing the below DOM structure for ‘Remember me’ element:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="checkbox" name="remember" id="remember" class="form-check-input"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As you can see, the above tag has multiple attributes, one of which is the &lt;strong&gt;ID locator&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn78vfyett0w5phlzbajg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn78vfyett0w5phlzbajg.png" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This certification is for anyone who wants to stay ahead among professionals who are growing their career in Selenium automation testing.&lt;/p&gt;

&lt;p&gt;Here’s a short glimpse of the Selenium 101 certification from LambdaTest:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/qx9FPFfJm7E"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hey! Test your web and mobile apps on &lt;a href="https://www.lambdatest.com/android-emulator-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug31_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Android online Emulator&lt;/a&gt;. Ensure your apps are compatible across latest and legacy Android operating systems, devices, and browsers.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax For ID Locator In Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;In order to use the ID Selenium locator to find an element via ID we use the below syntax:&lt;/p&gt;

&lt;p&gt;driver.findElement(By.id("remember "));&lt;/p&gt;

&lt;p&gt;Now, let’s try to incorporate this into a real-time example. In this example, I will be using the facebook login page, to login using ID locators for interacting with the WebElement object.&lt;/p&gt;

&lt;p&gt;Below is the DOM structure for email, password and log in field of the facebook login page:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="email" class="inputtext" name="email" id="email" value="[sadhvisingh9049@gmail.com](mailto:sadhvisingh9049@gmail.com)" data-testid="royal_email"&amp;gt;



&amp;lt;input type="password" class="inputtext" name="pass" id="pass" data-testid="royal_pass"&amp;gt;



&amp;lt;input value="Log In" aria-label="Log In" data-testid="royal_login_button" type="submit" id="u_0_8"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Referenced is the screenshot highlighting the same:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiuhem8ziaa62pldcpkqh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiuhem8ziaa62pldcpkqh.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frhv6lu34rtvip9cjcygn.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frhv6lu34rtvip9cjcygn.jpg" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Delving Into Code For ID Locator In Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;The below code will help you relate with the demonstration of ID locator in Selenium WebDriver in a better way. Let’s dig into it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package Chromedriver;

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

public class Locator_By_ID {

public static void main(String[] args) {
        // TODO Auto-generated method stub

//Setting up chrome using chromedriver by setting its property
        System.setProperty("webdriver.chrome.driver", "Pth of chrome driver"); 

        //Opening browser
        WebDriver driver= new ChromeDriver() ;

        //Opening window tab in maximize mode
        driver.manage().window().maximize();

        //Opening application
        driver.get("[https://www.facebook.com](https://www.facebook.com)");

        //Locating the email field element via ID tag and storing it in the webelement
        WebElement email_field=driver.findElement(By.id("email"));

        //Entering text into the email field
        email_field.sendKeys("[sadhvisinghXXXX@gmail.com](mailto:sadhvisinghXXXX@gmail.com)");

        //Locating the password field element via ID tag and storing it in the webelement
        WebElement password_field=driver.findElement(By.id("pass"));

        //Entering text into the password field
        password_field.sendKeys("xxxxxxxxx");

        //Locating the login button to login to the application
        WebElement login_button=driver.findElement(By.id("u_0_2"));

        //Clicking on the 'login' button
        login_button.click();

}

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Referenced is the screenshot highlighting the same:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flisqmpcz93qxanboihm9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flisqmpcz93qxanboihm9.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmojse7h8tbs0tvg5l0lf.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmojse7h8tbs0tvg5l0lf.jpg" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hey! Test your web and mobile apps on &lt;a href="https://www.lambdatest.com/android-emulator-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug31_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;online Android Emulator&lt;/a&gt;. Ensure your apps are compatible across latest and legacy Android operating systems, devices, and browsers.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Delving Into Code For ID Locator In Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;The below code will help you relate with the demonstration of ID locator in Selenium WebDriver in a better way. Let’s dig into it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package Chromedriver;

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

public class Locator_By_ID {

public static void main(String[] args) {
        // TODO Auto-generated method stub

//Setting up chrome using chromedriver by setting its property
        System.setProperty("webdriver.chrome.driver", "Pth of chrome driver"); 

        //Opening browser
        WebDriver driver= new ChromeDriver() ;

        //Opening window tab in maximize mode
        driver.manage().window().maximize();

        //Opening application
        driver.get("[https://www.facebook.com](https://www.facebook.com)");

        //Locating the email field element via ID tag and storing it in the webelement
        WebElement email_field=driver.findElement(By.id("email"));

        //Entering text into the email field
        email_field.sendKeys("[sadhvisinghXXXX@gmail.com](mailto:sadhvisinghXXXX@gmail.com)");

        //Locating the password field element via ID tag and storing it in the webelement
        WebElement password_field=driver.findElement(By.id("pass"));

        //Entering text into the password field
        password_field.sendKeys("xxxxxxxxx");

        //Locating the login button to login to the application
        WebElement login_button=driver.findElement(By.id("u_0_2"));

        //Clicking on the 'login' button
        login_button.click();

}

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Another Example For Demonstrating The ID Locator
&lt;/h2&gt;

&lt;p&gt;Let’s cater to another similar example for ID where we are making a booking through Airbnb:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F76tmqgjybsgw9jn8pyet.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F76tmqgjybsgw9jn8pyet.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Below is the DOM structure for the same:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Location field:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="text" class="_up0kwni" aria-autocomplete="list" aria-describedby="Koan-magic-carpet-koan-search-bar__description" aria-expanded="false" autocomplete="off" autocorrect="off" spellcheck="false" id="Koan-magic-carpet-koan-search-bar__input" name="query" placeholder="Anywhere" role="combobox" value="Delhi"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Check-in:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="text" class="_14fdu48d" data-veloute="checkin_input" id="checkin_input" name="checkin" placeholder="dd-mm-yyyy" value="Wed, 10th Apr" readonly="" &amp;lt;="" pre=""&amp;gt;

&amp;lt;b&amp;gt;Checkout:&amp;lt;/b&amp;gt;







&amp;lt;pre&amp;gt; &amp;lt;input type="text" class="_14fdu48d" data-veloute="checkout_input" id="checkout_input" name="checkout" placeholder="dd-mm-yyyy" value="Sun, 14th Apr" readonly=""&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Search Button:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button type="submit" class="_z5x9aua" aria-busy="false"&amp;gt;&amp;lt;span class="_ftj2sg4"&amp;gt;Search&amp;lt;/span&amp;gt;&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now let’s dig into the code snippet:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package Chromedriver;

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

public class ID_Location {

public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub

//Setting up chrome using chromedriver by setting its property
        System.setProperty("webdriver.chrome.driver", "path of chromedriver"); 

        //Opening browser
        WebDriver driver= new ChromeDriver() ;

        //Opening window tab in maximize mode
        driver.manage().window().maximize();

        //Opening application
        driver.get("[https://www.airbnb.co.in/](https://www.airbnb.co.in/)");

        //Locate element via ID for Location field and store it in Webelement
        WebElement Location= driver.findElement(By.id("Koan-magic-carpet-koan-search-bar__input"));

        //Input value in Location field
        Location.sendKeys("Delhi", Keys.ENTER);

        //Locate element via ID for Check-in field and store it in Webelement
                WebElement Check_in= driver.findElement(By.id("checkin_input"));

                //Click on Check_in field to select the date
                Check_in.click();

                //Select the desired date
                driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='MagicCarpetSearchBar']/div[2]/div/div/div/div[2]/div/div/div/div/div/div[2]/div[2]/div/div[2]/div/table/tbody/tr[2]/td[3]")).click();

              //Locate element via ID for Check-out field and store it in Webelement
                WebElement Check_out= driver.findElement(By.id("checkout_input"));

                //Click on Check_out field to select the date
                Check_out.click();

                //Wait for date selection
                Thread.sleep(3500);

                //Select the desired date
                driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='MagicCarpetSearchBar']/div[2]/div/div/div/div[3]/div/div/div/div/div/div[2]/div[2]/div/div[2]/div/table/tbody/tr[2]/td[4]")).click();

                //Locating the submit button and clicking on it
                driver.findElement(By.tagName("button")).click();

                //close the driver
                driver.close();



    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hey! Test your web and mobile apps on &lt;a href="https://www.lambdatest.com/android-emulator-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug31_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;online Emulator Android&lt;/a&gt;. Ensure your apps are compatible across latest and legacy Android operating systems, devices, and browsers.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  It’s A Wrap!
&lt;/h2&gt;

&lt;p&gt;ID’s are the most reliable and easiest way to locate an element owing to its uniqueness. So if you are starting with Selenium WebDriver, ID’s can be your best friend. But make sure you to hit on the right track while locating them, and be aware of duplicate IDs or auto-generated ones. Happy testing! 🙂&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>devops</category>
      <category>webdev</category>
      <category>testing</category>
    </item>
    <item>
      <title>How To Use Name Locator In Selenium Automation Scripts?</title>
      <dc:creator>sadhvisingh1</dc:creator>
      <pubDate>Wed, 10 Apr 2019 14:42:50 +0000</pubDate>
      <link>https://dev.to/testmuai/how-to-use-name-locator-in-selenium-automation-scripts-59jg</link>
      <guid>https://dev.to/testmuai/how-to-use-name-locator-in-selenium-automation-scripts-59jg</guid>
      <description>&lt;p&gt;Locators in Selenium play an important role in the life of an automation engineer. Depending on how skilled you are in locating an element is directly proportional to the stability and efficiency of your automation testing with Selenium. There are multiple ways of utilizing locators in Selenium to find an element on a page but deciding the right one, does the business. In this article, I will be referencing on how to use by.name locator in Selenium automation scripts.&lt;/p&gt;

&lt;p&gt;If you’re new to Selenium and wondering what it is then we recommend checking out our guide — &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;What is Selenium?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are an advanced or medium Selenium practitioner, then you can chuck on dedicated articles mentioned above. And go for our complete guide to help you illustrate the practical demonstration of CSS locator in Selenium.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding The DOM(Document Object Model)
&lt;/h2&gt;

&lt;p&gt;The Document Object Model(DOM) defines the structure of a webpage which constitutes of various tag elements and their corresponding attributes. For example, below shows the LambdaTest login page:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffotzskzcpben39b6dxqy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffotzskzcpben39b6dxqy.png" width="800" height="383"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the screenshot above, we are trying to locate the ‘email’ field. The DOM structure of the email field is displayed below:&lt;/p&gt;

&lt;p&gt;&amp;lt; input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg" &amp;gt;&lt;/p&gt;

&lt;p&gt;The attributes for the above DOM structure are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Type&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Name&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Value&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Placeholder&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Required&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Autofocus&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Class&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hey! For &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;selenium testing tool&lt;/a&gt;, click here. Test on Selenium Grid Cloud of 3000+ Desktop &amp;amp; Mobile Browsers.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding The By.name Locator In Selenium
&lt;/h2&gt;

&lt;p&gt;By.name Selenium locator is used to identify the elements of a webpage. This attribute can be mentioned as part of multiple tags like &amp;lt; input &amp;gt;, &amp;lt; button &amp;gt;, &amp;lt; select &amp;gt; etc. Unlike ID, this may or may not be unique to a page. A webpage may contain multiple tags with the same By.name attribute value. In such a case, if your intent is to select your desired element, the By.name locator in Selenium may not be the correct choice.&lt;/p&gt;

&lt;p&gt;A key point to note is when you tend to select an element with a desired name attribute value, it will select the first element it encounters. You may come across another scenario, where you wish to select elements with the same name attribute value, in this case, the &lt;strong&gt;By.name&lt;/strong&gt; locator in Selenium may work using the &lt;strong&gt;findElements&lt;/strong&gt; syntax. I will show both scenarios example in the code as we go ahead in the article.&lt;/p&gt;

&lt;p&gt;In order to locate element via the By.name locator in Selenium, we use the below command:&lt;/p&gt;

&lt;p&gt;driver.findElement(By.name(“Element NAME”));&lt;/p&gt;

&lt;p&gt;Let’s dig into the code snippet to understand the usage of the By.namelocator.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgyiuw0twgpexls7kf4w7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgyiuw0twgpexls7kf4w7.png" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This certification is for anyone who wants to stay ahead among professionals who are growing their career in Selenium automation testing.&lt;/p&gt;

&lt;p&gt;Here’s a short glimpse of the Selenium 101 certification from LambdaTest:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/qx9FPFfJm7E"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;How to choose the right &lt;a href="https://www.lambdatest.com/blog/automation-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;automation testing tool&lt;/a&gt;? Check out this blog to select the right tools for automation testing.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Test Scenario 1 For By.nameLocator In Selenium
&lt;/h2&gt;

&lt;p&gt;In the below code example, we are logging on to the LambdaTest platform. Here we will locate the ‘email’ and ‘password’ field using the By.name attribute.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package Chromedriver;

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

public class Name_Locator {

public static void main(String[] args) {

        //Setting up chrome using chromedriver by setting its property
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\navyug\\workspace\\Lambdatest\\src\\Chromedriver\\chromedriver.exe"); 

        //Opening browser
        WebDriver driver= new ChromeDriver() ;

        //Opening window tab in maximize mode
        driver.manage().window().maximize();

        //Opening application
        driver.get("[https://accounts.lambdatest.com/login](https://accounts.lambdatest.com/login)");

        //Locating the email field element via Name tag and storing it in the webelement
        WebElement email_field=driver.findElement(By.name("email"));

        //Entering text into the email field
        email_field.sendKeys("[sadhvisingh24@gmail.com](mailto:sadhvisingh24@gmail.com)");

        //Locating the password field element via Name tag and storing it in the webelement
        WebElement password_field=driver.findElement(By.name("password"));

        //Entering text into the password field
        password_field.sendKeys("LoremIpsum");

        //Clicking on the login button to login to the application
        WebElement login_button=driver.findElement(By.xpath("//button[text()='LOGIN']"));

        //Clicking on the 'login' button
        login_button.click();

        //Closing the window
        driver.close();
        }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Hey! Perform live-interactive cross browser testing on &lt;a href="https://www.lambdatest.com/safari-browser-for-windows?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug30_sd&amp;amp;utm_term=sd&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Safari for windows&lt;/a&gt; here.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Test Scenario 2 For By.name Locator In Selenium
&lt;/h2&gt;

&lt;p&gt;In the below code example, post-logging in, under the automation section of the menu bar, we have a page where two web elements containing radio button have the same By.name attribute value as radio. We intend to switch between these two buttons. Below is the DOM structure for both those elements:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;amp;lt; div class="TimelineTab_viewRadioButton__18Zf3"&amp;amp;gt;
&amp;amp;lt; label &amp;amp;gt;&amp;lt;input type="radio" name="radio" value="build_view"&amp;gt;&amp;lt;span class="TimelineTab_labelText__2DZ00 TimelineTab_font14__2K4jw"&amp;gt;Build View&amp;lt;/span&amp;gt;
&amp;lt;!-- label--&amp;gt;&amp;amp;lt; label&amp;amp;gt;&amp;amp;lt; input type="radio" name="radio" value="test_view"&amp;amp;gt;
&amp;lt;span class="TimelineTab_labelText__2DZ00 TimelineTab_font14__2K4jw"&amp;gt;Test View&amp;lt;!-- span--&amp;gt;&amp;lt;!-- label --&amp;gt;&amp;amp;lt; /div &amp;amp;gt;&amp;lt;/span&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The code snippet below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package Chromedriver;

import java.util.List;
import java.util.concurrent.TimeUnit;

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

public class Multiple_Name_Values {

public static void main(String[] args) {

//Setting up chrome using chromedriver by setting its property
                System.setProperty("webdriver.chrome.driver", "C:\\Users\\navyug\\workspace\\Lambdatest\\src\\Chromedriver\\chromedriver.exe"); 

                //Opening browser
                WebDriver driver= new ChromeDriver() ;

                //Opening window tab in maximize mode
                driver.manage().window().maximize();

                //Maintaining an implicit wait for the entire application in case of throttling network or DOM not loaded
                driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
                //Opening application
                driver.get("[https://accounts.lambdatest.com/login](https://accounts.lambdatest.com/login)");

                //Locating the email field element via Name tag and storing it in the webelement
                WebElement email_field=driver.findElement(By.name("email"));

                //Entering text into the email field
                email_field.sendKeys("[sadhvisingh24@gmail.com](mailto:sadhvisingh24@gmail.com)");

                //Locating the password field element via Name tag and storing it in the webelement
                WebElement password_field=driver.findElement(By.name("password"));

                //Entering text into the password field
                password_field.sendKeys("LoremIpsum");

                //Clicking on the login button to login to the application
                WebElement login_button=driver.findElement(By.xpath("//button[text()='LOGIN']"));

                //Clicking on the 'login' button
                login_button.click();

                //Click on the Automation menu link on the sidebar
                driver.findElement(By.xpath("//*[[@id](http://twitter.com/id)='app']/header/aside/ul/li[4]/a/span")).click();

//Finding all the webelement on the page with name attribute value as radio
                List&amp;lt;webelement&amp;gt; radio_button=driver.findElements(By.name("radio"));

                //Click on the test view of the element
                radio_button.get(1).findElement(By.xpath("//span")).click();

                //Closing the driver
                driver.close();
    }
}
&amp;lt;/webelement&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As you can see in the above two examples, we have used the By.name locator in different ways. One was via &lt;strong&gt;findElement&lt;/strong&gt; and the other was via &lt;strong&gt;findElements command&lt;/strong&gt;. In one, our target was to locate a single element while in the other we located multiple elements with the same By.name and attempted to switch to one of those elements. So as per defined need and business, we can use the By.name locator. By.name locators are easy to use and maintain, the trick lies in identifying their right usage.&lt;/p&gt;

&lt;p&gt;You can also read my previous article where I have explained the demonstration of all the locators in Selenium WebDriver. I will also be writing down a series of dedicated articles to demonstrate thorough usage of each locator of Selenium in detail. Stay tuned! 🙂&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>wev</category>
      <category>devops</category>
      <category>writing</category>
    </item>
    <item>
      <title>Locators In Selenium WebDriver With Examples</title>
      <dc:creator>sadhvisingh1</dc:creator>
      <pubDate>Fri, 05 Apr 2019 10:44:34 +0000</pubDate>
      <link>https://dev.to/testmuai/locators-in-selenium-webdriver-with-examples-182i</link>
      <guid>https://dev.to/testmuai/locators-in-selenium-webdriver-with-examples-182i</guid>
      <description>&lt;p&gt;Locators in Selenium are one of the most powerful commands. Its ideally the building block of the Selenium automation scripts. It helps locate the GUI elements through which multiple user actions can be performed. These are one of the important parameters for scripting, and if they end up to be incorrect or brittle, they may lead to script failure. A good scripting base foundation requires elements to be located appropriately. For this, we have multiple locators in Selenium WebDriver. Below is the list of these locators of Selenium WebDriver :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ID &lt;/li&gt;
&lt;li&gt;Name&lt;/li&gt;
&lt;li&gt;Linktext&lt;/li&gt;
&lt;li&gt;Partial Linktext&lt;/li&gt;
&lt;li&gt;Tag Name&lt;/li&gt;
&lt;li&gt;Class Name&lt;/li&gt;
&lt;li&gt;DOM Locator&lt;/li&gt;
&lt;li&gt;CSS Selector&lt;/li&gt;
&lt;li&gt;Xpath&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using the above locators in Selenium WebDriver you can locate elements through &lt;strong&gt;“findElement/findElements”&lt;/strong&gt; syntax. I will be demonstrating that for each locator of Selenium, in detail below. However, before we dig in to locate these elements through the above ways, let’s see how to find elements in DOM(Document object model).&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps To Find Element In DOM:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Open the target application and click on F12 or right click and select inspect.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage8-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage8-1.png" alt="cross browser testing" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A console window would open known Developer tools.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage11-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage11-1.png" alt="cross browser testing" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A section name as ‘Element’ would be default opened. This is where we locate elements through. To the left most, you can observe a mouse icon. As you hover on it, it would state ’select an element in the page to inspect it’. Click on it and navigate to the element you wish to locate to. As you click on the element you want to locate, the DOM would be highlighted for that element, something like below:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage12-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage12-1.png" alt="cross browser testing" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;This is it, the selected row in the DOM is the context from where you need to fetch your values from, in the example above, the highlighted DOM value is:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;a href="https://accounts.lambdatest.com/register" target="_blank"&amp;gt;Free Sign Up&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can choose the tagname i.e ‘a’ and text i.e ‘Free Sign Up’ to locate your element.&lt;/p&gt;

&lt;p&gt;The above technique will be used throughout my article for demonstrating the CSS locators in Selenium WebDriver . Now, we look on how to find locators in Selenium WebDriver.&lt;/p&gt;

&lt;h2&gt;
  
  
  ID Locator In Selenium
&lt;/h2&gt;

&lt;p&gt;This is the most common way of locating elements considering that they are unique to each element in the DOM(Document Object Model). Based on the World Wide Web Consortium(W3C) ID’s should be unique to elements and hence are considered as the fastest and safest method to locate element. But unfortunately, developers may or may not follow this rule as browsers do allow bypassing this rule. Specifically, in the case of a table or list the ID’s may populate incrementally or dynamically depending upon the data, which leads to locating such elements through other means.&lt;/p&gt;

&lt;p&gt;Below is an example of Makemytrip showcasing how ‘login’ field is been located via ID:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage3-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage3-1.png" alt="ID Locator In Selenium" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Below is the DOM structure of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="inputM make_relative"&amp;gt; &amp;lt;input id="ch_login_email" type="text" required=""&amp;gt; &amp;lt;span class="inputM__highlight"&amp;gt;&amp;lt;/span&amp;gt; &amp;lt;span class="inputM__bar"&amp;gt;&amp;lt;/span&amp;gt; &amp;lt;label&amp;gt;Email/Mobile number&amp;lt;/label&amp;gt; &amp;lt;div class="ch-error-msg ch-clearfix" id="ch_login_email_error" style="display:none"&amp;gt; &amp;lt;p class="o-i-error-icon"&amp;gt;&amp;lt;/p&amp;gt; &amp;lt;span class="ch-flL"&amp;gt; &amp;lt;span class="o-i-error-symbol"&amp;gt;&amp;lt;/span&amp;gt; &amp;lt;/span&amp;gt; &amp;lt;span class="ch-flL ch-error-innertxt"&amp;gt;&amp;lt;/span&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt;The syntax for locating via 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Syntax for locating Username is= &lt;code&gt;driver.findElement(By.id("ch_login_email"))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In case no such value matches with id, &lt;strong&gt;NoSuchElementException&lt;/strong&gt; will be raised.&lt;/p&gt;

&lt;h2&gt;
  
  
  Name Locator In Selenium
&lt;/h2&gt;

&lt;p&gt;An element can be defined via multiple attributes, one such is Name. Name locator in Selenium WebDriver can also be used to locate elements like ID locator. They may or may not be unique on a page, having multiple elements. In case there are elements with the same name, then the locator selects the first element with that name on the page.&lt;br&gt;&lt;br&gt;
In case no such name matches with the defined attribute value, NoSuchElementException will be raised&lt;/p&gt;

&lt;p&gt;Below is a screenshot of the DOM for Lambdatest, where the ID locator name on sign up page for email field is highlighted. &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage2-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage2-1.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Below is the DOM structure of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="email" placeholder="Work Email*" name="email" value="" class="form-control sign-up-input-2 "&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is the syntax for locating the email field via name&lt;br&gt;&lt;br&gt;
Syntax for locating Email is =&lt;code&gt;driver.findElement(By.name("email"));&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Link Text Locator In Selenium
&lt;/h2&gt;

&lt;p&gt;Elements can be located via link text as in hyperlinks. In a scenario where there are multiple links of the same text, the first link would be selected. Link texts are prefixed with the anchor text and this locator can only be used for anchor tag.&lt;/p&gt;

&lt;p&gt;Below is an example of lambdatest homepage showcasing selection of the blog link on the header. The DOM below shows the highlighted element:&lt;br&gt;&lt;br&gt;
 &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage7-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage7-1.png" alt="Link Text Locator In Selenium" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Below is the DOM structure of the same:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;a href="https://www.lambdatest.com/blog" target="_blank"&amp;gt;Blog&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The syntax for selecting the linktext=&lt;code&gt;driver.findElement(By.linkText("Blog"))&lt;/code&gt;;&lt;br&gt;&lt;br&gt;
Usually this locator is used to check for navigation flow.&lt;/p&gt;
&lt;h2&gt;
  
  
  Partial Link Text Locator In Selenium
&lt;/h2&gt;

&lt;p&gt;Locating element via Partial Link Text works similar to normal Link Text locator. The reason of using the Partial Link Text locator in Selenium WebDriver over the Link Text Locator is only due to the reason when you have a long linktext and you intent to use only partial text to perform further actions on it. Sometimes the intent of using this can also be to locate multiple links on a page with a common partial text.&lt;/p&gt;

&lt;p&gt;Below is a snapshot of the &lt;a href="https://www.lambdatest.com/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=sa-06-050419eu&amp;amp;utm_term=OrganicPosting" rel="noopener noreferrer"&gt;LambdaTest&lt;/a&gt; DOM highlighting the element with the link name as ‘start testing’, here we are locating the link with partial text as ‘testing’.&lt;br&gt;&lt;br&gt;
 &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage5.png" alt="Partial Link Text Locator In Selenium" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Below is the DOM structure of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;a href="https://accounts.lambdatest.com/register" target="_blank"&amp;gt;START TESTING &amp;lt;i class="fa fa-arrow-circle-right" aria-hidden="true"&amp;gt;&amp;lt;/i&amp;gt;&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The syntax for locating via partial link text is: &lt;code&gt;driver.findElement(By.PartialLinkText("Testing"));&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Tag Name Locator In Selenium
&lt;/h2&gt;

&lt;p&gt;As the name specifies, this css locator in Selenium WebDriver is used to identify elements with Tag names like div tag, a tag etc. A common example of this usage could be locating all links on your homepage and verifying whether they are functional or broken.&lt;/p&gt;

&lt;p&gt;The below syntax for locating all links on Lambdatest home:&lt;br&gt;&lt;br&gt;
&lt;code&gt;driver.findElements(By.tagName(a));&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Class Name Locator In Selenium
&lt;/h2&gt;

&lt;p&gt;Class Name locator helps in locating element defined through the class attribute. Below is an example of Lambdatest login DOM snapshot where login field is located via class name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage1-6-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage1-6-1.png" alt="Class Name Locator In Selenium" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Below is the DOM structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Syntax of locating element via class name:&lt;br&gt;&lt;br&gt;
&lt;code&gt;driver.findElement(By.className("form-control mt-3 form-control-lg "));&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  DOM Locator In Selenium
&lt;/h2&gt;

&lt;p&gt;In document object model(DOM) we locate element in terms of DOM model. As explained above we can identify element via ID and name through methods of the DOM like &lt;strong&gt;‘getElementById’&lt;/strong&gt; and &lt;strong&gt;‘getElementsByName’&lt;/strong&gt;. The method getElementById will locate only one element at a time, whereas the other method is used to provide an array of elements located by that name. In order to access any specified element in case of an array of elements, we can use index.&lt;/p&gt;

&lt;p&gt;The syntax used to access id and name through DOM is:&lt;br&gt;&lt;br&gt;
&lt;code&gt;document.getElementById (“id”)&lt;/code&gt;&lt;br&gt;&lt;br&gt;
&lt;code&gt;document.getElementsByNames (“name”)[index]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Below is the DOM structure of &lt;a href="https://accounts.lambdatest.com/login?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=sa-06-050419eu&amp;amp;utm_term=OrganicPosting" rel="noopener noreferrer"&gt;LambdaTest login page&lt;/a&gt;, where the intent is to locate the ‘remember me’ checkbox: &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage10-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage10-1.png" alt="LambdaTest login page" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="checkbox" name="remember" id="remember" class="form-check-input"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example to access element via DOM ID is:&lt;br&gt;&lt;br&gt;
&lt;code&gt;document.getElementById (“remember”)&lt;/code&gt;&lt;br&gt;&lt;br&gt;
&lt;code&gt;document.getElementsByNames (“remember”)[0]&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  CSS Selector Locator In Selenium CSS Selector Locator In Selenium
&lt;/h2&gt;

&lt;p&gt;Cascading style sheets are used to style your webpages and hence becomes one of the ways to locate your elements. If you are unable to access an element without ID or name, then CSS selector becomes one of the most obvious choices as compared to xpath.&lt;/p&gt;

&lt;p&gt;Since multiple debates go around the corner for both of them, their usages for me depends on the complexity of the scenario, though majorly people prefer using CSS selector since those are faster as compared to xpath.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CSS Selectors can be located through various formats available:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tag and ID&lt;/li&gt;
&lt;li&gt;Tag and Class&lt;/li&gt;
&lt;li&gt;Tag and Attribute&lt;/li&gt;
&lt;li&gt;Tag, Class and Attribute&lt;/li&gt;
&lt;li&gt;Matches (Starts with, Ends with, Contains)&lt;/li&gt;
&lt;li&gt;Child elements&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Tag and ID
&lt;/h3&gt;

&lt;p&gt;To locate element via Tag and ID we use three components&lt;br&gt;&lt;br&gt;
Syntax: &lt;code&gt;css=(Html tag )(#) (value of the ID attribute)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Html tag:&lt;/strong&gt; It is used to provide the tag we wish to locate, example input tag.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;# :&lt;/strong&gt; This hash sign is used to represent the ID attribute. Keep in mind, when you wish to locate an element via ID through CSS selector then it required to have a hash sign on the same. For other attributes we need not use the hash sign.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Value of the ID attribute:&lt;/strong&gt; This represents the value of the ID we are using the locate the element.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Below is the DOM part indicating the login field of Makemytrip.com &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage6-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage6-1.png" alt="Tag and ID" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;div class="inputM make_relative"&amp;gt;
 &amp;lt;input id="ch_login_email" type="text" required=""&amp;gt; &amp;lt;span class="inputM__highlight"&amp;gt;&amp;lt;/span&amp;gt; &amp;lt;span class="inputM__bar"&amp;gt;&amp;lt;/span&amp;gt; &amp;lt;label&amp;gt;Email/Mobile number&amp;lt;/label&amp;gt; &amp;lt;div class="ch-error-msg ch-clearfix" id="ch_login_email_error" style="display:none"&amp;gt; &amp;lt;p class="o-i-error-icon"&amp;gt;&amp;lt;/p&amp;gt; &amp;lt;span class="ch-flL"&amp;gt; &amp;lt;span class="o-i-error-symbol"&amp;gt;&amp;lt;/span&amp;gt; &amp;lt;/span&amp;gt; &amp;lt;span class="ch-flL ch-error-innertxt"&amp;gt;&amp;lt;/span&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to locate element via CSS selector, the following command can be written as:&lt;br&gt;&lt;br&gt;
&lt;code&gt;driver.findElement(By.cssSelector("input# ch_login_email "))&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Tag and Class
&lt;/h3&gt;

&lt;p&gt;This locator works like ID, only difference is in its format. We use dot while denoting the class attribute value rather than hash in case of class.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;br&gt;&lt;br&gt;
&lt;code&gt;css=(HTML tag)(.)(Value of Class attribute)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The below DOM snapshot and code highlights the class attribute to be accessed: &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage4.png" alt="Tag and Class" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;driver.findElement(By.cssSelector("form-control mt-3 form-control-lg"))&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Tag and Attribute
&lt;/h3&gt;

&lt;p&gt;As the name specifies the element can be locate via tag name and its corresponding attribute defined with value. In case if multiple elements have the same tag and attribute, the first one will be selected.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;br&gt;&lt;br&gt;
&lt;code&gt;css=(HTML Page)[Attribute=Value]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using the below DOM structure, the following code will be used to locate the web element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;input type="phone" placeholder="Phone*" name="phone" value="" class="form-control sign-up-input-2 "&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;driver.findElement(By.cssSelector("input[name= ‘phone’]"))&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Tag, class and attribute
&lt;/h3&gt;

&lt;p&gt;This locator is used in combination with class name and other attribute value.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;br&gt;&lt;br&gt;
&lt;code&gt;css=(HTML tag&amp;gt;)(. )(Class attribute value)([attribute=Value of attribute])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using the below DOM structure, to locate elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button type="submit" class=" btn sign-up-btn-2 btn-block"&amp;gt;Signup&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Syntax to locate element via class :&lt;br&gt;&lt;br&gt;
&lt;code&gt;driver. findElement(By.cssSelector(“button. btn sign-up-btn-2 btn-block [type = submit]”))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This combination can also be implied on ID. The only difference is to use hash rather than dot when using ID attribute and defining its ID value in place of class value.&lt;/p&gt;
&lt;h3&gt;
  
  
  Locating Elements via Matches
&lt;/h3&gt;

&lt;p&gt;Selenium CSS selector,helps in matching multiple strings through the use of multiple patterns like ^, $, *. Below are quick details on the same:&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Starts-With&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This helps in locating element when we try to match elements with string that starts with a designated value&lt;/p&gt;

&lt;p&gt;Syntax:&lt;br&gt;&lt;br&gt;
&lt;code&gt;css=(HTML tag)([attribute^=start of the string])&lt;/code&gt;&lt;br&gt;&lt;br&gt;
Referencing below DOM structure to locate element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;driver.findElement(By.cssSelector("input[name^='em']"))&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Ends-With&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This helps in locating element when we try to match elements with string that ends with a designated value.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;br&gt;&lt;br&gt;
&lt;code&gt;css=(HTML tag)([attribute$=end of the string])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Referencing below DOM structure to locate element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;driver.findElement(By.cssSelector("input[name$=’ail’]"))&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Contains&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;This helps in locating element when we try to match elements with string that contains a designated value.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;br&gt;&lt;br&gt;
&lt;code&gt;css=(HTML tag)([attribute*=partial string])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using the same login DOM structure to locate element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;driver.findElement(By.cssSelector("input[class*=’control’]"))&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Child Elements
&lt;/h3&gt;

&lt;p&gt;With the use of child elements, we can locate elements inside other elements. This is helpful when trying to access data of a table or list of details etc.&lt;/p&gt;

&lt;p&gt;Below is the DOM structure where I intent to access text written with ‘Screenshot’:&lt;br&gt;&lt;br&gt;
 &lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage9-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.lambdatest.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F03%2Fimage9-1.png" alt="child elements" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ul class="overview-list"&amp;gt;
&amp;lt;li class="automation"&amp;gt;
 &amp;lt;span&amp;gt;Automation Test&amp;lt;/span&amp;gt;&amp;lt;/li&amp;gt;
 &amp;lt;li class="realtime"&amp;gt;
 &amp;lt;span&amp;gt;Realtime Test&amp;lt;/span&amp;gt;&amp;lt;/li&amp;gt;
 &amp;lt;li class="screenshot"&amp;gt;
 &amp;lt;span&amp;gt;Screenshot Test&amp;lt;/span&amp;gt;&amp;lt;/li&amp;gt; 
&amp;lt;li class="responsive"&amp;gt;
 &amp;lt;span&amp;gt;Responsive Test&amp;lt;/span&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to locate element, the following syntax would be used:&lt;br&gt;&lt;br&gt;
&lt;code&gt;Css= tagname.class name li:nth-of-child(index of the referenced child which in our case is 3)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;driver.findElement(By.cssSelector(“ul. overview-list li:nth-of-child(3)”);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Similarly in order to access responsive we can use, last-child reference as below:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Css= ul. overview-list li:last-child&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;driver.findElement(By.cssSelector(“ul. overview-list li:last-child”);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Do check our blog on &lt;a href="https://www.lambdatest.com/blog/how-pro-testers-use-css-selectors-in-selenium-automation-scripts/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=sa-06-050419eu&amp;amp;utm_term=OrganicPosting" rel="noopener noreferrer"&gt;CSS Selectors in Selenium Automation Scripts&lt;/a&gt; to help you understand this CSS locator in Selenium WebDriver better.&lt;/p&gt;
&lt;h2&gt;
  
  
  XPath Locator In Selenium
&lt;/h2&gt;

&lt;p&gt;Xpath helps in locating elements on the web page using XML expressions. The basic syntax used for using XPath as a CSS locator in Selenium WebDriver is:&lt;br&gt;&lt;br&gt;
&lt;code&gt;Xpath: //tagname[@attribute=’value’]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here tagname signifies the tag in the DOM structure you are targeting to, for example an input tag or anchor tag etc. Attributes are defined via prefix ‘@’ and their corresponding value. Different attributes like name, id, class etc can be used here. There are multiple ways through which xpath can be defined:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Standard Xpath&lt;/li&gt;
&lt;li&gt;Using Contains&lt;/li&gt;
&lt;li&gt;Using Xpath with AND &amp;amp; OR&lt;/li&gt;
&lt;li&gt;Using starts-with&lt;/li&gt;
&lt;li&gt;Using text in Xpath&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s take a quick look into their details:&lt;/p&gt;
&lt;h3&gt;
  
  
  Standard Xpath
&lt;/h3&gt;

&lt;p&gt;This is like the one defined above in syntax.&lt;br&gt;&lt;br&gt;
A quick example is for the below DOM structure of LambdaTest I am referencing to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Syntax of xpath is: &lt;code&gt;//input[@name= ’email’]&lt;/code&gt;In order to locate element, it can be written as follows:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;driver.findElement(By.xpath(“//input[@name= ’email’]”))&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Contains
&lt;/h3&gt;

&lt;p&gt;This works similar to CSS selector ‘contains’ symbol. It can be used when any element value changes dynamically and partially. For example, if value of login changes with the login text appended has been constant, then contains would be helpful in locating elements. Below is the referenced syntax of it:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Xpath: //tagname[contains(@attribute, ‘partial value of attribute’)]&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using the below DOM structure to locate the login field with class name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;driver.findElement(By.xpath(“//input[contains(@class, ‘form-control’)]”))&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Xpath using ‘AND’ &amp;amp; ‘OR’
&lt;/h3&gt;

&lt;p&gt;These are used when we want to locate an element via two condition sets. In case of ‘AND’ both the conditions should be true and for ‘OR’ either of the two should be true.&lt;/p&gt;

&lt;p&gt;Syntax using OR :&lt;br&gt;&lt;br&gt;
&lt;code&gt;Xpath=//input[@id='login_1' OR @name='login’]&lt;/code&gt; Syntax using AND :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Xpath=//input[@id='login_1' AND @name='login’]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The below example highlights the login field DOM structure of Lamdatest using both the above ‘AND’ and ‘OR’ expressions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;driver.findElement(By.xpath(“//input[@type='email' OR @name='email’]))&lt;/code&gt;&lt;br&gt;&lt;br&gt;
&lt;code&gt;driver.findElement(By.xpath(“//input[@type='email' AND @name='email’]))&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Starts-with
&lt;/h3&gt;

&lt;p&gt;This is again like the functionality of CSS selector. It helps in locating element that starts with a specified attribute value. Syntax used is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Xpath=//tagname[starts-with(@attribute,'starting name of the attribute value')]&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Using the below DOM structure to locate the password field of signup form of Lambdatest:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="password" placeholder="Desired Password*" name="password" class="form-control sign-up-input-2 " aria-autocomplete="list"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;driver.findElement(By.xpath(“//input[starts-with(@name,'pass')]”))&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Text
&lt;/h3&gt;

&lt;p&gt;It helps to locate element via xpath using exact text match. Sometimes we have tags containing text, which we wish to locate element through. Locating element through text can help us achieve this.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;br&gt;&lt;br&gt;
&lt;code&gt;Xpath=//div[text()='Logged In']&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Below is an example of Lambdatest DOM structure of sign-up page, where I am trying to locate the sign-up text on that page.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;p class="signup-titel"&amp;gt;SIGN UP&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using above DOM structure, following is the use case of text:&lt;br&gt;&lt;br&gt;
&lt;code&gt;driver.findElement(By.xpath(“//p[@text()=’ SIGN UP’]”))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Both CSS selector and Xpath are measured equally in terms of CSS locators in Selenium WebDriver. They both are useful with respect to complex scenarios. Choosing which one among the two is completely upon you and the scenario you opt to automate. The only key thing to remember is easy maintainability of your locators, this makes half of your job easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices For Using Locators In Selenium WebDriver
&lt;/h2&gt;

&lt;p&gt;Keeping in mind which locator to choose is as important as ensuring you know all the locators. Certain best practices and rules have been laid down to ensure you make efficient use of locators in Selenium WebDriver. Listing few below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Do not locate elements that depends on information which may change as they may make your locators easy to break and less maintainable.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is one of the key rules that one needs to keep in mind while &lt;a href="https://www.lambdatest.com/blog/8-actionable-insights-to-write-better-automation-code/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=sa-06-050419eu&amp;amp;utm_term=OrganicPosting" rel="noopener noreferrer"&gt;writing better automation code&lt;/a&gt; using locators in Selenium WebDriver. If locator is dependent on a single entity like for instance class, name, id etc which if changed may need to be repaired but in case of complex locators like &lt;code&gt;By.XPath(“//div[@class=”class-form_6180″]/div[5]/h3”)&lt;/code&gt;, it may lead to frequent breakage if any of the div’s changes or the class name etc. Try to make your locators in Selenium WebDriver precise and dependent on single entity than multiple. Only in case you do not have any option left, move to complex locators, but make sure the dependent elements have less likelihood of changing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Ensure your locator In Selenium matches only the required information to be selected and not multiple other information present along with it.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is contextual and depends upon your scenario. In case you wish to locate single element ensure it matches uniquely to only one element. It should never be the case, where there are multiple elements identified via locator and you opt to choose for the second or third selection. This can be a point of your script breakage, since if the page design changes or the assumed count on which the selected element appears changes, your locator will tend to break. So always make sure, your locator locates the exact match.&lt;br&gt;&lt;br&gt;
Secondly, if you are trying to look out for multiple matches (using ‘findElements’), ensure it matches all the desired elements you are looking out for.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;While using locators in Selenium, do not locate elements that depend on auto-generated values.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When new to this business this is something we get into. Usually ID’s are auto generated and while locating them and using them in scripts could be problematic. Every time the script or page containing those ID’s are run they tend to change and make your scripts fail. The easiest solution to figure out whether they are auto generated or not is watching them through minimum 2 runs and identifying them through the prefix/post fixed number associated with it. In case they are changed or incremented, you can conclude those are auto generated and you look out for other ways to locate those elements.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;While choosing locators of Selenium, do not use XPath or CSS Selector provided by the Dev Tools.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Yes, you read that right! Copying xpath or css selector from developer tools is something we believe is the easiest task to do, but believe me these are one of the problems that appears in the longer run, causing problems in the stability and readability of your scripts. Your browser providing you these values does not look out for meaningful XPath or CSS locators and give you complex ones, with multiple dependent factors, which I mentioned above may lead to frequent breakages. So even though this may look tempting and easier to do activity try refraining yourself from it.&lt;/p&gt;

&lt;p&gt;These are some of the factors one should keep in mind while writing locators in Selenium WebDriver. The technique or strategy you use to locate elements can be any but make sure the above rules are fulfilled which helps to make your script easy to maintain and read. These locators in Selenium are the building blocks of your script, make sure they are correct and your road ahead on it is smoother and more focused towards other aspects of the functionality of your project as you automate them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://accounts.lambdatest.com/register/?utm_source=dev&amp;amp;utm_medium=Blog&amp;amp;utm_campaign=sa-06-050419eu&amp;amp;utm_term=OrganicPosting" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0e5vtqzcoqm2zr3cb7la.jpg" width="800" height="154"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
