<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Varun Varshney on Medium]]></title>
        <description><![CDATA[Stories by Varun Varshney on Medium]]></description>
        <link>https://medium.com/@varganpat12345?source=rss-11fe5158ab5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*U5bebg00TEfXTjAD</url>
            <title>Stories by Varun Varshney on Medium</title>
            <link>https://medium.com/@varganpat12345?source=rss-11fe5158ab5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 02:49:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@varganpat12345/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introduction to Log4j]]></title>
            <link>https://medium.com/gradeup/introduction-to-log4j-159c9dd9f719?source=rss-11fe5158ab5------2</link>
            <guid isPermaLink="false">https://medium.com/p/159c9dd9f719</guid>
            <category><![CDATA[logging]]></category>
            <category><![CDATA[quality-assurance]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Varun Varshney]]></dc:creator>
            <pubDate>Fri, 26 Oct 2018 10:11:04 GMT</pubDate>
            <atom:updated>2018-11-06T08:13:05.583Z</atom:updated>
            <content:encoded><![CDATA[<p>Log4j is a fast, flexible and reliable logging framework (APIS) written in<a href="https://www.guru99.com/java-tutorial.html"> Java </a>developed in early 1996. It is distributed under the<a href="https://www.guru99.com/apache.html"> Apache </a>Software License. Log4J has been ported to the C, C++, C#, <a href="https://www.guru99.com/perl-tutorials.html">Perl</a>, <a href="https://www.guru99.com/python-tutorials.html">Python</a>, Ruby and Eiffel Languages. It is a tool used for small to large scale Selenium Automation projects.</p><p>log4j is highly configurable through external configuration files at runtime. It views the logging process in terms of levels of priorities and offers mechanisms to direct logging information to a great variety of destinations, such as a database, file, console, UNIX Syslog etc.</p><h3>Why use Log4j?</h3><ul><li>It is an open source</li><li>With Log4j, it is possible to store the flow details of our Selenium Automation in a file or databases</li><li>Log4j is used for large as well as small projects</li><li>In Log4j, we use log statements rather than SOPL statements in the code to know the status of a project while it is executing.</li></ul><h3>Log4j Features</h3><ul><li>It is thread-safe.</li><li>It is optimized for speed.</li><li>It is based on a named logger hierarchy.</li><li>It supports multiple output appenders per logger.</li><li>It supports internationalization.</li><li>It is not restricted to a predefined set of facilities.</li><li>Logging behavior can be set at runtime using a configuration file.</li><li>It is designed to handle Java Exceptions from the start.</li></ul><p><strong>Log4j has three main components:</strong></p><ul><li>loggers: Responsible for capturing logging information.</li><li>appenders: Responsible for publishing logging information to various preferred destinations.</li><li>layouts: Responsible for formatting logging information in different styles.</li></ul><p><strong>Loggers</strong></p><p>It is responsible for logging information. To implement loggers into a project following steps need to be performed<strong>.</strong></p><p>Create an instance for logger class: Logger class is a Java-based utility that has got all the generic methods already implemented to use log4j.</p><p>The Logger class provides different methods to handle logging activities. It provides two static methods for obtaining a Logger Object.</p><ul><li><strong>Public static Logger getRootLogger()</strong></li><li><strong>Public static Logger getLogger(String name)</strong></li></ul><p><strong>Define the Log4j level:</strong></p><p>Primarily there are five kinds of log levels</p><ul><li>All — This level of logging will log everything ( it turns all the logs on )</li><li>DEBUG — print the debugging information and is helpful in development stage</li><li>INFO — print informational message that highlights the progress of the application</li><li>WARN — print information regarding faulty and unexpected system behavior.</li><li>ERROR — print error message that might allow system to continue</li><li>FATAL — print system critical information which are causing the application to crash</li><li>OFF — No logging</li></ul><p><strong>Appenders</strong>:</p><p>It is used to deliver LogEvents to their destination. It decides what will happen with log information. In simple words, it is used to write the logs in file. Following are few types of Appenders</p><ul><li>ConsoleAppender logs to standard output</li><li>File appender prints logs to some file</li><li>Rolling file appender to a file with maximum size</li></ul><p><strong>Layouts</strong>:</p><p>It is responsible for formatting logging information in different styles.</p><h3>The Layout Types</h3><p>The top-level class in the hierarchy is the abstract class org.apache.log4j.Layout. This is the base class for all other Layout classes in the log4j API.</p><p>The Layout class is defined as abstract within an application, we never use this class directly; instead, we work with its subclasses which are as follows:</p><ul><li>DateLayout</li><li>HTMLLayout</li><li>PatternLayout</li><li>SimpleLayout</li><li>XMLLayout</li></ul><h3>How log4j is configured?</h3><p>To configure log4j we have to decide which appender to implement. Accordingly, parameters of appender will be set.</p><ul><li>We will use ALL level and RollingFileAppender</li><li>We will do two configurations or logs,</li></ul><p>First: root logger, that will write all system generated logs in file name i.e. Root.logs</p><p>Second: Will write the information generated by manual commands in code into the file name- App.logs</p><ul><li>Layout will be PatternLayout</li></ul><p><strong>Step 1: Create Dependencies in pom.xml file of log4j</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*sRWIghjaIGNTq7NQWIFBjQ.png" /></figure><p><strong>Step 2: Create log4j.properties File</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/642/0*SJpxS0AQDa0nv8Lp" /></figure><p><strong>Step 3: Store log4j.properties File</strong></p><p>Put the log4j.properties file in the path as follows:</p><p>/src/main/resources/log4j.properties</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/985/0*7r37MRT2iOJkhygq" /></figure><p><strong>Step 4: Create Two Log Files</strong></p><p>Path of the files, as follows:</p><p>/projects/Demo/Root.logs</p><p>/projects/Demo/App.logs</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/0*DVhXozOfzo6a5Snn" /></figure><h4><strong>Step 5: Create Sample Program:</strong></h4><p><strong>i</strong>mport org.apache.log4j.*;</p><p>import org.testng.annotations.Test;</p><p>public class Demo</p><p>{</p><p>static Logger <em>log</em>=Logger.<em>getLogger</em>(“Demo”);</p><p>@Test</p><p>public void main2()</p><p>{</p><p><em>log</em>.trace(“Trace Message!”);</p><p><em>log</em>.debug(“Debug Message!”);</p><p><em>log</em>.info(“Info Message!”);</p><p><em>log</em>.warn(“Warn Message!”);</p><p><em>log</em>.error(“Error Message!”);</p><p><em>log</em>.fatal(“Fatal Message!”);</p><p>}</p><p>}</p><p><strong>Step 6: Now Run the Class and observe the log Files:</strong></p><p><strong>App.logs</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/503/0*SmXe8A6-Jah1XAfX" /></figure><p><strong>Root.Logs</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/0*VOFkdXIQK3IwcXVr" /></figure><p><strong>Important Points:</strong></p><p>1)We can use Logger method in multiple classes but for that we have to edit . the configurations for each class in log4j.properties file.</p><p>For Example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/703/1*rxRu1QuIh1oQ0po32k3WOw.png" /></figure><p>In the above Example:</p><p>We make:</p><p>a) Root Logs: which had all the logs of the classes.</p><p>b) Application Logs: which had 3 logs file as App1.logs , App2.logs , and App3.logs for each different classes.</p><p>2.)<strong> How do Levels Works?</strong></p><p>A log request of level <strong>p</strong> in a logger with level <strong>q</strong> is <strong>enabled</strong> if p &gt;= q. This rule is at the heart of log4j. It assumes that levels are ordered. For the standard levels, we have ALL &lt; DEBUG &lt; INFO &lt; WARN &lt; ERROR &lt; FATAL &lt; OFF.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/445/1*e1Sn7E5qZVvkEoG0BisKtQ.png" /></figure><p>The Following example shows how we can filter all our DEBUG and INFO messages. This program uses of logger method setLevel(Level.X) to set a desired logging level:</p><p>This example would print all the messages except Debug and Info:</p><pre>import org.apache.log4j.*;<br><br>public class LogClass {<br>   private static org.apache.log4j.Logger log = Logger.getLogger(LogClass.class);<br>   <br>   public static void main(String[] args) {<br>      log.setLevel(Level.WARN);<br><br>      log.trace(&quot;Trace Message!&quot;);<br>      log.debug(&quot;Debug Message!&quot;);<br>      log.info(&quot;Info Message!&quot;);<br>      log.warn(&quot;Warn Message!&quot;);<br>      log.error(&quot;Error Message!&quot;);<br>      log.fatal(&quot;Fatal Message!&quot;);<br>   }<br>}</pre><p>When you compile and run the <strong>LogClass</strong> program, it would generate the following result −</p><pre>Warn Message!<br>Error Message!<br>Fatal Message!</pre><p>3.)</p><p><strong>log4j.appender.FILE.Append=true :</strong></p><p>This properties of File Appender will append the logs each time a class is executed.</p><p><strong>log4j.appender.FILE.Append=false:</strong></p><p>This properties of File Appender will not append the logs each time a class is executed i.e each time store current logs.</p><p>4.) <strong>HTMLLayout Example</strong></p><p>Following is a simple configuration file for HTMLLayout:</p><pre># Define the root logger with appender file<br>log = /usr/home/log4j<br>log4j.rootLogger = DEBUG, FILE</pre><pre># Define the file appender<br>log4j.appender.FILE=org.apache.log4j.FileAppender<br>log4j.appender.FILE.File=${log}/htmlLayout.html</pre><pre># Define the layout for file appender<br>log4j.appender.FILE.layout=org.apache.log4j.HTMLLayout<br>log4j.appender.FILE.layout.Title=HTML Layout Example<br>log4j.appender.FILE.layout.LocationInfo=true</pre><p>Now consider the following Java Example which would generate logging information:</p><pre>import org.apache.log4j.Logger;</pre><pre>import java.io.*;<br>import java.sql.SQLException;<br>import java.util.*;</pre><pre>public class log4jExample{<br>   /* Get actual class name to be printed on */<br>   static Logger log = Logger.getLogger(log4jExample.class.getName());<br>   <br>   public static void main(String[] args)throws IOException,SQLException{<br>      log.debug(&quot;Hello this is an debug message&quot;);<br>      log.info(&quot;Hello this is an info message&quot;);<br>   }<br>}</pre><p>Compile and run the above program. It would create an htmlLayout.html file in /usr/home/log4j directory which would have the following log information:</p><p>Log session start time Mon Mar 22 13:30:24 AST 2010</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*tcRo1bKu5BfoLrwPm4dGpA.png" /></figure><p>You would use a web browser to open htmlLayout.html file. It is also important to note that the footer for the &lt;/html&gt; and &lt;/body&gt; tags is completely missing.</p><p>One of the big advantages of having log file in HTML format is that it can be published as a web page for remote viewing.</p><p><strong>5.) What are Pros and Cons of Logging?</strong></p><p>Following are the Pros and Cons of Logging −</p><p>Logging is an important component of the software development. A well-written logging code offers quick debugging, easy maintenance, and structured storage of an application’s runtime information.</p><p>Logging does have its drawbacks also. It can slow down an application. If too verbose, it can cause scrolling blindness. To alleviate these concerns, log4j is designed to be reliable, fast and extensible.</p><p>Since logging is rarely the main focus of an application, the log4j API strives to be simple to understand and to use.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=159c9dd9f719" width="1" height="1" alt=""><hr><p><a href="https://medium.com/gradeup/introduction-to-log4j-159c9dd9f719">Introduction to Log4j</a> was originally published in <a href="https://medium.com/gradeup">BYJU’S Exam Prep Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Selenium Webdriver Locators]]></title>
            <link>https://medium.com/gradeup/introduction-to-selenium-webdriver-locators-9c3807d6440?source=rss-11fe5158ab5------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c3807d6440</guid>
            <category><![CDATA[webdriver]]></category>
            <category><![CDATA[quality-assurance]]></category>
            <category><![CDATA[selenium]]></category>
            <category><![CDATA[automation-testing]]></category>
            <category><![CDATA[scripting]]></category>
            <dc:creator><![CDATA[Varun Varshney]]></dc:creator>
            <pubDate>Tue, 18 Sep 2018 13:12:52 GMT</pubDate>
            <atom:updated>2018-09-18T13:12:52.549Z</atom:updated>
            <content:encoded><![CDATA[<p>The locator can be termed as an address that identifies a GUI element uniquely within the webpage. Locators are the HTML properties of a GUI element which tells the Selenium about the web element it needs to perform the action on.</p><p>There is a diverse range of web elements. The most common amongst them are:</p><ul><li>Text box</li><li>Button</li><li>Drop Down</li><li>Hyperlink</li><li>Check Box</li><li>Radio Button</li></ul><p>Locator is a command that tells Selenium IDE which GUI elements ( say Text Box, Buttons, Check Boxes etc) its needs to operate on. Identification of correct GUI elements is a prerequisite to creating an automation script. But accurate identification of GUI elements is more difficult than it sounds. Sometimes, you end up working with incorrect GUI elements or no elements at all! Hence, Selenium provides a number of Locators to precisely locate a GUI element.</p><p><strong>Types of Locators?</strong></p><p>The different types of Locators are as follows,</p><p>The different types of Locators :</p><ol><li><strong>ID</strong></li><li><strong>Name</strong></li><li><strong>Link Text</strong></li><li><strong>CSS Selector</strong></li></ol><ul><li>Tag and ID</li><li>Tag and class</li><li>Tag and attribute</li><li>Tag, class, and attribute</li><li>Inner text</li></ul><p>5<strong>. DOM (Document Object Model)</strong></p><ul><li>getElementById</li><li>getElementsByName</li><li>dom:name</li><li>dom: index</li></ul><p>6<strong>. XPath</strong></p><p>In this section, we are going to discuss the XPath ,CSS Selector and DOM with their expressions that are capable of locating complex and dynamic web elements on the web page.</p><p><strong>XPath Locators</strong></p><p>Dynamic web elements are elements on the web page whose attributes get changed dynamically when refreshed or on dynamic operations. As we know that in Selenium WebDriver, if we cannot find the web elements with the help of usual locators such as id, class, name, etc. then we use XPath to find such a complex Web Element using WebDriver API.</p><p><strong>What is Xpath</strong></p><p>XPath is defined as XML path. It is a syntax or language for finding any element on the web page using XML path expression. XPath is used to find the location of any element on a webpage using HTML DOM structure.</p><h3>Various types of X-path</h3><p>Xpath are of two types as follows.</p><ul><li><strong>Absolute Xpath: </strong>It is the most straight forward way of locating a web element but the demerit for the absolute Xpath is that it gets failed when there are any changes made in the Xpath of the web element on the HTML document. It has a characteristic that it begins with a single forward slash character (‘/’) i.e. selecting an element starting from the root node. Following is the syntax cum example for an absolute XPath expression of the element</li></ul><p><strong><em>html/body/div[1]/section/div[1]/div/div/div[3]/div/div/div/div/div[2]/div[1]/div/h3[2]/b</em></strong></p><ul><li><strong>Relative Xpath: </strong>For this type of Xpath, the path begins from the middle of the HTML DOM (Document Object model) structure. It has a characteristic that it begins with a double forward slash character (‘//’) i.e. selecting a web element from anywhere of the web page. It has a benefit to begin from the middle of the HTML DOM structure and abstain from writing long XPath starting from the root node. Following is the syntax cum example for a relative XPath expression of the element</li></ul><p><strong><em>Relative xpath: //*[@class=’class-test-box’]//*</em></strong></p><h3>Xpath axes:</h3><p>Xpath axes are used to search various nodes in XML document from the current node context. These are the methods which are used to locate those web elements which cannot be located by the normal Xpath method that does not have ID, Class name, name, link text, CSS path, etc. Therefore, axes have the use to locate the web elements that changes dynamically on refresh or on dynamic operations.</p><p>Examples of axes are ancestor, parent, child, sibling, preceding, self, etc.</p><h3>Difference between single ‘/’ or double ‘//’</h3><p>A <strong>single slash</strong> at the start of Xpath instructs XPath engine to look for element starting from <strong>root node</strong>.</p><p>A <strong>double slash</strong> at the start of Xpath instructs XPath engine to search look for matching element <strong>anywhere</strong> in the XML document.</p><h3>Handling complex &amp; dynamic web elements by using XPath:</h3><p><strong>1) Basic XPath</strong></p><p>XPath expression select nodes or list of nodes on the basis of attributes like <strong>ID , Name, Classname</strong>, etc. from the XML document as illustrated below.</p><p>Syntax: .//&lt;htmlTag&gt;[@attributeType=’attributeValue’]</p><p>a.) By Id</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/0*7I2yBPMwYNeQDNb2" /></figure><p>b.) By name</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/0*JQqW-FaJWBsKmyBo" /></figure><p>c.)By ClassName</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/0*vp3vCMfwzfsPxRna" /></figure><p><strong>Note:</strong> ‘<strong>*</strong>‘ after double slash is to match any tag with the desired text</p><p>Example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/964/0*r-fO5GZ0qDf2U2r3" /></figure><p><strong>2) contains():</strong></p><p>It is a method that is used in an XPath expression. When the value of any attribute changes dynamically e.g. login information, this method come into use. It can locate a web element with the available partial text. Following are the examples of contains () method.</p><ul><li>Xpath=//*[contains (@type, ‘sub-type’)]</li><li>Xpath=.//* [contains (@name, ‘button’)]</li><li>Xpath=//*[contains(@id, ‘login’)]</li><li>Xpath=//*[contains(text (),’testing’)]</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/0*1PnExcoX71n7cttm" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/0*sDB_n4svrmyfcePF" /></figure><p><strong>3) Using OR &amp; AND:</strong></p><p>In OR expression, two conditions are used, whether 1st condition OR 2nd condition should be true. It is also applicable if any one condition is true or maybe both. Means any one condition should be true to find the element.</p><p>In the below XPath expression, it identifies the elements whose single or both conditions are true.</p><p>Xpath=//*[@type=’submit’ OR @name=’btnReset’]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/505/0*BBc2jjrtmfe_Z35y" /></figure><p>In AND expression, two conditions are used, both conditions should be true to find the element. It fails to find element if any one condition is false.</p><p>Xpath=//input[@type=’submit’ and @name=’btnLogin’]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/0*sRxD4Wn4QUMabvCR" /></figure><p><strong>4) Start-with function:</strong></p><p>This function is used to find a web element whose value of attribute changes on the refresh or on any dynamic operation on the web page. In this expression, we match the starting text of the attribute that is used to locate an element whose attribute has changed dynamically. E.g.On the web page ID of a particular element changes dynamically such as ‘id1’, ‘id2’, ‘id3’, etc. but the text remains the same. Following are the examples of starts-with expression.</p><p>Xpath=//label[starts-with(@id, ‘message’)]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/549/0*5dJZMCDFVzFrQh8J" /></figure><p><strong>5) Text():</strong></p><p>In this expression, with text function, we find the element with exact text match as shown below.</p><p>Xpath=//td[text()=’UserID’]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/689/0*dF4EPAfFtBOK3DlB" /></figure><p><strong>6). position()</strong></p><p>Selects the element out of all input element present depending on the position number provided.</p><p>xpath=//input[@type=’text’])[2]</p><p>Or</p><p>xpath=//input[@type=’text’])[position()=2]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/690/0*ccU2Tdp6NsdJQWmu" /></figure><p><strong>7. Finding elements using index</strong></p><p>By providing the index position in the square brackets, we could move to the nth element. Based on the below xpath, we could identify the Last Name field.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/0*30wtgSaevPGGsni7" /></figure><p><strong>8. last()</strong></p><p>Selects the last element (of mentioned type) out of all input element present</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/0*kSs8lryjLlnxqm6h" /></figure><p><strong>9. Multiple Attribute</strong></p><p>//&lt;HmlTtag&gt;[@attribute_name1=’attribute_value1&#39;][@attribute_name2=’attribute_value2]</p><p>Or</p><p>//*[@attribute_name1=’attribute_value1&#39;][@attribute_name2=’attribute_value2]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/609/0*QhvIolSV4xgRsG27" /></figure><p><strong>10. X-Path Axes Methods</strong></p><p><strong>a)Following:</strong></p><p>This method selects all the elements in the HTML document from the current node. Below is an example.</p><ul><li>Xpath=//*[@type=’password’]//following::input</li><li>Xpath=//*[@type=’password’]//following::input[2]</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/0*SsjI0AKM77vTMZYp" /></figure><p><strong>b) Ancestor:</strong></p><p>This method selects all the ancestors’ element such as grandparent, parent, etc. from the current node. Below is an example.</p><ul><li>Xpath=//*//ancestor::p</li><li>Xpath=//*//ancestor::div [2]</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/909/0*p-egTIS0AvHQAD0d" /></figure><p><strong>c) Child:</strong></p><p>This method selects all the children elements from the current node. Below is an example.</p><ul><li>Xpath=//*[@id=’navigation-list’]/child::li</li><li>Xpath=//*[@id=’navigation-list’]/child::li[2]</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/0*SdGB2r5vcj-XTYMT" /></figure><p><strong>d) Preceding:</strong></p><p>This method selects all the nodes that come before the current node. Below is an example.</p><ul><li>Xpath=//*[@type=’text’]//preceding::input</li><li>Xpath=//*[@type=’text’]//preceding::input[3]</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/921/0*klS2rgdk1WoeiD9n" /></figure><p><strong>e) Following-sibling:</strong></p><p>This method Select the following siblings from the context node. Siblings are located at the same level of the current. Below is an example.</p><ul><li>xpath=//*[@type=’text’]//following-sibling::input</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/0*utESWYHsqFyTpFGu" /></figure><p><strong>f) Parent</strong>:</p><p>This method selects the parent of the current node. Below is an example.</p><ul><li>Xpath=//*[@id=’soft-test-class’]//parent::div</li><li>Xpath=//*[@id=’soft-test-class’]//parent::div[1]</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/812/0*be_vktc0W4pOORec" /></figure><p><strong>g) Self:</strong></p><p>This method selects the current node. Below is an example.</p><ul><li>Xpath =//*[@type=’text’]//self::input</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/757/0*Jgn_2jFUqxHgKyat" /></figure><p><strong>h) Descendant:</strong></p><p>This method selects the descendants of the current. Below is an example.</p><ul><li>Xpath=//*[@id=’soft-test-class’]//descendant::a</li><li>Xpath=//*[@id=’soft-test-class’]//descendant::a[1]</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/0*04VrDPn6ySNtjsNT" /></figure><h3>Locating by CSS Selector</h3><p>CSS Selectors are string patterns used to identify an element based on a combination of HTML tag, id, class, and attributes.</p><p>CSS Selectors have many formats, but we will only focus on the most common ones.</p><ul><li>Tag and ID</li><li>Tag and class</li><li>Tag and attribute</li><li>Tag, class, and attribute</li><li>Inner text</li></ul><p><strong>1.) Tag and ID</strong></p><p><strong>Syntax: </strong>$$(“tag#id”)</p><p><strong>Description:</strong></p><ul><li><strong>Tag</strong> → the HTML tag of the element being accessed</li><li><strong># </strong>— -&gt; the hash sign. This should always be present when using a CSS Selector with ID</li><li><strong>Id </strong>— -&gt; the ID of the element being accessed</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/0*7tJ50k8n1vJMq22G" /></figure><p><strong>2.) Tag and Class</strong></p><p><strong>Syntax: </strong>$$(“tag.class”)</p><p><strong>Description:</strong></p><ul><li><strong>Tag</strong> → the HTML tag of the element being accessed</li><li><strong>. </strong>— -&gt; the dot sign. This should always be present when using a CSS Selector with class</li><li><strong>class </strong>— -&gt; the class of the element being accessed</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/0*67fVnWTRTZmWjq0-" /></figure><p><strong>3.) Tag and Attribute</strong></p><p><strong>Syntax: </strong>$$(“tag[attribute=value]”)</p><p><strong>Description:</strong></p><ul><li><strong>Tag</strong> → the HTML tag of the element being accessed</li><li><strong>[] </strong>— -&gt;square brackets within which a specific attribute and its corresponding value will be place</li><li><strong>attribute </strong>— -&gt; the attribute to be used. It is advisable to use an attribute that is unique to the element such as a name or ID.</li><li><strong>value</strong> — -&gt;the corresponding value of the chosen attribute.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/0*qy_ynK7fwRR-3iwS" /></figure><p><strong>4.) Tag , Class and Attribute</strong></p><p><strong>Syntax: </strong>$$(“tag.class[attribute=value]”)</p><p><strong>Description:</strong></p><ul><li><strong>Tag</strong> → the HTML tag of the element being accessed</li><li><strong>[] </strong>— -&gt;square brackets within which a specific attribute and its corresponding value will be place</li><li><strong>. -</strong>-&gt; the dot sign. This should always be present when using a CSS Selector with class</li><li><strong>class </strong>— -&gt; the class of the element being accessed</li><li><strong>attribute </strong>— -&gt; the attribute to be used. It is advisable to use an attribute that is unique to the element such as a name or ID.</li><li><strong>value</strong> — -&gt;the corresponding value of the chosen attribute.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/599/0*0jUqr8ohHz0iPvAm" /></figure><p><strong>4.) Inner Text</strong></p><p><strong>Syntax: </strong>$$(“tag.contains(“innertext”)”)</p><p><strong>Description:</strong></p><ul><li><strong>Tag</strong> → the HTML tag of the element being accessed</li><li><strong>innerttext </strong>— -&gt; inner text of the element</li></ul><h3>Locating by DOM (Document Object Model)</h3><p>There are four basic ways to locate an element through DOM:</p><ul><li>getElementById</li><li>getElementsByName</li><li>dom:name (applies only to elements within a named form)</li><li>dom:index</li></ul><p><strong>1.) getElementById:</strong></p><ul><li>It will get only one element for you.</li><li>That element bears the ID that you specified inside the parentheses of getElementById().</li></ul><p><strong>Syntax: </strong>document.getElementById(“<em>id of the element</em>”)</p><p><strong>Description:</strong></p><p>id of the element = this is the value of the ID attribute of the element to be accessed. This value should always be enclosed in a pair of parentheses (“”).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/764/0*QbKp-_OM3On6ZyOr" /></figure><p><strong>2.) getElementsByName</strong></p><ul><li>It will get a collection of elements whose names are all the same.</li><li>Each element is indexed with a number starting from 0 just like an array</li><li>You specify which element you wish to access by putting its index number into the square brackets in getElementsByName’s syntax below.</li></ul><p><strong>Syntax: </strong>document.getElementsByName(“<em>name</em>”)[<em>index</em>]</p><p><strong>Description:</strong></p><ul><li><strong>name</strong> = name of the element as defined by its ‘name’ attribute</li><li><strong>index</strong> = an integer that indicates which element within getElementsByName’s array will be used.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/698/0*ptNIYOUZgRJ4Gt77" /></figure><p><strong>3.) dom:name</strong></p><p><strong>Syntax: </strong>document.forms[“<em>name of the form</em>”].elements[“<em>name of the element</em>”]</p><p><strong>Description:</strong></p><ul><li><strong>name of the form</strong> = the value of the name attribute of the form tag that contains the element you want to access</li><li><strong>name of the element</strong> = the value of the name attribute of the element you wish to access</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/0*hM1GoFpNwNujcx7h" /></figure><p><strong>4.) dom:index</strong></p><p><strong>Syntax: </strong>document.forms[“<em>index of the form</em>”].elements[“<em>index of the element</em>”]</p><p><strong>Description:</strong></p><ul><li><strong>index of the form</strong> = the value of the id attribute of the form tag that contains the element you want to access</li><li><strong>Index of the element</strong> = the value of the id attribute of the element you wish to access</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/0*tnlvsoHcwuiGGvtV" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/476/1*KANUT0qdN73_CTemrE4SMQ.png" /></figure><p><strong>Xpath Vs Css Selector (Which one is better):</strong></p><p>CSS selectors perform far better than Xpath and it is well documented in Selenium community. Here are some reasons,</p><ul><li>Xpath engines are different in each browser, hence make them inconsistent</li><li>IE does not have a native xpath engine, therefore selenium injects its own xpath engine for compatibility of its API. Hence we lose the advantage of using native browser features that WebDriver inherently promotes.</li><li>Xpath tend to become complex and hence make hard to read in my opinion</li></ul><p>However there are some situations where, you need to use xpath, for example, searching for a parent element or searching element by its text.</p><p>That’s it, folks!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c3807d6440" width="1" height="1" alt=""><hr><p><a href="https://medium.com/gradeup/introduction-to-selenium-webdriver-locators-9c3807d6440">Introduction to Selenium Webdriver Locators</a> was originally published in <a href="https://medium.com/gradeup">BYJU’S Exam Prep Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>