Capture static control with XPath
Original XPath
In some cases, the XPath includes a part that is always changed whenever loading the page and it causes failure. As usual, you can use Wildcardsdoes to handle however XPath is not apply this method. Therefore, you need to use some special expressions.
“starts-with” expression
Assuming the 0_0_0_7_2_4 is changed part, you can re-write XPath as
“ends-with” expression
Assuming the t_0_0_0_7 is changed part, you can re-write XPath as
“contains” expression
Assuming the 0_7 is constant, the rest is changed part, you can re-write XPath as
Use dynamic control with XPath
You can follow this topic to know how to get dynamic identifier of window/control.
After that, you must use “setting – escape sequences = yes”.
Without variable
With variable
You need to:
- Add one more backslash (\) character before each existing one
- .
- Use the pound (#) character to begin the expression and separate constant string and variable by ampersand (&) character.
Xpath reference
https://www.w3schools.com/xml/xpath_intro.asp
https://www.guru99.com/xpath-selenium.html
Xpath Samples
Example: click the “expand ” button on a tree:
Step 1: click the expand button on node Sai Gon
//span[contains(text(),'Sai Gon')]//preceding-sibling::div
Explanation:
- //span[contains(text(),'Sai Gon')] --> go to the span node with text containing ‘Sai Gon’.
- //preceding-sibling::div --> Because the expanding button is in a div and span node Sai Gon is at the same level so we use preceding-sibling::div go navigate to expanding button.
Step 2: click the expand button on node “1A Phan Xich Long”:
//span[contains(text(),'1A Phan Xich Long')]//preceding-sibling::div
Explanation: similar to above
Step 3: Click on node floor 3rd Juran:
//span[contains(text(),'1A Phan Xich Long')]//following-sibling::ul//span[contains(text(),'Juran')]
Explanation:
- //span[contains(text(),'1A Phan Xich Long')]//following-sibling::ul/li --> From node Phan Xich Long, open the following node type ul, then open child node li
- the Then select span node with text containing ‘Juran’.