Introduction to XPaths
The purpose of this help page is to assist you in creating your own XPaths if you’re a beginner.
What is an XPath?
In Automation, if you’re working with Page Objects or if the elements on UI are not found by a standard Salesforce locator, you can use an XPath to find an element on the web page.
An XPath is defined as an XML path. It is a syntax or language for finding any element on a webpage using an XML path expression, provided the webpage has an HTML DOM structure.
The basic format of XPath is explained below:
An XPath contains the path of the element situated on the web page. The standard syntax for creating XPath is:
XPath=//tagname[@Attribute=’Value’]
- //: Select current node.
- Tagname: Tagname of the particular node.
- @: Select attribute.
- Attribute: Attribute name of the node.
- Value: Value of the attribute.
Creating XPaths
Basic XPath
XPath expressions select a node or a list of nodes from the XML document based on attributes such as ID, Name, and Classname.
For example:
In this example, the input tag has an id attribute of name_firstlea2. From this, we can create the following XPath:
//input[id='name_firstlea2']
As you build your XPath in a text editor, you can test whether it is pointing to the correct element using the Console tab of Google Chrome.
You can open this by:
- Pressing the F12 key on your keyboard, or
- Right-clicking anywhere on the page and selecting Inspect
After switching to Console, type $x(“your XPath”) and press Enter.
If the XPath points to any element, that element will be returned as a result.
For example:
< //input[@id=’name_firstlea2”] > is an xpath that you have to execute on the browser console. It will search for the input tag with id ‘name_firstlea2’ in HTML Dom. If that Xpath matches any condition in the same dom, it will return that element or elements. If you hover on that returned element, that element will get highlighted.
Using functions
Contains()
Contains() is a method used in XPath expressions. It is used when the value of any attribute changes dynamically or when the attribute value is partially static and partially changing with each execution, such as login information.
Contains() can find an element with partial text, as shown in the example below.
Here is an input tag where id=“opp3”:
When we enter the following XPath:
//input[contains(@id,'opp')]
We get the following result:
This shows the returned element on that particular Xpath that we are executing. In this case, the XPath matches the Opportunity name field.
Text()
Text() is another method used in XPath expressions. With Text(), we can find an element with an exact text match.
For example, here is a Description label tag:
When we enter the following XPath:
//label[text()='Description']
We get the following result:
XPath Axes
XPath Axes methods are used to find complex or dynamic elements. Below, we will see some of these methods. Refer to the Appendix below for more information on Axes methods.
Ancestor
The Ancestor axis selects the current node’s ancestor elements (grandparent, parent, etc.).
For example, in the screenshot below, we want to find the ancestors of the current node (label) with table tags:
When we enter the following XPath:
//label[text()='Description']/ancestor::table
We get the following result:
We may have a condition where our Xpath matches more than one element on the particular HTML Dom, so it will return all the elements that have matched the Xpath Condition. Two results are returned in this case because we have two ancestor tables with that node.
Following-Sibling
The Following-Sibling axis selects the following siblings of the context node. Siblings are at the same level as the current node.
For example:
When we enter the following XPath:
//label[text()='Assistant']/../following-sibling::td
We get the following result:
Parent
The Parent axis selects the parent of the current node. There are two ways to achieve this, using Parent or using “..”.
For example:
Using Parent:
v
Using “..”
Indexing
Indexing helps with selecting one node uniquely based on indexes.
For example, in the below screenshot, we can identify the node by index[1]:
Adding fields manually to a page object
To manually add fields to a Page Object, create a new Page Object.
Clicking the downwards arrow next to the New Test button in the top-left of Provar Desktop. Select New Page Object from the dropdown:
On the pop-up that appears, provide a Name for the Page Object and select a Connection:
Then click the Finish button. This creates the Page Object.
Finally, click the Java Source tab of the Page Object. This is where we can add new fields.
Example 1: Text Fields
To add a text/input field to the Page Object, first build the XPath by inspecting the field’s page in Chrome:
Add the following code snippet to the Page Object:
@TextType() @FindBy(XPath = "//input[@id='cas14']") public WebElement subject;
It should look as follows:
Example 2: Buttons
To add a button to the Page Object, first build the XPath by inspecting the button’s page in Chrome:
Add the following code snippet to the Page Object:
@ButtonType() @FindBy(XPath = "//input[@name='save']") public WebElement save;
It should look as follows:
Example 3: Hyperlinks
To add a hyperlink to the Page Object, first build the XPath by inspecting the hyperlink’s page in Chrome:
v
Add the following code snippet to the Page Object:
@LinkType() @FindBy(XPath = "//a[contains(@title,'Google Search')]") public WebElement googleSearch;
It should look as follows:
Example 4: Checkboxes and radio buttons
To add a checkbox or radio button to the Page Object, first build the XPath by inspecting the page in Chrome:
Add the following code snippet to the Page Object:
@BooleanType() @FindBy(XPath = "//input[@id='IsRecurrence']") public WebElement recurrence;
Example 5: Picklists and drop-downs
To add a picklist/dropdown field to the Page Object, first build the XPath by inspecting the page in Chrome:
Locating fields inside a frame
To locate an element inside a frame, write the below piece of code inside a Page Object:
@PageFrame() public static class Frame { @AnnotationType() @FindBy(XPath = "XPath expression for the element") public WebElement element; } @FindBy(XPath = "XPath expression for the frame") public Frame frame;
For example:
In this scenario, the code snippet should look as follows:
@PageFrame() public static class Frame { @ButtonType() @FindBy(XPath = "//input[@value='Save']") public WebElement saveButton; } @FindBy(XPath = "//iframe[@id='theIframe']") public Frame frame;
Locating fields inside a table
While locating an element inside a table, write the below piece of code inside a Page Object.
@PageRow() public static class TableName { @AnnotationType() @FindBy(XPath = "XPath expression for Element") public WebElement element; } @FindBy(XPath = "XPath expression for Table") @PageTable(firstRowContainsHeaders = true/false, row = TableName.class) public List<TableName> tableName;
For example:
In this scenario, the code snippet should look as follows:
@PageRow() public static class AccountTable { @LinkType() @FindBy(XPath = ".//th[1]//a") public WebElement accountName; } @FindBy(XPath = "//table[@class='list']//tr") @PageTable(firstRowContainsHeaders = true, row = AccountTable.class) public List<AccountTable> accountTable;
Appendix: Axes methods
XPath Axes methods are used to find complex or dynamic elements. The major XPath axes follow family tree terminology.
self:: is you.
Downward:
- child:: are your immediate children.
- descendant:: are your children, and their children, recursively.
- descendant-or-self:: (aka //): are you and your descendants.
Upward:
- parent:: is your mother or father.
- ancestor:: are your parent, and your parent’s parent, recursively.
- ancestor-or-self:: are you and your ancestors.
Sideways (consider elements earlier in the document to be younger):
- previous-sibling:: are your younger siblings, in age order.
- following-sibling:: are your older siblings, in age order.
- previous:: are your younger siblings and their descendants, in age order.
- following:: are your older siblings and their descendants, in age order.
- Provar Automation
- System Requirements
- Browser and Driver Recommendations
- Installing Provar Automation
- Updating Provar Automation
- Licensing Provar
- Granting Org Permissions to Provar Automation
- Optimizing Org and Connection Metadata Processing in Provar
- Using Provar Automation
- API Testing
- Behavior-Driven Development
- Consolidating Multiple Test Execution Reports
- Creating and Importing Projects
- Creating Test Cases
- Custom Table Mapping
- Functions
- Debugging Tests
- Defining a Namespace Prefix on a Connection
- Defining Proxy Settings
- Environment Management
- Exporting Test Cases into a PDF
- Exporting Test Projects
- Japanese Language Support
- Override Auto-Retry for Test Step
- Mapping and Executing the Lightning Article Editor in Provar
- Managing Test Steps
- Namespace Org Testing
- NitroX
- Provar Automation
- Provar Test Builder
- ProvarDX
- Refresh and Recompile
- Reintroduction of CLI License Check
- Reload Org Cache
- Reporting
- Running Tests
- Searching Provar with Find Usages
- Secrets Management and Encryption
- Setup and Teardown Test Cases
- Tags and Service Level Agreements (SLAs)
- Test Cycles
- Test Plans
- Testing Browser Options
- Tooltip Testing
- Using the Test Palette
- Using Custom APIs
- Callable Tests
- Data-Driven Testing
- Page Objects
- Block Locator Strategies
- Introduction to XPaths
- Creating an XPath
- JavaScript Locator Support
- Label Locator Strategies
- Maintaining Page Objects
- Mapping Non-Salesforce fields
- Page Object Operations
- ProvarX™
- Refresh and Reselect Field Locators in Test Builder
- Using Java Method Annotations for Custom Objects
- Applications Testing
- Provar Manager
- How to Use Provar Manager
- Provar Manager Setup
- Provar Manager Integrations
- Release Management
- Test Management
- Test Operations
- Provar Manager and Provar Automation
- Setting Up a Connection to Provar Manager
- Object Mapping Between Automation and Manager
- How to Upload Test Plans, Test Plan Folders, Test Plan Instances, and Test Cases
- Provar Manager Filters
- Uploading Callable Test Cases in Provar Manager
- Uploading Test Steps in Provar Manager
- How to Know if a File in Automation is Linked in Test Manager
- Test Execution Reporting
- Metadata Coverage with Manager
- Provar Grid
- DevOps
- Introduction to Provar DevOps
- Introduction to Test Scheduling
- Apache Ant
- Configuration for Sending Emails via the Automation Command Line Interface
- Continuous Integration
- AutoRABIT Salesforce DevOps in Provar Test
- Azure DevOps
- Running a Provar CI Task in Azure DevOps Pipelines
- Configuring the Automation secrets password in Microsoft Azure Pipelines
- Parallel Execution in Microsoft Azure Pipelines using Multiple build.xml Files
- Parallel Execution in Microsoft Azure Pipelines using Targets
- Parallel execution in Microsoft Azure Pipelines using Test Plans
- Bitbucket Pipelines
- CircleCI
- Copado
- Docker
- Flosum
- Gearset
- GitHub Actions
- Integrating GitHub Actions CI to Run Automation CI Task
- Remote Trigger in GitHub Actions
- Parameterization using Environment Variables in GitHub Actions
- Parallel Execution in GitHub Actions using Multiple build.xml Files
- Parallel Execution in GitHub Actions using Targets
- Parallel Execution in GitHub Actions using Test Plan
- Parallel Execution in GitHub Actions using Job Matrix
- GitLab Continuous Integration
- Travis CI
- Jenkins
- Execution Environment Security Configuration
- Provar Jenkins Plugin
- Parallel Execution
- Running Provar on Linux
- Reporting
- Salesforce DX
- Git
- Version Control
- Masking Provar Credentials on CI
- Salesforce Testing
- Best Practices
- Improve Your Metadata Performance
- Java 21 Upgrade
- Salesforce Connection Best Practices
- Testing Best Practices
- Automation Planning
- Supported Testing Phases
- Provar Naming Standards
- Test Case Design
- Create records via API
- Avoid using static values
- Abort Unused Test Sessions/Runs
- Avoid Metadata performance issues
- Increase auto-retry waits for steps using a global variable
- Create different page objects for different pages
- The Best Ways to Change Callable Test Case Locations
- Working with the .testProject file and .secrets file
- Best practices for the .provarCaches folder
- Best practices for .pageObject files
- Troubleshooting
- How to Use Keytool Command for Importing Certificates
- Installing Provar After Upgrading to macOS Catalina
- Browsers
- Configurations and Permissions
- Connections
- DevOps
- Error Messages
- Provar Manager 3.0 Install Error Resolution
- Provar Manager Test Case Upload Resolution
- Administrator has Blocked Access to Client
- JavascriptException: Javascript Error
- macOS Big Sur Upgrade
- Resolving Failed to Create ChromeDriver Error
- Resolving Jenkins License Missing Error
- Resolving Metadata Timeout Errors
- Test Execution Fails – Firefox Not Installed
- Selenium 4 Upgrade
- Licensing, Installation and Firewalls
- Memory
- Test Builder and Test Cases
- Release Notes