banner



How To Find Element Using Text In Selenium Webdriver

In Selenium automation, if the elements are not constitute by the general locators like id, course, name, etc. then XPath is used to find an chemical element on the web page.

In this tutorial, nosotros will acquire about the Xpath and different XPath expression to find the complex or dynamic elements, whose attributes changes dynamically on refresh or whatsoever operations.

In this XPath tutorial, you will learn-

  • What is XPath?
  • Types of 10-path
  • Absolute XPath
  • Relative XPath
  • Basic XPath
  • Contains()
  • Using OR & AND
  • Starts-with role
  • Text()
  • XPath axes methods
  • Following
  • Ancestor
  • Kid
  • Preceding
  • Following-sibling
  • Parent
  • Self
  • Descendant

What is XPath in Selenium?

XPath in Selenium is an XML path used for navigation through the HTML structure of the folio. Information technology is a syntax or language for finding any element on a spider web page using XML path expression. XPath can be used for both HTML and XML documents to find the location of whatsoever element on a webpage using HTML DOM structure.

The basic format of XPath in selenium is explained beneath with screen shot.

Basic Format of XPath

Bones Format of XPath

Syntax for XPath selenium:

XPath contains the path of the element situated at the web page. Standard XPath syntax for creating XPath is.

Xpath=//tagname[@attribute='value']
  • // : Select current node.
  • Tagname: Tagname of the particular node.
  • @: Select attribute.
  • Attribute: Attribute name of the node.
  • Value: Value of the aspect.

To detect the element on web pages accurately in that location are different types of locators:

XPath Locators Find different elements on web page
ID To find the chemical element by ID of the chemical element
Classname To discover the element past Classname of the element
Name To find the element by proper name of the element
Link text To detect the element by text of the link
XPath XPath required for finding the dynamic element and traverse between various elements of the web page
CSS path CSS path also locates elements having no proper name, class or ID.

Types of X-path

There are two types of XPath:

ane) Accented XPath

2) Relative XPath

Accented XPath:

It is the direct way to find the chemical element, simply the disadvantage of the absolute XPath is that if there are any changes made in the path of the element then that XPath gets failed.

The primal characteristic of XPath is that it begins with the unmarried forrad slash(/) ,which means you can select the chemical element from the root node.

Below is the case of an absolute Xpath expression of the element shown in the below screen.

Annotation: You can practice the following XPath exercise on this http://demo.guru99.com/test/selenium-xpath.html

Click here if the video is not accessible

Accented XPath:

/html/body/div[2]/div[1]/div/h4[1]/b/html[1]/body[1]/div[2]/div[1]/div[1]/h4[one]/b[1]

Absolute XPath

Absolute XPath

Relative Xpath:

Relative Xpath starts from the middle of HTML DOM structure. It starts with double forward slash (//). Information technology can search elements anywhere on the webpage, means no need to write a long xpath and you tin showtime from the middle of HTML DOM construction. Relative Xpath is e'er preferred as information technology is not a complete path from the root element.

Below is the example of a relative XPath expression of the same element shown in the below screen. This is the mutual format used to observe element by XPath.

Click hither if the video is not accessible

Relative XPath: //div[@grade='featured-box cloumnsize1']//h4[1]//b[i]

Relative XPath

Relative XPath

What are XPath axes.

XPath axes search different nodes in XML document from electric current context node. XPath Axes are the methods used to find dynamic elements, which otherwise not possible by normal XPath method having no ID , Classname, Proper noun, etc.

Axes methods are used to observe those elements, which dynamically change on refresh or any other operations. In that location are few axes methods commonly used in Selenium Webdriver similar child, parent, ancestor, sibling, preceding, self, etc.

How To Write Dynamic XPath In Selenium WebDriver

1) Basic XPath:

XPath expression select nodes or list of nodes on the basis of attributes like ID , Proper name, Classname, etc. from the XML document as illustrated beneath.

Xpath=//input[@name='uid']

Hither is a link to access the page http://demo.guru99.com/exam/selenium-xpath.html

Basic XPath

Basic XPath

Some more than basic xpath expressions:

Xpath=//input[@blazon='text']				 Xpath=	//characterization[@id='message23'] Xpath=	//input[@value='RESET'] Xpath=//*[@grade='barone'] Xpath=//a[@href='http://demo.guru99.com/'] Xpath= //img[@src='//guru99.com/images/dwelling/java.png']          

