Generally, the GUI objects will be identify by the locators id, name, class etc., however it is very difficult to identify the dynamic object on the DOM. Dynamic Object Model (DOM) is an application programming interface (API) for HTML and XML documents, it defines the logical architecture as shown above.
There are few techniques available in XPath where in you can easily identify the GUI dynamic objects without any waste of time. Refer the above snapshot to under the below example
Syntax = //tagname[@attribute=’Value‘]
Example = //input[@id=’user-message‘]
There are two type of Xpaths:
- Absolute Xpath – Refers to complete path of an object on the page, denotes by single slash i.e., / Example: /html/body/div[2]/div/div[2]/div[1]/div[2]/form/div/input
- Relative Xpath – Refers to a path of an object on the page from a landmark, denotes by double slash i.e., // Example: //form/div/input
Few Xpath Keywords
- Contains - //tagname[contains(@attribute, ‘value‘)] – Checks for the value given in the code on your web page
- Starts-with - //tagname[starts-with(@attribute, ‘value‘)] – Checks for the value given in the code that starts with on your web page
- Multiple Xpaths - //tagname[@attribute=’Value‘]//tagname[@attribute=’Value‘] – Checks for the values given in both the xpaths code on your web page
- Operator “or” - //tagname[@attribute=’Value‘ or @attribute=’Value‘] – Checks for the values given in the code that is there on your web page at least one value from both of them
- Operator “and” - //tagname[@attribute=’Value‘ and @attribute=’Value‘] - Checks for the values given in the code that is there both the values on your web page
- Text - //tagname [text()=’text value‘] - Checks for the value given in the code that is present on your web page
- Ancestor - //*[@class=’ClassName’’]//ancestor::div - First, it finds the class which class is “ClassName” and then, starts finding div elements in the page.
- Following - //tagname[@id=’idName’]//following::input - First, it finds the form which id is “’idName” and then starts to find all input elements after that node.
- Child - //*[@class=’ClassName’]//ul[@id=’idName’]/child::li - Selects all children elements of the current node.
- Preceding - //img[contains(@src,’srcName’)]//preceding::li - First, will locate the bottom element, then use preceding with “li” to find all “li” elements
- Following-sibling - //*[@class=’className’]/child::div[2]//*[@class=’className’]//following-sibling::li - First, will locate child div elements, and then li elements of following li siblings
- Descendant - //*[@class=’className’]//*[@id=’idName’]//descendant::li - First, will locate className, and then descendant li elements
- Parent - //*[@id=’idName’]/button//parent::form - Finds the parent name of the button
- Locate an Element inside Array of Elements –
- //span[contains(text(),’tableValue’)]
- (//span[contains(text(),’tableValue’)])[1]
- (//span[contains(text(),’tableValue’)])[1]/following-sibling::strong[@class=’className’]
No comments:
Post a Comment