How to find Dynamic XPath in Chrome or Firefox using TruePath Web Extension tool - gsumit1/TruePath GitHub Wiki
Using the TruePath web extension tool, one can easily find XPath in Chrome or Firefox. This XPath Plugin dynamically generates multiple relative XPath, just on right click of the target element.
TruePath Supports:
- Page Object XPath,
- Relative XPath,
- Normal XPath format,
- XPath in iFrame,
- XPath in Frame,
- Indexed XPath,
- Multiple options for the XPath,
- Unique XPath,
- The capability to generate the XPath using text value,
- Easily use as Selenium locator, Selenium Page Object locator or Serenity Page Object locator.
How Can TruePath help You Finding Dynamic XPath?
Assume the username field of the Twitter Login page have the dynamic attributes and we can’t simply create XPath for it. We need to find the XPath of the Username field.
We can create the XPath of the textbox field with reference to the static field which has the low chance of the breakdown in the long run.
In this case, the static field would be Login to the Twitter label.
You can instantly get the relative XPath of the label using the TruePath as shown above. Now click on the XPath menu item, it then shows up a popup to select XPath with an option as Normal or Page Object XPath. Assume we copy the normal XPath.
Now, let's move the focus to the target field which in this case is the username.
As you can see, all the XPath for the text box started with the tag input. Noted down the tag.
XPath of the Log in to Twitter label: //h1[contains(.,'Log in to Twitter')] (copied earlier)
Starting tag of the username field: input
To construct the XPath for the username with respect to Log into the Twitter label, we can use XPath Axes, so the final XPath would be for
Username field: //h1[contains(.,'Log in to Twitter')]/following::input[1]
Here [1] signifies the first immediate input tag to the label.
Now we need to find the XPath of the Next field which is Password and assume all the attributes as dynamic.
Similarly, now move the focus to the target field which is in this case password and right click to note down the tag i.e., input(again in this case)
To construct the XPath for the password with respect to Log in to Twitter label, we can again use the XPath Axes, so the final XPath of the
Password field: //h1[contains(.,'Log in to Twitter')]/following::input[2]
Here [2] signifies the Second immediate input tag to the label.
TruePath also has built-in intelligence that if the element has long, auto-generated attributes such as id="abc123_ElementContains_Inputs_txtName" it will attempt to locate based on the final (and most significant) part of the value only.
TruePath works seamlessly for the following browsers:
- XPath in legacy Firefox versions,
- XPath in Firefox latest version,
- XPath in Firefox Quantum,
- XPath in Chrome
The above article also tries to answers some of the similar questions:
- This is how to find XPath in chrome for selenium
- This is how to find XPath in Firefox for selenium
- How to find the relative XPath in chrome
- How to find the relative XPath in Firefox
- How Can TruePath act as XPath Finder?
- How Can TruePath act as XPath Generator?
- How Can TruePath act as XPath Helper Chrome?