In order to perform any operation on the element like click or type into an element, we need to locate that element. It is very simple to locate elements if the HTML DOM has 'id' or 'name' and they are the safest locators to use. As we know it is always better to use ID and Name to locate the elements which will work for sure. You don't need to search for any other locator if there is ID or unique name present in your application.
It is always very important to make test scripts robust with reliable locators that do not break until changes made. In many cases like these, we depend locating elements by CSS or by XPath.
Though we have some browser plug-ins to generate xpath or css selector, but they are not much useful in real time applications.
Let us look for xpath examples to use ID and Name effectively with combinations
If the html looks like below :
<input type="text" aria-label="Email or Phone" value="Email or Phone" tabindex="1" placeholder="" name="email" id="email" class="inputtext _5aju" style="background-color: transparent;">
We can use as below :
In xpath we can use in different ways
1. With ID : -
//input[@id='email'] or we can also use as
2. With Name -
//input[@name='email'] or we can also use as
In css we can use as below :
1. With ID -
2. With Name -
All the above syntax are simple. We can directly use them by using id or name locators.
Identify element using multiple attributes
Here using xpath / Css, we can combine two locators when ever required, lets see how we can achieve.
Using xpath : -
//*[@id='email' or @name='email'], here first it will check for the id and then it will check for the second.
Based on index also, we can define the path as
We can also define by the using the value attribute
//input[@name='email'][@value='Email or Phone']
The same with Css
css=input[name=email][value=Email or Phone]
We can also define xpath with 'Style' attribute xpath
How to access direct child elements using xpath
A child in XPATH is represented with a "/". Example XPATH for child elements :
How to access Child elements using css selectors
In CSS the child is indicated with a "
>". Css examples of a link inside of a div tag can be identified as
div > a
And sometimes, if the element is not direct child, may be the element is inside another element. In such cases, we can use two slashes to match any subnode for xpath. Example for xpath as
//div//a. In css this is very simple by using whitespace. Example css for child / sub child as
How to match on text using CSS locators and Xpath
Now lets us look at the examples for 'Text'. When working with text, we will have two scenarios, one is 'Exactly' and other one is 'Contains'. As name describes, 'Exactly' will try to find the exact match and Contains looks for multiple matches.
We can use like this //button[.='Log In'] as Xpath to find out element containing exactly 'Log In'.
If the HTML is as below:
<div class="header-right">Check Our <a href="http://demo-test.com" class="btn btn-success">Demo Website!</a></div>
We find element by using xpath as
//div[contains(text(),'Demo Website!')] or
The above can be done using css as
css=div:contains('Demo Website!'). But If you want to match exactly to the text then we should have something like
css=a[text='Demo Website!'] or
Working with Links / anchor tags
Links have anchor tags, we can apply the same as we applied for 'Text', the only difference here is we should add anchor tag.
We can just use as 'link=Forgot your password?', using xpath we should use as //a[.='Forgot your password?']
We can also specify the partial text of the link as //a[contains(text(),'Forgot')]. This also can be written as //a[contains(.,'Forgot')]. We replaced 'text()' with 'dot'.
In Css we rewrite them as
css=a:contains('Forgot'), which will find the first anchor that contains 'Forgot'.
Some times we may need to work with URLs with href attributes. Xpath can also be used instead of finding the link text
//a[@href='url'] and using Css