What are the main functions of Xpath locator in Selenium?

0 votes
Can someone tell me what are the main functions of Xpath locator in Selenium?
May 9 in Selenium by Danish
50 views

1 answer to this question.

0 votes

Hi Danish, for handling complex and dynamic web elements, selenium provides some methods which can be used with Xpath. Following are the Xpath functions which are widely used:

1) Basic Xpath: This function selects nodes or list of nodes on the basis of attributes such as name, class name, id, etc. For eg. Xpath=//input[@id='id1']  or  Xpath=//*[@class='class1'

2) Contains(): It is a method that is used when the value of any attribute changes dynamically e.g. login information. It can locate a web element with the available partial text. For eg. Xpath=.//* [contains (@name, ‘button’)] or Xpath=//*[contains(text (),’sector’)]

3) Using OR & AND: In the case of the OR expression, we use two conditions. Here either 1st condition OR 2nd condition should be true. It is applied when one condition is true or both. It means that at least one condition should be true to find a web element. For eg. Xpath=//*[@type=’submit’ OR @name=’buttonSubmit’]

In the case of the AND expression, we use two conditions. Here both conditions should be true to locate a web element. It will fail to locate an element if any of the conditions are false. For eg. Xpath=//input[@type=’submit’ AND @name=’buttonEnter’]

4) Start-with function: 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. For eg. On a web page ID of a particular element changes dynamically such as ‘id1’, ‘id2’, ‘id3’, etc. but the text remains the same. So the Xpath for this element would be: Xpath=//label[starts-with(@id, ‘message’)]

5) Text(): This expression is used with the text function to locate an element with exact text. Following are the examples of text expression. For eg. Xpath=//td

answered May 9 by Anvi
• 13,400 points

Related Questions In Selenium

0 votes
1 answer

What is Xpath and what are the types of it in Selenium Webdriver?

Hi Piyush, XPath is defined as XML path. It is ...READ MORE

answered May 8 in Selenium by Pratibha
• 3,180 points
169 views
0 votes
1 answer
0 votes
1 answer

What are the main types of regex quantifiers used in Selenium Webdriver?

Hi Andrew, some of the regex quantifiers ...READ MORE

answered Jun 26 in Selenium by Abha
• 24,440 points
66 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,090 points
5,600 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
759 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,001 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
713 views
0 votes
1 answer

What are the different types of TestNG listeners in Selenium Webdriver?

Hello Jagrati, some of the TestNG listeners ...READ MORE

answered Jun 12 in Selenium by Anvi
• 13,400 points
89 views