Selenium Notes
Selenium Notes
Selenium Notes
By.xpath
What is Xpath ?
Xpath is defined as XML path.
It is a syntax or language for finding any element on the web page using
xml path expression.
Xpath is used to find the location of any element on a webpage using
HTML DOM structure
Xpath is can used to navigate through elements and attributes in DOM
Type of Xpath
1. Absolute Xpath/ Full Xpath
In absolute xpath we need to navigate form the root tag through each
and every node till that element found in that particular node
Absolute xpath starts from root node
Absolute xpath start with /
In Absolute xpath we use only tags/nodes
Absolute xpath is full path of xml path
/html/body/div[1]/div/div/[3]/div[1]/img
3. Contains() or Starts-with()
Contains is a method which will verify then attribute value is a part of
actual values then it will locate the element
Syntax: tagname[contains(@attribute,’value’)]
Ex: input[@id=’firstname’]….basic xpath
Input[contains(@attribute,’first’)]……..using contains()
//input[@id=’start’]
//input[@id=stop]
//input[contains(@id,’st’)]……….Dynamic xpath
//input[starts-with(@id,’st’)]……….Dynamic xpath
(//li[contains(text(),'8 GB RAM | 128 GB ROM | Expandable Upto 256 GB')] )
[1]/../../..//div[text()='realme 9 (Sunburst Gold, 128 GB)']
4. Text()
Text() is used to capture the text present in html node and also to check
the equality and to locate text element
Ex://a[text()=’Women’]
5. Chained xpath
Chained xpath used to xpath forward by another xpath
(we can capture from parent to child if similar element node
present with the help of number )
Ex: //form[@id=’searchbox’]//input[@id=’search_query_top’]
or
Ex: //form[@id=’searchbox’]//input[4]
Xpath Axes:
Self:
Current html node which we select or current html tag
Child:
Traverse all child element of the current html tag
Syntax: //*[attribute=’value’]/chid::tagname
Parent:
Traverse parent element of the current html tag
Syntax: //*[attribute=’value’]/parent::tagname
Following:
Traverse all the element that comes after the current tag
Syntax: //*[attribute=’value’]/following:: tagname
Preceding:
Traverse all nodes that comes before the current html tag
Syntax: //[attribute=’value’]/preceding:: tagname
Following-sibling :
Traverse from current html tag to next sibling html tag
Syntax: //current html tag[@attribute=;’value’]/following-sibling::previous
tag[@attribut=’value’]
Preceding-sibling:
Traverse from current html tag to previous sibling html tag
Syntax: //current html tag[@attribute=;’value’]/preceding-sibling::previous
tag[@attribut=’value’]
Ancestor:
Traverse all the ancestor elements (grandparent, parent, etc) of the
current html tag
Syntax: //*[attribute=’value’]/ancestor:: tagname
Descendant:
Traverse all descendent element (child nod , grandchild node, etc.) of
the current html tag
Syntax: //*[attribute =’value’]/Descendant::tagname