As we have described before how to find XPath in the browser, here we are going to describe different dynamic XPath functions.
What Is Dynamic XPath In Selenium?
XPath, or XML Path, is one of Selenium WebDriver’s most commonly used locators for navigating a page’s HTML structure. It can locate any element in a web page using HTML DOM structure in HTML and XML documents.
XPath allows XML document navigation to select individual elements, attributes, or other parts of an XML document for specific processing. For example, XPath generates reliable locators but is slower in terms of performance than CSS Selector.
The language XPath is used to select elements in an HTML page. Using XPath, you can find any element on a page based on its tag name, ID, CSS class, etc. In Selenium, there are two types of XPath. Dynamic XPath is also called as custom XPath and it is one way to locate element uniquely.
Dynamic XPath is used to locate exact attribute or decrease the number of matching nodes/result from a webpage and following XPath expressions can be used for the same:
- Contains
- Sibling
- Ancestor
How to find XPath for dynamic elements in selenium?
XPath axes from the current node are utilized to locate dynamic elements in Selenium, allowing the search for nodes in the XML document. This is crucial for finding nodes closest to the tree. XPath axes provide methods for locating dynamic elements that standard XPath methods might miss, especially when identifiers like ID, Classname, or Name are absent.
In Selenium WebDriver, axes methods such as child, parent, ancestor, sibling, preceding, self, etc., are commonly employed to handle dynamically changing elements.
Modifying test scripts due to changes in the Application Under Test (AUT) is a challenging task in test automation. Developers frequently alter identifiers, and elements may dynamically change during execution.
Automation testers should avoid setting fixed XPaths for test case elements to overcome these challenges. Instead, they should dynamically script XPaths based on specific patterns, ensuring adaptability to changes in the AUT.
Contains
Contains is used to locate the web element who matches the specific text from multiple blocks.
As per the below image, if in your web page, there are sections that have the same element for all row, then you can find the specific element by using Contains to select text in that row.
Example:
.//*[@class='product']//h4[contains(text(),'Text')]//ancestor::div[@class='table-good']
.//*[@class='product']//h4[contains(.,'Text')]//ancestor::div[@class='table-good']
Sibling
As the meaning of sibling, we can use this to find an element which is related to some other element. There are basically two types of sibling function which are used in XPath.
A) Preceding Sibling - If we select one sibling from given list, the “preceding sibling” function takes the preceding options of the selected one.
Example:
//ul[@class="OPENCR_flex_containerleft"]//li[@class="OPENCR_flex_box-other"]//select[@name="sort"]//option[contains (text(), 'by title')]/preceding-sibling::option
B) Following Siblings - If we select one sibling from given list, the “following sibling” function takes the following options of selected one. Example:
//ul[@class="OPENCR_flex_containerleft"]//li[@class="OPENCR_flex_box-other"]//select[@name="sort"]//option[contains (text(), 'by title')]/following-sibling::option
Ancestor
We can use this to find an element on basis of the parent element. As per below image, if in your web page there is a section which has a same element for all the rows then you can find one row element by using ancestor.
Frequently Asked Questions
Selenium handles dynamic links using techniques such as relative XPaths or unique attributes. These methods ensure flexibility in identifying links even when the webpage structure changes.
Selenium checks dropdown values by navigating to the webpage, locating the dropdown element, and verifying its enabling, visibility, and ability to allow multiple selections.
To get all links on a webpage, Selenium navigates to the desired page, retrieves a list of 'a' tag elements, and iterates through the list, printing link text and addresses.
Handling dynamic dropdowns involves actions like triggering dropdowns by typing, selecting options, and adapting to changes in the dropdown menu dynamically.
Dynamic web elements are identified using techniques like XPath with 'contains' or 'starts-with,' allowing flexibility in locating elements that may change the structure.
XPath is a powerful web locator in Selenium used to navigate XML documents and locate elements based on various attributes like tag name, ID, or class.
Apart from XPath, Selenium uses various locators like ID, Name, Class Name, Tag Name, Link Text, and Partial Link Text to identify and interact with WebElements.
Absolute XPath begins from the root, while Relative XPath starts from the current node. Relative XPath is preferred for flexibility and adaptability to changes in the webpage structure.
XPath is versatile and can be dynamically adapted to locate changing or dynamic WebElements using techniques like 'contains,' 'starts-with,' or other flexible approaches.