Capture static control with XPath

Original XPath

In some cases, the XPath includes part that is always changed whenever loading page and it causes failure. As usual, you can use Wildcards to handle however XPath is not applied 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 pound (#) character to begin 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 “expand ” button on a tree:


Step 1: click expand button on node Sai Gon

//span[contains(text(),'Sai Gon')]//preceding-sibling::div

Explanation:

  • //span[contains(text(),'Sai Gon')] --> go to span node with text contains ‘Sai Gon’.
  •  //preceding-sibling::div -->  because the expanding button is in a div and span node Sai Gon are at the same level so we use preceding-sibling::div go navigate to expanding button.

Step 2: click expand button on node “1A Phan Xich Long”:

//span[contains(text(),'1A Phan Xich Long')]//preceding-sibling::div

Explanation: similar as 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
  • Then select span node with text contains ‘Juran’.