<?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: ArunGupta651</title>
    <description>The latest articles on DEV Community by ArunGupta651 (@arungupta651).</description>
    <link>https://dev.to/arungupta651</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%2F1132104%2F5a1fd6d6-69b3-484a-9175-fe8fddbe9bae.png</url>
      <title>DEV Community: ArunGupta651</title>
      <link>https://dev.to/arungupta651</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arungupta651"/>
    <language>en</language>
    <item>
      <title>How To Use Arrays.asList() In Java [With Examples]</title>
      <dc:creator>ArunGupta651</dc:creator>
      <pubDate>Fri, 04 Aug 2023 09:38:17 +0000</pubDate>
      <link>https://dev.to/arungupta651/how-to-use-arraysaslist-in-java-with-examples-2lo0</link>
      <guid>https://dev.to/arungupta651/how-to-use-arraysaslist-in-java-with-examples-2lo0</guid>
      <description>&lt;p&gt;Test automation can be quite challenging sometimes when dealing with complex &lt;a href="https://www.lambdatest.com/learning-hub/test-case?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=learning_hub"&gt;test cases&lt;/a&gt;. Although there are multiple ways available in any programming language to solve a particular problem, you need to be vigilant that your solution follows the best and the most efficient coding practices.&lt;/p&gt;

&lt;p&gt;A fundamental yet important approach while designing an &lt;a href="https://www.lambdatest.com/blog/automation-testing-frameworks/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;automation testing framework&lt;/a&gt; is the parameterization of data-driven test cases. A data-driven test case can be invoked multiple times with a different set of data values, which are passed as arguments to the parameters defined in the test method to achieve parameterization.&lt;/p&gt;

&lt;p&gt;Talking about Java as the programming language for &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;automation testing&lt;/a&gt;, there are many ways available in which you can parameterize your tests with tools like &lt;a href="https://www.lambdatest.com/learning-hub/webdriver?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=learning_hub"&gt;Selenium WebDriver&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/appium-mobile-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;Appium&lt;/a&gt;, etc. But there is one method that can primarily be used for this purpose: &lt;em&gt;Arrays.asList()&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Arrays.asList()&lt;/em&gt; in Java is an important method that acts as a bridge between the &lt;em&gt;array&lt;/em&gt; and &lt;em&gt;collection&lt;/em&gt; interface in Java and provides many ways to implement parameterization.&lt;/p&gt;

&lt;p&gt;In this blog on &lt;em&gt;Arrays.asList()&lt;/em&gt; in Java, we will explore how the &lt;em&gt;Arrays.asList()&lt;/em&gt; in Java works and provide examples to illustrate its usage. Whether you’re a beginner or a seasoned Java developer, understanding &lt;em&gt;Arrays.asList()&lt;/em&gt; in Java can make your coding more efficient and productive.&lt;/p&gt;

&lt;p&gt;So, let’s dive in!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Deep dive to learn about test automation, its uasage, types and also gain insights on how to get started with&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/learning-hub/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=learning_hub"&gt;&lt;strong&gt;&lt;em&gt;automation testing&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Arrays.asList() in Java?
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;Arrays.asList()&lt;/em&gt; in Java is a part of the &lt;em&gt;java.util.Arrays&lt;/em&gt; package and is used to create a list from an Array. This method returns a fixed-size list backed by the specified array, which means any changes made to the list will be reflected in the original array and vice versa.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Arrays.asList()&lt;/em&gt; in Java can be used in multiple ways where you can pass an array, elements, and objects as arguments to the method. Let’s first understand its syntax and usage with the help of simple code examples. Post that, we’ll discuss the real-time examples related to test automation using &lt;a href="https://www.lambdatest.com/blog/selenium-with-java/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;Selenium with Java&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G3xMR2KZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AuefRvYwUhGnpy713.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G3xMR2KZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AuefRvYwUhGnpy713.png" alt="image" width="648" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;public&lt;/em&gt; is an access modifier.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;static&lt;/em&gt; is a non-access modifier.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;List&lt;/em&gt; is the method object return type.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;asList&lt;/em&gt; is the method name.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;a&lt;/em&gt; is a method argument.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Arrays.asList() method examples
&lt;/h3&gt;

&lt;p&gt;There are three ways in which you can use the &lt;em&gt;Arrays.asList()&lt;/em&gt; in Java to implement parameterization by passing different argument types and then converting them into a &lt;em&gt;list&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Array as an argument
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;Arrays.asList()&lt;/em&gt; in Java can accept an array as an argument and convert it to a list. The array passed as an argument to the method must be of Wrapper Class type (Integer, Float, etc.) instead of primitive data type (int, float, etc.).&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.Arrays;
import java.util.List;

public class ArraysAsListWithArrayExample {

   public static void main (String args[]) {

       //Initializing a string Array

       String arr[] = {"Java", "Python", "C", "Ruby"};

       //Getting the list view of Array

       List &amp;lt;String&amp;gt; list = Arrays.asList(arr);

       //Printing the elements inside the list

       System.out.println(list);

   }

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M8Rlwceg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AlAPSWAzn5jmfcA4Y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M8Rlwceg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AlAPSWAzn5jmfcA4Y.png" alt="image" width="800" height="127"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, a &lt;em&gt;String&lt;/em&gt; type array has been passed as an argument to the &lt;em&gt;Arrays.asList()&lt;/em&gt; in Java for converting the array to a list of string elements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Learn why Python is the top choice for automation testing. This comprehensive tutorial provides a step-by-step guide to&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/blog/python-automation-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;&lt;strong&gt;&lt;em&gt;automation testing using Python&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;&lt;em&gt;to help you streamline your testing process.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Elements as an argument
&lt;/h3&gt;

&lt;p&gt;Instead of passing the array, you can pass multiple comma-separated elements as arguments to the &lt;em&gt;Arrays.asList()&lt;/em&gt; method.&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.Arrays;
import java.util.List;

public class ArraysAsListWithElementsExample {

