Monday, May 18, 2020

Mastering of XPath with in Selenium

How to Identify Dynamic Web Elements?

  1. Absolute Path method
  2. Use Relative XPath using contains or starts with text
  3. Identify by index - driver.findElements(By.xpath(//*[contains(@id, ‘submit’).get(0).click();
    Here get(0) is used to get the first web element with matching XPath.
  4. Use Multiple attributes to locate an element ( and, or)


Xapath:
XPath is defined as XML pathIt is a syntax or language for finding any element on the web page using XML path expression.

  • // : Select current node.
  • Tagname: Tagname of the particular node.
  • @: Select attribute.
  • Attribute: Attribute name of the node.
  • Value: Value of the attribute.
XPath is a path written using html tags(or XML tags) and their attributes to reach to a particular node (or web element) in an HTML page or XML page.

Types of Xpath:
  • Relative xpath -  It starts with the double forward slash (//), which means it can search the element anywhere at the web page.
    Basic Syntax: //htmlTagname[@attribute=’value’]
  • Absolute xpath - The key characteristic of XPath is that it begins with the single forward slash(/) ,which means you can select the element from the root node. Basic Syntax: html/body/table/tbody/tr[2]/td/input

Methods in Xpaths:

Contains():

Contains is a method used when the value of any attribute changes dynamically. It can search an element with partial information.
Example: To search a button xPath used can be: //input[contains(@type=’submit’)]
Example 2: Xpath of a below displayed web element using contains method can be written like:
//input[contains(@name=’email’)] or //input[contains(@name=’ema’)]

Both above examples will work because contains uses partial matching.

Example 3: Using another method text() as an argument to the contains() method in XPath.
//a[contains(text(), ‘Mobile & Accessories’)]

Above xpath will search for the links which has a text Mobile & Accessories as the linktext.

Starts-with()

This method can be used when we are searching for web elements matching the start of the text of the attributes passed. The text() method can also be used which will match the starting of the text.
Syntax: For above example, xpath can be written using starts-with as 
//a[starts-with(text(),’Flight’)]

Text() –

This method is used when we are searching for elements matching the exact text.
//a[text()=’Mobile & Accessories’]

Operators in xpath:

AND and OR is two operators available in xpath.

AND operator, when applied to multiple attributes, identifies a web element only when all the attributes are pointing to that element.
Syntax: //input[@type=’text’  and @name=’uid’]

This xpath will identify an input web element which is of type text and name as uid.

OR operator, when applied to multiple attributes, defines a web element only when any one of the attributes points to that element:
Syntax: //input[@type=’text’  or @name=’uid’]

This xpath will identify an input web element which is either of type text or name is “uid.”

AXES in Selenium WebDriver:

Axis in xpath points the xpath processor to the direction in which it should navigate in the hierarchy of the html nodes.

Basic Syntax:
//html_tag[@attribute=’value’]//axes::html_tag

Frequently used Axes in XPath:

  1. Ancestor – contains ancestor of the context node.
  2. Child – includes the child of the context node.
  3. Following – select the elements which follow after the context node.
  4. Preceding – choose the elements which precede before the context node.
  5. Following-Sibling – choose the sibling coming after the context node.
  6. Preceding-Sibling – choose the sibling coming before the context node.
  7. Parent – contains the parent of the context node.

No comments:

Post a Comment