Friday, October 12, 2018

Generating XPath With Multiple Attribute

Sometimes it may not be possible to locate an element with a single attribute. In the real world, we have a similar issue. We cannot locate a person just by his/her first name or last name. We have to use a combination of first name and last name to locate a person without making any confusion. A similar technique is used in selenium for locating elements.

Syntax:
//tag[@attribute1='value1'][@attribute2='value2']…[@attributeN='valueN']
Example:
//input[@class='button'][@type='submit'][@value='LOGIN'][@name='Submit']
Let's get a clear view with the below example:
//div[@class = 'ctr-p']
After using the above XPath in 'google.com', we will get two nodes in result.

XPath in Selenium WebDriver

Now if we want to get a single node than we can simply add another attribute in the above XPath and get the desired result. We will use below XPath:
//div[@class = 'ctr-p'][@id = 'footer']
Generating XPath With Multiple Attribute

Here we can see after adding new attribute we are able to select our desired node only. To get the other node we can just simply use:
//div[@class = 'ctr-p'][@id = 'viewport']
XPath Query With Multiple Attribute

No comments:

Post a Comment