   public static void main (String args[]) {

       //Getting the list view of String elements

       List &amp;lt;String&amp;gt; list = Arrays.asList("Java", "Python", "C", "Ruby");

       //Printing the elements inside the list

       System.out.println(list);

   }

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cvDhnGGa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AtJKRUf2pHICh6LiX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cvDhnGGa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AtJKRUf2pHICh6LiX.png" alt="image" width="800" height="121"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example shown above, multiple &lt;em&gt;String&lt;/em&gt; type elements are directly passed as arguments to the method for converting them as a list of string elements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Objects as an argument
&lt;/h3&gt;

&lt;p&gt;There could be certain scenarios where you must create a list of objects of a self-defined class and then perform some action on that list.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Arrays.asList()&lt;/em&gt; in Java allows you to pass class objects as arguments. It then creates a list of the objects you can use to perform specific actions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class ProgrammingLanguage{

   //Declaring the instance variables

   public String name;
   public double version;

   //Constructor to initialize the instance variables

   public ProgrammingLanguage(String name, double version) {

       this.name = name;
       this.version = version;

   }

   // Overriding toString method to return the String representation of the object

   public String toString() {

       return "(" + this.name + "," + this.version + ")";

   }

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

&lt;/div&gt;



&lt;p&gt;In the &lt;em&gt;ProgrammingLanguage&lt;/em&gt; class shown above, we are initializing the instance variables with the help of the constructor and overriding the &lt;em&gt;toString()&lt;/em&gt; method to represent the objects in string form.&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.Arrays;
import java.util.List;

public class ArraysAsListWithObjectsExample {

   public static void main (String args[]) {

       //Getting the list view of ProgrammingLanguage class objects

       List &amp;lt;ProgrammingLanguage&amp;gt; list = Arrays.asList(

               new ProgrammingLanguage("Java", 17.0),
               new ProgrammingLanguage("Python", 3.10),
               new ProgrammingLanguage("C", 17.0),
               new ProgrammingLanguage("Ruby", 3.2)

               );

       //Printing the objects inside the list

       System.out.println(list);

   }

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T62oEkOu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AwaSUujZqsuQFLsnn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T62oEkOu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AwaSUujZqsuQFLsnn.png" alt="image" width="800" height="123"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the class above, we are passing the objects of the &lt;em&gt;ProgrammingLanguage&lt;/em&gt; class to the &lt;em&gt;asList()&lt;/em&gt; method as arguments to get a list of these objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  Arrays.asList() method usage in test automation
&lt;/h3&gt;

&lt;p&gt;In this section of the &lt;a href="https://www.lambdatest.com/learning-hub/selenium-java"&gt;Selenium Java tutorial&lt;/a&gt;, we will discuss the real-time examples in which we can use the &lt;em&gt;Arrays.asList()&lt;/em&gt; in Java to parameterize our test cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;In this Appium tutorial, learn about Appium and its benefits for mobile automation testing. Take a look at how Appium works and see how to perform&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/appium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;&lt;strong&gt;&lt;em&gt;Appium Automation testing&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;&lt;em&gt;of your mobile applications.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling different driver instances
&lt;/h3&gt;

&lt;p&gt;The basic capability of a &lt;a href="https://www.lambdatest.com/learning-hub/ui-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=learning_hub"&gt;UI testing&lt;/a&gt; framework is to execute the test cases on different browsers or platforms. This can be easily achieved using the concept of parameterization in the &lt;a href="https://www.lambdatest.com/learning-hub/testng?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=learning_hub"&gt;TestNG&lt;/a&gt; framework.&lt;/p&gt;

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

&lt;p&gt;Feel free to choose any framework you are comfortable working with to implement the logic of &lt;a href="https://www.lambdatest.com/blog/handling-browser-tabs-with-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;handling multiple browsers&lt;/a&gt;. TestNG is one option, but if you are not familiar with it or prefer another framework, you can explore other alternatives.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;Arrays.asList()&lt;/em&gt; in Java can be used predominantly for this purpose. Assuming Selenium WebDriver as the &lt;a href="https://www.lambdatest.com/blog/automation-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;automation testing tool&lt;/a&gt; in this example, we will see how we can write a logic for handling multiple driver instances for different browsers using the &lt;em&gt;Arrays.asList()&lt;/em&gt; method.&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.Arrays;
import java.util.List;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class DriverManager {

      private RemoteWebDriver driver;

      public static void init_driver(String browser) throws MalformedURLException {

              String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME");
              String authkey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY");

              // Create a list of browsers
              List&amp;lt;String&amp;gt; browsers = Arrays.asList("chrome", "firefox", "edge");

                 // Loop through the list of browsers and create a driver instance for each one
                 for (String browserName : browsers) {

                     if (browser.equalsIgnoreCase("chrome")) {

                     //To launch the chrome browser on local      
                     /*driver = new ChromeDriver();*/

                     //Setting chrome capabilities
                     DesiredCapabilities ChromeCaps = new DesiredCapabilities();
                     ChromeCaps.setCapability("platform", "Windows 10");
                     ChromeCaps.setCapability("browserName", "chrome");
                     ChromeCaps.setCapability("version", "latest");
                     ChromeCaps.setCapability("build", "TestNG With Java");
                     ChromeCaps.setCapability("name", m.getName() + this.getClass().getName());
                     ChromeCaps.setCapability("plugin", "git-testng");

                      //To launch the chrome browser on LambdaTest cloud grid
                      String[] Tags = new String[]{"Feature", "Magicleap", "Severe"};
                      ChromeCaps.setCapability("tags", Tags);

                      driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), ChromeCaps);

                     } else if (browser.equalsIgnoreCase("firefox")) {

                     //To launch the firefox browser on local        
                     /*driver = new FirefoxDriver();*/

                     //Setting firefox capabilities
                     DesiredCapabilities FirefoxCaps = new DesiredCapabilities();
                     FirefoxCaps.setCapability("platform", "Windows 10");
                     FirefoxCaps.setCapability("browserName", "firefox");
                     FirefoxCaps.setCapability("version", "114.0");
                     FirefoxCaps.setCapability("build", "TestNG With Java");
                     FirefoxCaps.setCapability("name", m.getName() + this.getClass().getName());
                     FirefoxCaps.setCapability("plugin", "git-testng");

                      //To launch the firefox browser on LambdaTest cloud grid
                      String[] Tags = new String[]{"Feature", "Magicleap", "Severe"};
                      FirefoxCaps.setCapability("tags", Tags);

                      driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), FirefoxCaps);

                     } else if (browser.equalsIgnoreCase("edge")) {

                     //To launch the edge browser on local          
                     /*driver = new EdgeDriver();*/

                     //Setting edge capabilities
                     DesiredCapabilities EdgeCaps = new DesiredCapabilities();
                     EdgeCaps.setCapability("platform", "Windows 10");
                     EdgeCaps.setCapability("browserName", "edge");
                     EdgeCaps.setCapability("version", "112.0");
                     EdgeCaps.setCapability("build", "TestNG With Java");
                     EdgeCaps.setCapability("name", m.getName() + this.getClass().getName());
                     EdgeCaps.setCapability("plugin", "git-testng");

                      //To launch the edge browser on LambdaTest cloud grid
                      String[] Tags = new String[]{"Feature", "Magicleap", "Severe"};
                      EdgeCaps.setCapability("tags", Tags);

                      driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), EdgeCaps);

                     } else {

                         throw new IllegalArgumentException("Invalid browser name: " + browser);
                     }

              // Use the driver instance to navigate to a webpage and perform some actions
              driver.get("https://www.lambdatest.com");

              System.out.println(driver.getTitle());

              // Close the driver instance
              driver.quit();
          }
      }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h4G2gqrq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AwkirXZYNOziPvv42.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h4G2gqrq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AwkirXZYNOziPvv42.png" alt="image" width="250" height="86"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will see that a list of browser names has been initialized with the help of the &lt;em&gt;Arrays.asList()&lt;/em&gt; in Java. This list is then iterated through a &lt;em&gt;for-each&lt;/em&gt; loop to instantiate the &lt;em&gt;driver&lt;/em&gt; class of the respective browser.&lt;/p&gt;

&lt;p&gt;You can run the tests on LambdaTest, a unified digital experience testing platform that allows you to perform &lt;a href="https://www.lambdatest.com/java-automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;Java automation testing&lt;/a&gt; on an &lt;a href="https://www.lambdatest.com/selenium-grid-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;online Selenium Grid&lt;/a&gt; across 3000+ browsers and real devices. You can generate automation capabilities for your tests via the &lt;a href="https://www.lambdatest.com/capabilities-generator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;LambdaTest capabilities generator&lt;/a&gt;. The code snippets of the capabilities, which are automatically generated using the capabilities generator, have been included in the test code above for reference.&lt;/p&gt;

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

&lt;p&gt;Subscribe to our &lt;a href="https://www.youtube.com/c/LambdaTest?sub_confirmation=1?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=video"&gt;LambdaTest YouTube Channel&lt;/a&gt; for live updates with the latest tutorials around &lt;a href="https://www.lambdatest.com/selenium-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;Selenium testing&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/cypress-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;Cypress testing&lt;/a&gt;, and more.&lt;/p&gt;

&lt;h3&gt;
  
  
  Validating multiple URL redirects
&lt;/h3&gt;

&lt;p&gt;A very interesting scenario that I came across in one of my previous projects was to validate a security checklist that contained a list of URLs and their expected redirected URLs.&lt;/p&gt;