2) Contains():

Contains() is a method used in XPath expression. Information technology is used when the value of any aspect changes dynamically, for instance, login data.

The contain feature has an ability to find the element with fractional text equally shown in below XPath example.

In this instance, nosotros tried to identify the element by just using partial text value of the attribute. In the below XPath expression fractional value 'sub' is used in place of submit button. It can be observed that the element is found successfully.

Consummate value of 'Type' is 'submit' just using only partial value 'sub'.

Xpath=//*[contains(@type,'sub')]

Complete value of 'name' is 'btnLogin' but using simply fractional value 'btn'.

Xpath=//*[contains(@proper noun,'btn')]

In the to a higher place expression, we have taken the 'name' equally an attribute and 'btn' as an partial value as shown in the below screenshot. This will notice 2 elements (LOGIN & RESET) as their 'name' attribute begins with 'btn'.

XPath in Selenium WebDriver: Complete Tutorial

Similarly, in the below expression, nosotros have taken the 'id' as an attribute and 'bulletin' as a fractional value. This will find 2 elements ('User-ID must not exist blank' & 'Password must not exist bare') every bit its 'name' aspect begins with 'message'.

Xpath=//*[contains(@id,'message')]

XPath in Selenium WebDriver: Complete Tutorial

In the below expression, nosotros have taken the "text" of the link as an attribute and 'here' as a partial value every bit shown in the below screenshot. This will find the link ('hither') as it displays the text 'here'.

Xpath=//*[contains(text(),'hither')] Xpath=//*[contains(@href,'guru99.com')]

XPath in Selenium WebDriver: Complete Tutorial

3) Using OR & AND:

In OR expression, two conditions are used, whether 1st status OR 2nd status should be truthful. It is also applicative if whatsoever one status is true or maybe both. Means whatever ane condition should be true to find the element.

In the below XPath expression, it identifies the elements whose single or both conditions are truthful.

Xpath=//*[@type='submit' or @name='btnReset']

Highlighting both elements as "LOGIN " chemical element having attribute 'type' and "RESET" chemical element having attribute 'name'.

XPath in Selenium WebDriver: Complete Tutorial

In AND expression, ii conditions are used, both conditions should exist true to find the element. Information technology fails to notice chemical element if whatsoever one condition is false.

Xpath=//input[@blazon='submit' and @name='btnLogin']

In beneath expression, highlighting 'LOGIN' element as it having both attribute 'type' and 'name'.

XPath in Selenium WebDriver: Complete Tutorial

iv) Xpath Starts-with

XPath starts-with() is a office used for finding the web element whose attribute value gets changed on refresh or by other dynamic operations on the webpage. In this method, the starting text of the attribute is matched to notice the chemical element whose attribute value changes dynamically. You can also find elements whose attribute value is static (not changes).

For instance -: Suppose the ID of item element changes dynamically like:

Id=" message12″

Id=" message345″

Id=" message8769″

and and so on.. but the initial text is same. In this instance, we employ Outset-with expression.

In the beneath expression, there are ii elements with an id starting "message"(i.e., 'User-ID must not be blank' & 'Password must non exist blank'). In beneath instance, XPath finds those element whose 'ID' starting with 'message'.

Xpath=//label[starts-with(@id,'message')]

XPath in Selenium WebDriver: Complete Tutorial

5) XPath Text() Function

The XPath text() function is a built-in function of selenium webdriver which is used to locate elements based on text of a web element. It helps to observe the verbal text elements and information technology locates the elements inside the set of text nodes. The elements to exist located should exist in string grade.

In this expression, with text role, we find the element with exact text lucifer as shown beneath. In our instance, we find the chemical element with text "UserID".

Xpath=//td[text()='UserID']

XPath in Selenium WebDriver: Complete Tutorial

6) XPath axes methods:

These XPath axes methods are used to find the complex or dynamic elements. Beneath we will run across some of these methods.

For illustrating these XPath axes method, we will utilize the Guru99 banking concern demo site.

a) Following:

Selects all elements in the certificate of the current node( ) [ UserID input box is the electric current node] as shown in the beneath screen.

Xpath=//*[@type='text']//following::input

XPath in Selenium WebDriver: Complete Tutorial

There are 3 "input" nodes matching past using "post-obit" centrality- password, login and reset button. If you want to focus on any item element so you can employ the below XPath method:

Xpath=//*[@blazon='text']//following::input[one]

