Handling Complex And Dynamic Elements In Selenium Using Xpath
Posted By : Neha Dahiya | 28-Feb-2018
Basic Xpath :
Based on different type of attribute like ID, Name, Class Name, CSS Selector etc. Xpath is to select list of nodes and nodes from the XML Document as follows:
Xpath= //input[@name='passFirst0']
Some more examples of basic Xpath:
Using And & Or Expression
AND expression is used where both the conditions should be true to find an element. It fails if any of the conditions
Xpath=//input[(@type='image') and (@name='login')]
OR expression is used where one of the conditions should be true or both the conditions can be true to find an element.
//input[(@type='image') or (@name='login')]
Contains()
Contains() method is used to find the element whose attribute value changes dynamically. Contains() method can also use partial text value to find the element.
Consider the example below:
Here, in the above example element is identified using partial text value. In the XPath value “Atlanta” is used instead of “Atlanta to Las Vegas”. Observe in the image above that element is identified successfully “1 of 1”.
Following-sibling:
Following-sibling selects the element which follows to the context node. Elements at the same level as of the current node are siblings. Following siblings will find the element which comes after the current node.
XPath=//a[text()='Home']/../../../following-sibling::tr[1]//a[text()='Flights']
Preceding:
Preceding identifies all the elements that are before the current node, consider the following example: it identifies the element “Home” which is present before “Flights”.
Xpath=//a[text()='Flights']/preceding::a[text()='Home']
Thanks!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Neha Dahiya
Neha is a bright QA Engineer with skills in manual testing . Apart from finding bugs in application, she loves sketching and painting.