Â
- An absolute XPath expression starts with a single forward slash ("/").
- It describes the complete path from the root element to the target element. Example: /html/body/div[1]/div[2]/input
- A relative XPath expression starts with a double forward slash ("//").
- It allows you to search for an element anywhere in the document, regardless of its position. Example: //input[@id='username']
What is Dynamic XPath in Selenium Webdriver?
In Selenium, XPath is used to locate elements on a web page. XPath can be used to locate elements based on their tag name, attribute value, or their position in the DOM (Document Object Model).
Dynamic XPath locators are used to locate elements that do not have a static, static id, or name attribute.
There are a few different dynamic XPath locators:
1. Using the contains() function: The contains() function is used to locate elements based on a partial match to their attribute value.
For example, the XPath to locate the element with the attribute value "username" would be
//*[contains(@name, 'username')]
2. Using the starts-with() function: The starts-with() function is used to find elements based on the starting characters of their attribute value.
For example, to locate an element with the attribute value "username" that begins with "user", the XPath would be
  //*[starts with (@name, 'user')
3. Using the end-with() function: The end-with() function is used to locate elements based on the last characters of their attribute value.
For example, to locate an element with the attribute value "username" that ends with "name", the XPath would be
  //*[ends (@name, 'name')]
4. Using parent-child relationships: Parent-child relationships are used to locate elements based on their relationship to other elements in the DOM.
For example, to locate an element that is a child of a specific element, the XPath would be
  //parent-element/child-element
5. Using the text() function: The text() function is used to find elements based on their inner text.
For example, the XPath to locate an element containing the text "submit" would be
//* [text()='submit']
6. Using following-sibling: Following-sibling is used to locate elements based on their sibling element.
For example, to locate an element that is following a specific element's sibling, the XPath would be
//preceding-sibling-element/following-sibling::element
7. Using the ancestor-descendant relationship: The ancestor-descendant relationship is used to locate elements based on their relationship with other elements in the DOM.
For example, to locate an element that is a descendant of a specific element, the XPath would be
//ancestor-element //descendant-element
8. Using predecessor-siblings: predecessor-siblings is used to locate elements based on their sibling element.
For example, to locate an element that is a preceding sibling of a specific element, the XPath would be
//following-sibling-element/preceding-sibling::element
9. Using OR operator: OR operator is used to find the elements based on multiple conditions.
For example, the XPath to locate an element with the attribute value "username" or "email" would be
//* [@name='username' or @name='email']
10. Using AND operator: AND operator is used to find the elements based on multiple conditions.
For example, to locate an element with attribute value "username" and attribute class "form-control", the XPath would be
//*[@name='username' and @class='form-control']
11. Using position: Position is used to locate elements based on their position in the DOM.
For example, to locate the third element of a specific type, the XPath would be
//tagname[3]
12. Using last(): The last() function is used to locate the last element of a specific type in the DOM.
For example, to locate the last element of a specific type, the XPath would be
// tagname [last()]
Post a Comment