You tin change the XPath co-ordinate to the requirement by putting [1],[ii]…………and then on.

With the input as '1', the beneath screen shot finds the detail node that is 'Countersign' input box element.

XPath in Selenium WebDriver: Complete Tutorial

b) Ancestor:

The ancestor axis selects all ancestors chemical element (grandparent, parent, etc.) of the current node as shown in the below screen.

In the beneath expression, we are finding ancestors element of the electric current node("ENTERPRISE TESTING" node).

Xpath=//*[text()='Enterprise Testing']//ancestor::div

XPath in Selenium WebDriver: Complete Tutorial

At that place are 13 "div" nodes matching by using "ancestor" centrality. If you want to focus on any particular chemical element and then you can use the below XPath, where yous change the number 1, 2 as per your requirement:

Xpath=//*[text()='Enterprise Testing']//ancestor::div[i]

You can change the XPath co-ordinate to the requirement by putting [i], [two]…………and then on.

c) Child:

Selects all children elements of the current node (Java) equally shown in the below screen.

Xpath=//*[@id='java_technologies']//kid::li

XPath in Selenium WebDriver: Complete Tutorial

There are 71 "li" nodes matching by using "child" axis. If you want to focus on whatever detail element then you tin can use the below xpath:

Xpath=//*[@id='java_technologies']//child::li[1]

Y'all tin modify the xpath co-ordinate to the requirement by putting [1],[2]…………and and so on.

d) Preceding:

Select all nodes that come before the current node as shown in the below screen.

In the below expression, it identifies all the input elements earlier "LOGIN" button that is Userid and password input chemical element.

Xpath=//*[@blazon='submit']//preceding::input

XPath in Selenium WebDriver: Complete Tutorial

There are 2 "input" nodes matching by using "preceding" axis. If you want to focus on any particular element and then you can use the below XPath:

Xpath=//*[@blazon='submit']//preceding::input[1]

You tin alter the xpath according to the requirement by putting [1],[2]…………so on.

e) Following-sibling:

Select the following siblings of the context node. Siblings are at the same level of the current node equally shown in the below screen. It volition find the element after the current node.

            xpath=//*[@blazon='submit']//following-sibling::input

XPath in Selenium WebDriver: Complete Tutorial

One input nodes matching by using "following-sibling" axis.

f) Parent:

Selects the parent of the current node as shown in the below screen.

Xpath=//*[@id='rt-feature']//parent::div

XPath in Selenium WebDriver: Complete Tutorial

At that place are 65 "div" nodes matching by using "parent" axis. If you want to focus on any particular element then you lot tin employ the below XPath:

Xpath=//*[@id='rt-characteristic']//parent::div[ane]

You can change the XPath co-ordinate to the requirement past putting [ane],[2]…………and so on.

g) Cocky:

Selects the current node or 'self' means it indicates the node itself as shown in the beneath screen.

XPath in Selenium WebDriver: Complete Tutorial

One node matching past using "cocky " axis. It always finds simply one node as it represents self-chemical element.

Xpath =//*[@blazon='password']//cocky::input

h) Descendant:

Selects the descendants of the current node as shown in the below screen.

In the below expression, information technology identifies all the element descendants to current element ( 'Principal body surroundings' frame element) which means down nether the node (child node , grandchild node, etc.).

Xpath=//*[@id='rt-feature']//descendant::a

XPath in Selenium WebDriver: Complete Tutorial

At that place are 12 "link" nodes matching by using "descendant" axis. If you desire to focus on any detail element then y'all can utilise the below XPath:

Xpath=//*[@id='rt-characteristic']//descendant::a[1]

Y'all can change the XPath according to the requirement by putting [1],[ii]…………and so on.

Summary:

XPath is required to find an element on the web folio as to do an performance on that particular chemical element.

  • In that location are ii types of selenium XPath:
    • Accented XPath
    • Relative XPath
  • XPath Axes are the methods used to find dynamic elements, which otherwise non possible to find past normal XPath method
  • XPath expression select nodes or list of nodes on the basis of attributes similar ID , Name, Classname, etc. from the XML document .

Besides Check:- Selenium Tutorial for Beginners: Larn WebDriver in 7 Days

Source: https://www.guru99.com/xpath-selenium.html

Posted by: coatesperis1986.blogspot.com

0 Response to "How To Find Element Using Text In Selenium Webdriver"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel