Single Attribute:
Single attribute XPath uses one attribute to locate an web element.Syntax:
//tag[@attribute = 'value']Example:
//input[@id = 'user-message']This would select the input element nodes which has 'user-message' as it’s id attribute value.
Let's get a clear view with the below example:
//li[@id = 'ogbkddg:4']
Here, //li[@id = 'ogbkddg:4'] selects all the 'li' node which has 'ogbkddg:4' as it's attribute id.
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.
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']
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']
No comments:
Post a Comment