&lt;p&gt;This list was huge and contained many URLs. The best way to test this scenario was to parameterize a test method with these URLs as arguments and open all of them one by one in the browser to check the redirected URL.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Learn why Python is the top choice for automation testing. This comprehensive tutorial provides a step-by-step guide to&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/blog/python-automation-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;&lt;strong&gt;&lt;em&gt;automation testing with python&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;&lt;em&gt;which help you streamline your testing process.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Although I used an Excel sheet as a data source for this test scenario, here I will try to provide a simplified code example using the &lt;em&gt;Arrays.asList()&lt;/em&gt; method, which will take the URLs as arguments and print the redirected URLs on the console.&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.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ValidateURLRedirect {

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

  //Creating ChromeDriver instance

  WebDriver driver = new ChromeDriver();

  //Initializing a String array with URLs

  String URLs[] = {"https://app.lambdatest.com/", "https://accounts.lambdatest.com/detail/profile"};

  //Passing the String array to the asList method as an argument

  List &amp;lt;String&amp;gt; URLlist = Arrays.asList(URLs);

  //Iterating through the list and printing the redirected URLs

  for (String URL: URLlist) {
   driver.get(URL);
   System.out.println(driver.getCurrentUrl());
  }
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ftbhrbE2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A4Y2LgFmU7B3fWGtm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ftbhrbE2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A4Y2LgFmU7B3fWGtm.png" alt="image" width="800" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To gain access to these URLs, &lt;a href="https://accounts.lambdatest.com/register?_gl=1*8xb78a*_gcl_aw*R0NMLjE2ODQ4NDA4OTcuQ2p3S0NBandwYXlqQmhBbkVpd0EtN2VuYTk1clpJd2I2czNlWmt3UTdnQXdCTkJLYVJsMldnVUdFaE1BcDYySjU3cVJNRnkxNUltS0Job0NtQlVRQXZEX0J3RQ..*_gcl_au*MTk0MTU5MjM0NS4xNjgzNTI5ODg3"&gt;register for a free account&lt;/a&gt; and log in to your LambdaTest account.&lt;/p&gt;

&lt;p&gt;Post logging in, you can see that this code is simply converting the array of URLs into a list using the &lt;em&gt;Arrays.asList()&lt;/em&gt; method and then iterating the list using a &lt;em&gt;for-each&lt;/em&gt; loop to open all the URLs one by one in the browser. Once the URLs are loaded, the current page URL is extracted and printed on the console.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling multiple web elements
&lt;/h3&gt;

&lt;p&gt;There are certain scenarios in which you need to &lt;a href="https://www.lambdatest.com/blog/handling-webelements-in-selenium-python/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;handle web elements&lt;/a&gt; at once to validate some functionality. A good example of such a scenario is to validate that there are no broken links or images on a webpage.&lt;/p&gt;

&lt;p&gt;To build the logic for this scenario, you first need to collect all the links and then hit them one by one to check if the server is returning a 404 response code. The &lt;em&gt;Arrays.asList()&lt;/em&gt; in Java again comes in handy here and does the job easily.&lt;/p&gt;

&lt;p&gt;Here’s a code example for this.&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.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
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 ValidateLinksAndImagesExample {
public static void main(String args[]) throws IOException {

     //Creating ChromeDriver instance

WebDriver driver = new ChromeDriver();

      //Opening the test URL 

driver.get("https://the-internet.herokuapp.com/broken_images");
      //Initializing a list of img elements

       List&amp;lt;WebElement&amp;gt; allImages = driver.findElements(By.tagName("img"));

 //Creating a list to store the broken image URLs

       List&amp;lt;String&amp;gt; brokenImages = new ArrayList&amp;lt;String&amp;gt;();

//Iterating through the list of elements and hitting the img src URLs

       for (WebElement link : allImages) {
           String src = link.getAttribute("src");
           System.out.println("Testing image source "+src);
           HttpURLConnection connection = (HttpURLConnection) new URL(src).openConnection();
           connection.setRequestMethod("HEAD");
           int responseCode = connection.getResponseCode();
           if (responseCode &amp;gt;= 400) {

   //Adding all the broken image URLs to the list

               brokenImages.add(src);
           }
       }

       System.out.println("Broken images:");

 //Printing the broken img URLs list

       System.out.println(Arrays.asList(brokenImages));

   }

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1ssNv-4d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AFYt5dSsZlYD9dmJl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1ssNv-4d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AFYt5dSsZlYD9dmJl.png" alt="image" width="800" height="182"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see in this code example, all &lt;em&gt;img&lt;/em&gt; elements present on the test URL are first collected into a list, and then their src URLs are extracted and hit one by one to assert the response code. All the URLs which returned 404 as a response code are added to another list, and then this list is printed on the console. There are currently two broken images available on this test URL, and the &lt;em&gt;src&lt;/em&gt; URLs of both are printed in the console.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus: Arrays.asList vs. new ArrayList(Arrays.asList())
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Arrays.asList()&lt;/em&gt; and &lt;em&gt;new ArrayList&amp;lt;&amp;gt;(Arrays.asList())&lt;/em&gt; are two different ways of creating a List from an array in Java. Let’s discuss them in detail.&lt;/p&gt;

&lt;h3&gt;
  
  
  Arrays.asList()
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Arrays.asList()&lt;/em&gt; in Java creates a list backed by the original array. This means that any changes made to the list will be reflected in the original array and vice versa. The resulting list is fixed-size and cannot be resized. If you need a mutable list, you need to create a new list based on the original list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mutable&lt;/strong&gt; in Java refers to those objects whose state can be changed after they are created. In this case, a mutable list is a list that can be resized by adding or removing the elements. On the other hand, an immutable list is a list in which no structural changes can be made.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Learn why Python is the top choice for&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/blog/python-automation-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;&lt;strong&gt;&lt;em&gt;automation testing in python&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;. This comprehensive tutorial provides a step-by-step guide to Python automation testing to help you streamline your testing process.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Below is the code example to demonstrate that any changes made to the list will also be reflected in the original array.&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.Arrays;
import java.util.List;

public class ModifyElementsExample {

   public static void main (String args[]) {

       //Initializing a string Array

       String [] arr = {"Java", "Python", "C", "Ruby"};

       //Getting the list view of Array

       List &amp;lt;String&amp;gt; list = Arrays.asList(arr);

       //Modifying the value of thatist

       list.set(3, "C++");

       //Printing the original Array

       System.out.println(Arrays.toString(arr));

   }

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Pj9EdnYj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AZe2QLW_t4gNZT0o_.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Pj9EdnYj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AZe2QLW_t4gNZT0o_.png" alt="image" width="628" height="128"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here you can see that the value of the third index of the list has been changed to “&lt;em&gt;C++&lt;/em&gt;” from “&lt;em&gt;Ruby&lt;/em&gt;” and when the original array was printed on the console, it also reflected the change.&lt;/p&gt;

&lt;p&gt;The second example shown below is to prove that the list created using the &lt;em&gt;Arrays.asList()&lt;/em&gt; in Java is immutable.&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.Arrays;
import java.util.List;

public class ModifyListExample {

   public static void main (String args[]) {

       //Initializing a string Array

       String [] arr = {"Java", "Python", "C", "Ruby"};

       //Getting the list view of Array

       List &amp;lt;String&amp;gt; list = Arrays.asList(arr);

       //Modifying the structure of the list

       list.add("C++");

       //Printing the List

       System.out.println(list);

   }

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

&lt;/div&gt;



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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t6By9yjJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A74YcK4jFZvdE937I.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t6By9yjJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A74YcK4jFZvdE937I.png" alt="image" width="800" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here you can see that the program had thrown an &lt;em&gt;UnsupportedOperationException&lt;/em&gt; when an attempt at the structural modification of the list was made using the &lt;em&gt;add()&lt;/em&gt; method.&lt;/p&gt;

&lt;h3&gt;
  
  
  new ArrayList(Arrays.asList())
&lt;/h3&gt;

&lt;p&gt;On the other hand, &lt;em&gt;new ArrayList&amp;lt;&amp;gt;(Arrays.asList())&lt;/em&gt; creates a new &lt;em&gt;ArrayList&lt;/em&gt; based on the elements of the array. This means any changes to the new list will not affect the original array and vice versa. The resulting list is mutable and can be resized.&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.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ModifyNewListExample {

   public static void main (String args[]) {

       //Initializing a string Array

       String [] arr = {"Java", "Python", "C", "Ruby"};

       //Getting the list view of Array

       List &amp;lt;String&amp;gt; list = new ArrayList&amp;lt;String&amp;gt;(Arrays.asList(arr));

       //Modifying the list

       list.add("C++");

       //Printing the original Array

       System.out.println(list);

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

&lt;/div&gt;



&lt;p&gt;In the example above, the list has been modified successfully.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ao5LDGLU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Apqx5O_qY-CAp4CTH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ao5LDGLU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Apqx5O_qY-CAp4CTH.png" alt="image" width="798" height="104"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Learn why Python is the top choice for automation testing . This comprehensive tutorial provides a step-by-step guide to&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/blog/python-automation-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_04&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;&lt;strong&gt;&lt;em&gt;automation testing in python&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;&lt;em&gt;to help you streamline your testing process.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;Arrays.asList()&lt;/em&gt; in Java works as a bridge between the &lt;em&gt;array&lt;/em&gt; and the &lt;em&gt;collection&lt;/em&gt; interface. It converts an array into a fixed-size list, and any changes made to the values of the list elements will be reflected in the original array and vice versa.&lt;/p&gt;

&lt;p&gt;This is a convenient method to implement parameterization in your tests, especially when you are not using other supporting libraries like TestNG in your framework.&lt;/p&gt;

&lt;p&gt;Throughout this blog, we explored the syntax and usage of the &lt;em&gt;Arrays.asList()&lt;/em&gt; in Java and provided examples to demonstrate its functionality.&lt;/p&gt;

</description>
      <category>java</category>
      <category>automationtesting</category>
      <category>softwaretesting</category>
    </item>
    <item>
      <title>XPath vs CSS Selectors: A Detailed Guide</title>
      <dc:creator>ArunGupta651</dc:creator>
      <pubDate>Thu, 03 Aug 2023 08:39:53 +0000</pubDate>
      <link>https://dev.to/arungupta651/xpath-vs-css-selectors-a-detailed-guide-4d1l</link>
      <guid>https://dev.to/arungupta651/xpath-vs-css-selectors-a-detailed-guide-4d1l</guid>
      <description>&lt;p&gt;Writing the locators for the web elements is like laying down the foundation of an automation testing framework. This foundation must be strong enough to hold the growing size of a framework.Otherwise, the result will be a flaky and unreliable system that will tend to fail by even the smallest changes in the &lt;a href="https://www.lambdatest.com/blog/document-object-model/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;Document Object Model&lt;/a&gt; (DOM) structure.&lt;/p&gt;

&lt;p&gt;Though there are many locators available in Selenium, there has been a lot of debate when it comes to choosing between the XPath and the CSS Selectors. I have worked on many projects throughout my career as a Quality Engineer, and I have also seen this XPath vs CSS Selectors discussion happening across the teams. However, there is no clear winner.&lt;/p&gt;

&lt;p&gt;I believe locating the elements by ID is the most reliable and effective method. However, I have seen a lot of projects where the use of ID attributes in the HTML elements is minimal. Because of this, other options like XPath and CSS Selectors need to be explored for writing the locator expressions.&lt;/p&gt;

&lt;p&gt;In this blog on XPath vs CSS Selectors, I will broadly talk about the best practices to follow when working with the XPath and CSS Selectors but will briefly explain the other locators as well with examples using Selenium Java.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Run your Playwright test scripts instantly on 50+ browser and OS combinations using the LambdaTest cloud. Read more about&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/playwright?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;&lt;strong&gt;&lt;em&gt;playwright browser testing&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;&lt;em&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a locator in Selenium?
&lt;/h3&gt;

&lt;p&gt;While testing the functional aspects at the front-end of a website, you need to perform many operations on a web page. These operations could be clicking a button, checking a checkbox, or entering text into a textbox. You can automate these operations using &lt;a href="https://www.lambdatest.com/learning-hub/webdriver?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=learning_hub"&gt;Selenium WebDriver&lt;/a&gt; by locating the web elements in the HTML DOM.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qK0kRZWU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AXq3tyHSC0J9YtZQb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qK0kRZWU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AXq3tyHSC0J9YtZQb.png" alt="image" width="800" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As the name suggests, a &lt;a href="https://www.lambdatest.com/learning-hub/selenium-locators?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=learning_hub"&gt;locator in Selenium&lt;/a&gt; lets you find the respective WebElement. It can be defined as an address that uniquely identifies an element on a web page. Once the element is located, you can use other supporting methods to perform specific operations on the element, like clicking or entering a text.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;When writing this XPath vs CSS Selectors blog, the Selenium version was Selenium 4.8.0. However, the way the locators are being used is agnostic of the test automation framework (e.g., Selenium, Cypress, etc.) being used for test automation.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Different locators in Selenium
&lt;/h3&gt;

&lt;p&gt;Selenium supports eight types of locators, including XPath and CSS Selectors. I will explain each locator in detail, along with examples. Here, I will use &lt;a href="https://www.lambdatest.com/selenium-playground?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;LambdaTest Selenium Playground&lt;/a&gt; as the AUT (Application Under Test).&lt;/p&gt;

&lt;p&gt;LambdaTest is a cloud-based digital experience testing platform that supports manual and &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;automated testing&lt;/a&gt; for web and mobile. It provides an online &lt;a href="https://www.lambdatest.com/blog/selenium-grid-setup-tutorial/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;Selenium Grid&lt;/a&gt; of 3000+ real browsers and OS and eliminates the hassle you face while performing automation testing on your local grid.&lt;/p&gt;

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

&lt;p&gt;Subscribe to the &lt;a href="https://www.youtube.com/c/LambdaTest?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=video"&gt;LambdaTest YouTube Channel&lt;/a&gt; and get the latest tutorials around &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devtodevto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;automation testing&lt;/a&gt;, Selenium testing, &lt;a href="https://www.lambdatest.com/playwright?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;Playwright&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/appium-mobile-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;Appium&lt;/a&gt;, and more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;It’s crucial to debug websites for Safari before pushing them live. In this article, we look at how to debug websites using&lt;/em&gt;&lt;/strong&gt; &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=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;&lt;strong&gt;&lt;em&gt;dev tools for Safari&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;&lt;em&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As this blog on XPath vs CSS Selectors specifically focuses on XPath and CSS Selectors locators, I have intentionally omitted them from this list. They will be thoroughly covered in later sections of the blog on XPath vs CSS Selectors.&lt;/p&gt;

&lt;p&gt;To jump directly to the section on XPath and CSS Selectors, click &lt;a href="https://docs.google.com/document/d/1MG3u8oO4Miy6BVPZ7hGGgPpMY_SqaOtNrIR7ujwq_CE/edit#heading=h.wljil3lncsc"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  ID locator
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/making-the-move-with-id-locator-in-selenium-webdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;ID locator&lt;/a&gt; is used to find the element based on its ID attribute. When an element has an ID available, it is the most desirable locator because IDs are unique for all elements. This locator directly finds the element with a simple, non-complex locator expression.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2FVse3aE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AjvaDVAG5rt6JU6hQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2FVse3aE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AjvaDVAG5rt6JU6hQ.png" alt="image" width="800" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this XPath vs CSS Selectors blog, you can see that we are trying to locate the highlighted textbox. We can do so by inspecting this element in the browser console and checking what attributes this element has.&lt;/p&gt;

&lt;p&gt;This is an input element, and we can see that it has the &lt;em&gt;ID&lt;/em&gt; attribute with value = “&lt;em&gt;user-message&lt;/em&gt;”. In this case, we can use a simple locator expression with driver object reference as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--za_8okZO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AQ4fLDQN3qrm2nmXv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--za_8okZO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AQ4fLDQN3qrm2nmXv.png" alt="image" width="512" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Name locator
&lt;/h3&gt;

&lt;p&gt;&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=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;Name locator&lt;/a&gt; is used to find the element based on its &lt;em&gt;name&lt;/em&gt; attribute. While it is possible to create a basic locator expression using an element’s &lt;em&gt;name&lt;/em&gt; attribute, it’s also possible that the element may not have a name attribute at all.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6x_KIYYM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AtGD1e4OJYbUkLKtc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6x_KIYYM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AtGD1e4OJYbUkLKtc.png" alt="image" width="800" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example of this XPath vs CSS Selectors blog, the textbox has the value of the &lt;em&gt;name&lt;/em&gt; attribute as “&lt;em&gt;password&lt;/em&gt;”. Hence, the locator expression can be written as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z52G_qHB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AQkAioL3fI_f3bzv-.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z52G_qHB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AQkAioL3fI_f3bzv-.png" alt="image" width="800" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  className locator
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/selenium-java-tutorial-class-name-locator-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;className locator&lt;/a&gt; is used to find the element based on its &lt;em&gt;class&lt;/em&gt; attribute. It is also similar to the &lt;em&gt;ID&lt;/em&gt; and &lt;em&gt;name&lt;/em&gt; locators we discussed above.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dxYeFmg0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2ATXsJvxj0_L19-uIS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dxYeFmg0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2ATXsJvxj0_L19-uIS.png" alt="image" width="800" height="245"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example of this XPath vs CSS Selectors blog, the checkbox element can be located using the &lt;em&gt;class&lt;/em&gt; attribute as:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;It’s crucial to debug websites for Safari before pushing them live. In this article, we look at how to debug websites using&lt;/em&gt;&lt;/strong&gt; &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=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;&lt;strong&gt;&lt;em&gt;dev tools in Safari&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  tagName locator
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/locating-elements-by-tagname-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;tagName locator&lt;/a&gt; is used to find the element directly using its tag name. However, this is not a preferred locator to identify the elements as it tends to return duplicate results because there can be multiple elements of a particular type on the HTML page structure. For example, &lt;em&gt;input, button, div&lt;/em&gt;, etc.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--D2-Yd2Vb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Aq2IoY-KulqseZGIG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--D2-Yd2Vb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Aq2IoY-KulqseZGIG.png" alt="image" width="800" height="245"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example of this XPath vs CSS Selectors blog, an &lt;em&gt;input&lt;/em&gt; type element can be located using the tagName attribute. The locator expression can be written as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TDty7Nj3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2ArLsmq-yOOg1iBv3R.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TDty7Nj3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2ArLsmq-yOOg1iBv3R.png" alt="image" width="800" height="343"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As mentioned above, this expression will identify all the input elements available on the web page.&lt;/p&gt;

&lt;h3&gt;
  
  
  linkText locator
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/using-link-text-and-partial-link-text-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;linkText locator&lt;/a&gt; is used to find an anchor element based on the hyperlink’s text. You don’t need to work with any attribute of the anchor tag. The element can be easily located using the visible text of the hyperlink.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ODqfZpFX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Ayn3v1oRwHqSR1LVW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ODqfZpFX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Ayn3v1oRwHqSR1LVW.png" alt="image" width="800" height="417"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example of this XPath vs CSS Selectors blog, the first menu link of the &lt;a href="https://www.lambdatest.com/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;LambdaTest&lt;/a&gt; website can be located by the visible link text “&lt;em&gt;Platform&lt;/em&gt;”. The resulting locator expression will look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hN6vGp3Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A0_kdgDUMFukSRpUq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hN6vGp3Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A0_kdgDUMFukSRpUq.png" alt="image" width="800" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  partialLinkText Locator
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/using-link-text-and-partial-link-text-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;partialLinkText locator&lt;/a&gt; is also used to find an anchor element based on the text of the anchor link. It differs from linkText in that it can match the partial text of the hyperlink.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Inspect web elements to help developers and testers to debug UI flaws or make modifications in HTML or CSS files. Learn&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/software-testing-questions/how-to-inspect-on-macbook?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;&lt;strong&gt;&lt;em&gt;how to inspect on MacBook&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bjA6y5dU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A4_6qRvuLBAIPixg0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bjA6y5dU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A4_6qRvuLBAIPixg0.png" alt="image" width="800" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example of this XPath vs CSS Selectors blog, the hyperlink text is “&lt;em&gt;Cross Browser Testing&lt;/em&gt;”. We can locate this anchor element using the partial text of the hyperlink like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HarZzpkQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AixpWOgv7T4m_2hOP.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HarZzpkQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AixpWOgv7T4m_2hOP.png" alt="image" width="800" height="302"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is XPath?
&lt;/h3&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=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;XPath&lt;/a&gt;, or XML Path, is a syntax expression language for finding the elements on a web page. XPath can be used to find the elements in both XML and HTML documents.&lt;/p&gt;

&lt;p&gt;XPath is used extensively with popular &lt;a href="https://www.lambdatest.com/blog/automation-testing-frameworks/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;automation testing frameworks&lt;/a&gt; like &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;Selenium&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/learning-hub/cypress-tutorial?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=learning_hub"&gt;Cypress&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/playwright?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;Playwright&lt;/a&gt;, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An XPath expression can traverse through the first node of the XML/HTML DOM structure to the desired element. The standard syntax of an XPath expression contains the current node selector and the element’s tag name with its attribute and the attribute’s value.&lt;/p&gt;

&lt;p&gt;The XPath expression looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W2w20cAT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AZk86s9ImuHhWMtr7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W2w20cAT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AZk86s9ImuHhWMtr7.png" alt="image" width="690" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the pictorial representation of the XPath expression.&lt;/p&gt;

&lt;h3&gt;
  
  
  Types of XPath
&lt;/h3&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Absolute XPath&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Relative XPath&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Absolute XPath
&lt;/h4&gt;

&lt;p&gt;Absolute XPath is used to find the element by traversing through the entire DOM structure beginning from the starting node ( &lt;code&gt;&amp;lt; html &amp;gt;&lt;/code&gt; tag) of the HTML page to the element to be located on the page. The main characteristic of the absolute XPath is that it begins with a single forward slash (/).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Inspect web elements to help developers and testers to debug UI flaws or make modifications in HTML or CSS files. Learn&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/software-testing-questions/how-to-inspect-on-macbook?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;&lt;strong&gt;&lt;em&gt;how to inspect element on MacBook&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Shown below is an example of the absolute XPath of the &lt;em&gt;Name&lt;/em&gt; textbox as shown in the image below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W3_PeIh4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AUY15tciqUMnTU-Ky.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W3_PeIh4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AUY15tciqUMnTU-Ky.png" alt="image" width="800" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VAJcfciR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2ASlWBRg1AF8aIHvcw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VAJcfciR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2ASlWBRg1AF8aIHvcw.png" alt="image" width="800" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Absolute XPath is not a preferred way to locate the elements as it’s fragile and tends to break if there is any change in the DOM structure.&lt;/p&gt;

&lt;p&gt;Let’s see a demonstration here to prove the fragility of the absolute XPath expression. In the above example of this XPath vs CSS Selectors blog, the DOM structure has been edited.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Apx87o0t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AG7z2WewY2MMql9e2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Apx87o0t--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AG7z2WewY2MMql9e2.png" alt="image" width="800" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The same absolute XPath expression can now not locate the element as one &lt;em&gt;div&lt;/em&gt; element between the starting node and the element is removed.&lt;/p&gt;

&lt;h4&gt;
  
  
  Relative XPath
&lt;/h4&gt;

&lt;p&gt;Relative XPath does not require the starting node and can locate an element from the middle of the DOM structure. It begins with the double forward slash (//) and is the preferred XPath expression to locate the elements, as the expressions are short and robust.&lt;/p&gt;

&lt;p&gt;Here, the relative XPath to locate the &lt;em&gt;Name&lt;/em&gt; textbox will be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SNgiDl6G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A0LSiNruTcdWaYgJB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SNgiDl6G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A0LSiNruTcdWaYgJB.png" alt="image" width="546" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x7b705pG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2ARSimgfkSaPH408rS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x7b705pG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2ARSimgfkSaPH408rS.png" alt="image" width="800" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see in the above example of this XPath vs CSS Selectors blog, the relative XPath expression is still locating the element even when several nodes between the starting node and the element have been removed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Need a great solution for cross browser testing on Safari? Forget about emulators or simulators — use real online browsers. Try LambdaTest to test on&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/software-testing-questions/how-to-inspect-on-macbook?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=webpage"&gt;&lt;strong&gt;&lt;em&gt;safari browser online.&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Writing the XPath expressions
&lt;/h3&gt;

&lt;p&gt;When working on web projects of significant size, there will likely be situations where using basic XPath expressions to locate an element is not ideal. It is so because it’s possible that the element might not have any attribute which could uniquely identify it.&lt;/p&gt;

&lt;p&gt;Also, there could be cases where the value of the only attribute of the element keeps changing with the page refresh. Then the question is: How would you locate it?&lt;/p&gt;

&lt;p&gt;Well, XPath provides flexibility in the form of methods to locate such dynamic elements. Let’s dive deep into it.&lt;/p&gt;

&lt;h3&gt;
  
  
  XPath Contains() Method
&lt;/h3&gt;

&lt;p&gt;Contains() method can be used in the XPath expression to locate an element whose attribute value keeps changing with the page refresh.&lt;/p&gt;

&lt;p&gt;This method works on the principle of finding the partial text. It is useful if you know that some part of the dynamic attribute value remains constant. It can also be used when the attribute value is lengthy and contains multiple words (e.g., a placeholder text value).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here, in this example, the &lt;em&gt;button&lt;/em&gt; type element is located using the &lt;em&gt;class&lt;/em&gt; attribute. But there are multiple classes defined for this button. Therefore, it cannot be located directly using the &lt;em&gt;className&lt;/em&gt; locator.&lt;/p&gt;

&lt;p&gt;To find this element using the &lt;em&gt;class&lt;/em&gt; attribute, I have used the &lt;em&gt;contains()&lt;/em&gt; method, and the XPath expression looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r8yXm_nn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AdSA37uGPsRm8Pw0o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r8yXm_nn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AdSA37uGPsRm8Pw0o.png" alt="image" width="800" height="238"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;XPath Starts-with() Method&lt;/p&gt;

&lt;p&gt;Starts-with() method also works on the same principle of finding the partial text. It is different from the contains() method in that it tends to find the static text at the beginning of the attribute value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Wrou-_Bp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Auq2BgtKOUAwfnU8P.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Wrou-_Bp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Auq2BgtKOUAwfnU8P.png" alt="image" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have used the same example of the button element to demonstrate this method as well. To locate this element using the class attribute, I have used the starts-with() method in this XPath vs CSS Selectors blog, and the XPath expression looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JzqDx8WE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AxBdkNcVXhqhmYVBz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JzqDx8WE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AxBdkNcVXhqhmYVBz.png" alt="image" width="800" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see in the above example of this XPath vs CSS Selectors blog, the class attribute value starts with the class name “mt-20”. Therefore, this expression was able to locate this element successfully.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;This blog discusses the most exciting features of 13 best&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/blog/best-test-automation-frameworks-2021/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;&lt;strong&gt;&lt;em&gt;automation testing frameworks&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;&lt;em&gt;in 2021.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  XPath Text() Method
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;text()&lt;/em&gt; method is used to locate the element based on its text value. This method works similarly to the &lt;em&gt;linkText&lt;/em&gt; locator. However, this method works on any element which has the text value like a &lt;code&gt;&amp;lt; span &amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt; p &amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt; button &amp;gt;&lt;/code&gt;, or an &lt;code&gt;&amp;lt; div &amp;gt;&lt;/code&gt;, whereas the linkText only works with the &lt;code&gt;&amp;lt; a &amp;gt;&lt;/code&gt; element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zEFakzOY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AKPEkFRudzj5lYnF-.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zEFakzOY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AKPEkFRudzj5lYnF-.png" alt="image" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above example of this XPath vs CSS Selectors blog, the &lt;em&gt;button&lt;/em&gt; element is located using its text with the help of the &lt;em&gt;text()&lt;/em&gt; method. The simple XPath expression for this case can be written as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qMhN_zBZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A-fY1h9VgG1pCZ8s3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qMhN_zBZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A-fY1h9VgG1pCZ8s3.png" alt="image" width="800" height="235"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  XPath Logical operators
&lt;/h3&gt;

&lt;p&gt;While using the logical locators (&lt;em&gt;and, or)&lt;/em&gt;, multiple attribute values of an element are compared. With “&lt;em&gt;or&lt;/em&gt;” operator, the element will be found if any of the compared values are true. With “&lt;em&gt;and&lt;/em&gt;” operator, both the values need to be true to find the element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BNccyyTM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AlU1HnUCA9qHKEOTQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BNccyyTM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AlU1HnUCA9qHKEOTQ.png" alt="image" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here in this example, I have used both &lt;em&gt;ID&lt;/em&gt; and &lt;em&gt;class&lt;/em&gt; attributes of the &lt;em&gt;button&lt;/em&gt; element with the ‘&lt;em&gt;and&lt;/em&gt;’ operator. As you can see, the ID can be uniquely identified, but multiple classes are defined.&lt;/p&gt;

&lt;p&gt;Therefore, &lt;em&gt;contains()&lt;/em&gt; method is required for the &lt;em&gt;class&lt;/em&gt; attribute to write the following correct expression in this case:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Improve your software testing process with our&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/blog/automation-testing-tutorial/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;&lt;strong&gt;&lt;em&gt;automation testing tutorial&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;. Discover the benefits of automation testing, learn about the best tools and techniques, and get expert insights on optimizing your testing strategy.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Similarly, the expression can be written using the &lt;em&gt;or&lt;/em&gt; operator as well like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ap6Cg81N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AsotMyKN2B6XST0w8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ap6Cg81N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AsotMyKN2B6XST0w8.png" alt="image" width="800" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please note that both the logical operators are case-sensitive and need to be specified in lowercase within the expression.&lt;/p&gt;

&lt;h3&gt;
  
  
  XPath Axes Methods
&lt;/h3&gt;

&lt;p&gt;Like the methods explained in the above section of this XPath vs CSS Selectors blog, XPath provides additional methods to locate the dynamic elements. These methods are called axes methods. XPath axes methods can traverse the DOM bidirectionally and are extremely useful in locating the elements that don’t have any attributes.&lt;/p&gt;

&lt;p&gt;There are many axes methods, but we will discuss the most common ones here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Following&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Preceding&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ancestor&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Parent&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Child&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Following
&lt;/h4&gt;

&lt;p&gt;It selects all elements in the DOM after the closing tag of the current node.&lt;/p&gt;

&lt;p&gt;The syntax of the Following method is &lt;em&gt;following::tag name&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This can be better explained with the help of the example below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bivQL3B4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Ak0nNLYwvg-PcFU5x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bivQL3B4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Ak0nNLYwvg-PcFU5x.png" alt="image" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the following keyword finds all the &lt;em&gt;input&lt;/em&gt; elements, which are after the “&lt;em&gt;Name&lt;/em&gt;” textbox e.g., &lt;em&gt;“Email”, “Password”, “Company,”&lt;/em&gt; and “&lt;em&gt;Website&lt;/em&gt;” textboxes. The XPath expression for this example is:&lt;/p&gt;

&lt;p&gt;As mentioned above, this expression locates all the elements which appear after the current node. If you want to locate the first input element here in this example, you can use the index like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mVZ4ycSE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Asc-pHBR-0ZascFzl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mVZ4ycSE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Asc-pHBR-0ZascFzl.png" alt="image" width="800" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, you can use any attribute of the required element to avoid using the indexes as the best practice. Here, I have demonstrated another XPath expression using the &lt;em&gt;ID&lt;/em&gt; attribute of the second &lt;em&gt;input&lt;/em&gt; element i.e., the “&lt;em&gt;Email&lt;/em&gt;” textbox.&lt;/p&gt;

&lt;h4&gt;
  
  
  Preceding
&lt;/h4&gt;

&lt;p&gt;It selects all elements in the DOM, which come before the opening tag of the current node.&lt;/p&gt;

&lt;p&gt;The syntax of the Preceding method is &lt;em&gt;preceding::tag name&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This can be better explained with the help of the example below.&lt;/p&gt;

&lt;p&gt;Here, the &lt;em&gt;preceding&lt;/em&gt; keyword finds all the input elements, which are before the “Company” textbox e.g. “&lt;em&gt;Name&lt;/em&gt;”, “&lt;em&gt;Email&lt;/em&gt;” and “&lt;em&gt;Password&lt;/em&gt;” textboxes. The XPath expression for this example is:&lt;/p&gt;

&lt;p&gt;Just like the example above, you can use the index or any attribute value if you want to locate a specific element from all the elements found.&lt;/p&gt;

&lt;h4&gt;
  
  
  Ancestor
&lt;/h4&gt;

&lt;p&gt;It selects all ancestor elements (parent, grandparent, etc.) of the current node.&lt;/p&gt;

&lt;p&gt;The syntax of the Ancestor method is &lt;em&gt;ancestor::tag name&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This can be better explained with the help of the example below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0Hf1NfUY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AD2ZCCDmWpvyIDa5w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0Hf1NfUY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AD2ZCCDmWpvyIDa5w.png" alt="image" width="800" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the &lt;em&gt;ancestor&lt;/em&gt; keyword finds all the &lt;em&gt;div&lt;/em&gt; elements which are ancestors of the current node i.e. the “&lt;em&gt;Company&lt;/em&gt;” textbox. The XPath expression for this example is:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5UjGoaE0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AFoQtg-7c9FNHGmX1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5UjGoaE0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AFoQtg-7c9FNHGmX1.png" alt="image" width="800" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again, here this expression found 8 &lt;em&gt;div&lt;/em&gt; elements which are the ancestors (i.e., parent, grandparent) of the current node. If you want to select a particular element, you can use the index or the relevant attribute value.&lt;/p&gt;

&lt;h4&gt;
  
  
  Parent
&lt;/h4&gt;

&lt;p&gt;It selects the parent element of the current node.&lt;/p&gt;

&lt;p&gt;The syntax of the Parent method is &lt;em&gt;parent::tag name&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This can be better explained with the help of the example below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y6J3O6eL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AWfMtQgcri_mD2ZPV.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y6J3O6eL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AWfMtQgcri_mD2ZPV.png" alt="image" width="800" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the &lt;em&gt;parent&lt;/em&gt; keyword finds the parent &lt;em&gt;div&lt;/em&gt; element, the parent of the current node, i.e., the “&lt;em&gt;Company&lt;/em&gt;” textbox. The XPath expression for this example is:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uJc1c1ki--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AIQM6HLPTt2sLz7b3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uJc1c1ki--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AIQM6HLPTt2sLz7b3.png" alt="image" width="800" height="238"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Child
&lt;/h4&gt;

&lt;p&gt;It selects all children elements of the current node.&lt;/p&gt;

&lt;p&gt;The syntax of the Child method is &lt;em&gt;child::tag name&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This can be better explained with the help of the example below.&lt;/p&gt;

&lt;p&gt;Here, the &lt;em&gt;child&lt;/em&gt; keyword selects all the &lt;em&gt;input&lt;/em&gt; elements which are children of the current &lt;em&gt;div&lt;/em&gt; element. The XPath expression for this example is:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Urdubt59--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Aj3XJNp9YS7SHcFqI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Urdubt59--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Aj3XJNp9YS7SHcFqI.png" alt="image" width="800" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages of using XPath
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;XPath can locate an element at any level of the DOM structure and don’t need to traverse from the beginning of the root node to the desired element&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;XPath expressions are bidirectional and can locate the element from top to bottom and from bottom to top (i.e., from parent to the child node and from child to parent node)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;XPath works with both XML and HTML documents and is supported by multiple programming languages&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;XPath expressions support multiple methods to find the elements which are difficult to locate due to dynamic attribute values or limited attributes. For example, if you need to locate an element that does not have any unique attribute, then you can use XPath axes methods to establish the parent-child relationship with another unique element. In this way, the required elements can be easily located.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Discover the 17 key&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/blog/17-key-benefits-of-automation-testing-for-a-successful-release/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;&lt;strong&gt;&lt;em&gt;benefits of automation testing&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;, which will help you decide whether this is the right call for your SDLC. Find out more.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Shortcomings of using XPath
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;XPath expressions can become difficult to write or understand when it is used to locate elements with dynamic attribute values.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;XPath tends to break if there are changes in the page structure. Though using relative XPath essentially solves this problem, there are still chances that changes in the page structure may cause the XPath to fail.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;XPath does not support the elements having multi-valued attributes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What are CSS Selectors?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/how-pro-testers-use-css-selectors-in-selenium-automation-scripts/"&gt;CSS (Cascading Style Sheets) Selector&lt;/a&gt; is a locator that uses character sequence patterns to identify the elements based on the HTML structure.&lt;/p&gt;

&lt;p&gt;For starters, the syntax of the CSS Selector might seem a bit more complex than other locators like ID, Name, className, etc. However, it is a super-effective and preferred expression to locate elements with dynamic attribute values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;CSS Selector syntax is quite similar to the syntax of XPath, as discussed above. It can be represented as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EttJ5jKG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AkVNHux-he9WF1LF0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EttJ5jKG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AkVNHux-he9WF1LF0.png" alt="image" width="648" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is a pictorial representation of the CSS syntax:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3O07p2ru--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AkUwJuuQqNt5h48Kj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3O07p2ru--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AkUwJuuQqNt5h48Kj.png" alt="image" width="800" height="430"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Writing the CSS Selector expressions
&lt;/h3&gt;

&lt;p&gt;A CSS Selector expression can be written with multiple attributes of an element. The most common methods of writing a CSS Selector expression are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;CSS selector with &lt;em&gt;ID&lt;/em&gt; attribute&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CSS selector with &lt;em&gt;class&lt;/em&gt; attribute&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CSS selector with other attributes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CSS selector by combining different attributes&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  CSS Selector with ID
&lt;/h3&gt;

&lt;p&gt;The ID attribute of an element can be used to write the CSS Selector expression. I will again take an example from the &lt;a href="https://ecommerce-playground.lambdatest.io/"&gt;LambdaTest Selenium Playground&lt;/a&gt; website to locate an element using its ID attribute.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dT_dA4A9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A4hItYbSE7Yl9kuDd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dT_dA4A9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A4hItYbSE7Yl9kuDd.png" alt="image" width="800" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, you can see that the value of the ID attribute of the input element is “&lt;em&gt;user-message&lt;/em&gt;”. Hence, the resulting CSS Selector expression according to the syntax will be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m_YPHsgc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Agvw_s3Q_mocKkCnT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m_YPHsgc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Agvw_s3Q_mocKkCnT.png" alt="image" width="800" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This expression can be further simplified by using a shortcut character for the ID attribute, and this character is ‘#’. So here, the simplified CSS Selector expression will be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--308cMw1c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2ARVx-9JQk4HsRQDVn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--308cMw1c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2ARVx-9JQk4HsRQDVn.png" alt="image" width="800" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, I have just written the element tag name followed by the ‘#’ character and the value of the ID attribute.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;This article will walk you through the best&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/blog/best-software-testing-certifications/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;&lt;strong&gt;&lt;em&gt;Automation testing certifications&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;&lt;em&gt;that will help boost your career as a test automation engineer.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  CSS Selector with class
&lt;/h3&gt;

&lt;p&gt;Like the ID attribute, a class attribute can also be used to write a CSS Selector expression.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CZ3hFRu_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A2W2Lh6n5NTtkZ3hv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CZ3hFRu_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A2W2Lh6n5NTtkZ3hv.png" alt="image" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the value of the class attribute of the checkbox is “&lt;em&gt;mr-10&lt;/em&gt;”. So, the CSS Selector expression will be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nAmEacbU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2ACLneYlJLbHp1flM_.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nAmEacbU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2ACLneYlJLbHp1flM_.png" alt="image" width="800" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again, like the ID attribute, the class attribute also has a shortcut character that can be used to write the simplified CSS Selector expression. Here, we can use the dot (.) character, and the resulting expression will become:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MshYpHEP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A2Mx8Dt8NdgGBOcZF.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MshYpHEP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A2Mx8Dt8NdgGBOcZF.png" alt="image" width="800" height="301"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  CSS Selector with other attributes
&lt;/h3&gt;

&lt;p&gt;Just like the examples shared above with ID and class attributes, a CSS Selector expression can be written using any attribute of the element. As you must have noticed that the radio button element had multiple attributes.&lt;/p&gt;

&lt;p&gt;So now, I will use a different attribute to write the locator expression:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Lz2AsUGd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Au62WJZygsUqSn4fc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Lz2AsUGd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Au62WJZygsUqSn4fc.png" alt="image" width="800" height="489"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we can use the &lt;em&gt;value&lt;/em&gt; attribute, and as per the CSS Selector syntax, the expression will be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SKQgK7al--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AoVFoC3du4Mr3BDsX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SKQgK7al--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AoVFoC3du4Mr3BDsX.png" alt="image" width="800" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please note that the shortcut characters are only for the ID and class attributes and not for all attributes of a web element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Elevate the app testing process from these 12 best&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/blog/mobile-app-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;&lt;strong&gt;&lt;em&gt;mobile automation testing tools&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;. Our comprehensive list includes top-performing tools for achieving excellence. Learn more here.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  CSS Selector by combining ID with different attributes
&lt;/h3&gt;

&lt;p&gt;There are certain instances where you need to write the locator expression by combining different attributes of an element to identify the element uniquely.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qAvJc7xB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AjlPsF9c3WiaCNk4N.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qAvJc7xB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AjlPsF9c3WiaCNk4N.png" alt="image" width="800" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the textbox element can be located by combining the &lt;em&gt;ID&lt;/em&gt; attribute with the &lt;em&gt;type&lt;/em&gt; attribute of the element. The resulting expression will be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4FSOfv2V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AMa14GZI3plUTgFbE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4FSOfv2V--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AMa14GZI3plUTgFbE.png" alt="image" width="800" height="232"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The expression starts with the tag name &lt;em&gt;‘input‘&lt;/em&gt;. Then the &lt;em&gt;‘#‘&lt;/em&gt; character is used for the ID attribute followed by its value. And the additional attribute &lt;em&gt;‘type‘&lt;/em&gt; is placed inside the square brackets with its value &lt;em&gt;‘text‘&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  CSS Selector by combining class with different attributes
&lt;/h3&gt;

&lt;p&gt;Similar to the example shared above, a CSS Selector expression can be written by combining the class attribute with other attributes of an element.&lt;/p&gt;

&lt;p&gt;So for the same example, a resulting expression combining the class and value attributes can be written as:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7b3hK9cL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AsIlOt_RWMj0MoMAs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7b3hK9cL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AsIlOt_RWMj0MoMAs.png" alt="image" width="800" height="249"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Walkthrough:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The expression starts with the tag name &lt;em&gt;’input’&lt;/em&gt;. Then the &lt;em&gt;’.’&lt;/em&gt; character is used for the class attribute followed by its value. And the additional attribute ‘value’ is placed inside the square brackets with its value &lt;em&gt;’Male’&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;A CSS Selector expression can be written by following the same rule by combining any number of attributes of an element.&lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages of using CSS Selectors
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;CSS Selectors are the most effective locators in finding the elements having dynamic attribute values&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A CSS Selector expression offers better performance than the equivalent XPath expression&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A CSS Selector expression may look a little complicated but is easy to understand when compared with the XPath expression as it can be written in a simplified and short form.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Shortcomings of using CSS Selectors
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;CSS Selector only offers a unidirectional flow, i.e., from parent to child element. It is impossible to traverse from child to parent element using a CSS Selector expression.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CSS Selector doesn’t provide a way to locate the elements by matching the partial text of the value of their attributes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  XPath vs CSS Selectors — Which one is faster?
&lt;/h3&gt;

&lt;p&gt;If we talk about performance, it has been observed that CSS Selector expressions are much better in terms of performance. Now the question arises, &lt;em&gt;Why is XPath slower than the CSS Selector?&lt;/em&gt;&lt;/p&gt;

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

&lt;p&gt;Well, there is no definitive answer to this question, but a generalized reasoning could be that the XPath expression provides much more selections than CSS Selector. It’s a complex expression language, making it harder to optimize than the CSS Selector.&lt;/p&gt;

&lt;p&gt;In my opinion, there can be cases in your project where XPath can turn out to be quite reliable and fast. And in other cases, CSS Selector will be faster.&lt;/p&gt;

&lt;p&gt;While searching for the answer to this question, I came across some interesting discussions on some forums, and I thought of sharing some of them with you in this blog on XPath vs CSS Selectors.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h5MKV80O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AGJht-YHvnXBowxnS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h5MKV80O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AGJht-YHvnXBowxnS.png" alt="image" width="800" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Read the full discussion here.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--57fcQJIm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AJXomENyI9WM5-QH7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--57fcQJIm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AJXomENyI9WM5-QH7.png" alt="image" width="800" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Read the full discussion here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus Tip: How to write the best locator expressions?
&lt;/h3&gt;

&lt;p&gt;After a detailed discussion of all the locators, it is evident that each one holds significance. However, to fully harness the power of XPath and CSS Selectors, it is crucial to utilize them correctly. Here are some useful tips which you can refer to write reliable and robust locator expressions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;This article on the Best&lt;/em&gt;&lt;/strong&gt; &lt;a href="https://www.lambdatest.com/blog/top-ui-automated-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;&lt;strong&gt;&lt;em&gt;UI Automation Testing&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; &lt;strong&gt;&lt;em&gt;Tools cover what is UI automated testing, challenges while performing UI testing, and top tools that can help you perform UI testing.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Avoid the absolute locator expressions
&lt;/h3&gt;

&lt;p&gt;As they are the most fragile and unreliable. They tend to break as soon as any changes are made to the DOM structure within the starting node to your targeted node.&lt;/p&gt;

&lt;p&gt;Example of Absolute XPath expression:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--412VUGC0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AclGKtwazUvY2A7In.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--412VUGC0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AclGKtwazUvY2A7In.png" alt="image" width="800" height="198"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Example of Absolute CSS Selector expression:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e4kHHSGZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AI23PQQaYAbrht-YG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e4kHHSGZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AI23PQQaYAbrht-YG.png" alt="image" width="800" height="232"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Use relative locator expressions
&lt;/h3&gt;

&lt;p&gt;Instead, which are robust and more reliable even in case of any changes to the DOM structure as long as your targeted node is unchanged.&lt;/p&gt;

&lt;p&gt;Example of Relative XPath expression:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SKBsZMNV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Atpq31GHsXT5Co0Ed.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SKBsZMNV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Atpq31GHsXT5Co0Ed.png" alt="image" width="800" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Example of Relative CSS Selector expression:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rurhTwmQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AsqkNvXji-MpqRT9c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rurhTwmQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2AsqkNvXji-MpqRT9c.png" alt="image" width="800" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Always try to keep the locator expressions as short as possible
&lt;/h3&gt;

&lt;p&gt;By using unique attribute values.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Avoid writing your locator expressions using attributes that are likely to change in the future. For example, A &lt;em&gt;label&lt;/em&gt; attribute of a button with a value ‘OK’ might get changed to ‘Submit’ in the future.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Avoid writing index-based locators for selecting an element from multiple similar elements.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, consider the following HTML snippet:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zbcXGaSE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2ArbZ5l8_hRrCDhXIt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zbcXGaSE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2ArbZ5l8_hRrCDhXIt.png" alt="image" width="604" height="472"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to locate the first button, writing the locator as &lt;em&gt;//menu/button[1]&lt;/em&gt; might not be a good idea. This is so because if the position of the buttons is toggled in the future, then this locator expression will give unwanted results.&lt;/p&gt;

&lt;p&gt;The ideal locator expression, in this case, would be &lt;em&gt;//menu/button[text()=‘Option 1’]&lt;/em&gt; which is based on the button value instead of its index.&lt;/p&gt;

&lt;h3&gt;
  
  
  Best browser extensions for generating locator expressions
&lt;/h3&gt;

&lt;p&gt;Based on my experience, I have tried to explain the XPath and CSS Selectors in the most detailed manner so that you can write the locator expressions for the most stubborn elements you may come across in your career path.&lt;/p&gt;

&lt;p&gt;However, it becomes time-consuming to write the locator expressions manually for hundreds of elements when you work on a large-scale project. To deal with this challenge, many browser extensions are available which can automatically create locator expressions.&lt;/p&gt;

&lt;h3&gt;
  
  
  SelectorsHub
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/selectorshub-the-next-gen-xpath-css-selectors-tool/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=aug_03&amp;amp;utm_term=ap&amp;amp;utm_content=blog"&gt;SelectorsHub&lt;/a&gt; is a Chrome browser extension that can be used to quickly generate both XPath and CSS Selector expressions. It also lists all the available attributes of the element and eliminates the need to dig into the raw HTML. You can also right-click on the element and quickly copy the XPath or CSS Selector without opening the browser console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SkIo58JE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Av0HgryccbA-ZrNLb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SkIo58JE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Av0HgryccbA-ZrNLb.png" alt="image" width="800" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here in this example, you can see that it is listing down all the attributes and locator expressions of the checkbox &lt;em&gt;input&lt;/em&gt; element.&lt;/p&gt;

&lt;h3&gt;
  
  
  ChroPath
&lt;/h3&gt;

&lt;p&gt;ChroPath is another helpful Chrome browser extension that you can use to generate your locator expressions automatically. It also automatically lists all the attributes of the element along with the XPath and CSS Selector expressions.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NlAec8_B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A8UmYnoShGBHrEUHG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NlAec8_B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2A8UmYnoShGBHrEUHG.png" alt="image" width="800" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, you can see that it also lists all the attributes and locator expressions of the same checkbox &lt;em&gt;input&lt;/em&gt; element.&lt;/p&gt;

&lt;h3&gt;
  
  
  POM Builder
&lt;/h3&gt;

&lt;p&gt;POM Builder is also a chrome browser extension that works on the same principle of directly copying the XPath or CSS Selector locator by right-clicking on the desired element.&lt;/p&gt;

&lt;p&gt;It also recommends you the best locator based on its complexity and robustness.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--836AHB7---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Alk9lkAL-TBkVRO8u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--836AHB7---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/800/0%2Alk9lkAL-TBkVRO8u.png" alt="image" width="800" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here in this example, you can see that it lists down all the attributes and locator expressions of the radio &lt;em&gt;input&lt;/em&gt; element. It is also showing the recommended locator at the top.&lt;/p&gt;

&lt;h3&gt;
  
  
  XPath vs CSS Selectors: Conclusion
&lt;/h3&gt;

&lt;p&gt;So here we are at the end of the blog on XPath vs CSS Selectors, seeking the answer to the most widely asked question of the test automation community. Which one to use: XPath or CSS Selector?&lt;/p&gt;

&lt;p&gt;Well, to be honest, there cannot be a decisive answer to this question as both are the most powerful locators capable of writing rich, robust, and complex locator expressions to find the most challenging and dynamic elements. My inclination is towards CSS Selectors as I feel it’s a marginally better-performing locator expression when compared with the XPath. I also enjoy the ease of writing the CSS Selectors expressions using the shortcut characters.&lt;/p&gt;

&lt;p&gt;I also appreciate XPath’s ability to traverse the DOM in both directions and its extensive selection of axis methods. Therefore, it’s more of a need than a preference that becomes a deciding factor to choose between the two. Now I’ll end this blog on this final note in which I’ll urge you to experience the power of these amazing locators and choose the one which best suits your requirements.&lt;/p&gt;

&lt;p&gt;Good Bye and keep testing in progress!&lt;/p&gt;

</description>
      <category>xpath</category>
      <category>css</category>
      <category>automationtesting</category>
      <category>softwaretesting</category>
    </item>
  </channel>
</rss>
