Selenium IDE- Locating Strategies (By XPath)
- XPath is a language used for locating nodes in XML documents.
- XPath can be used as a substitute when you don’t have a suitable id or name attribute for the element you want to locate.
- XPath provides locating strategies like:
- XPath Absolute
- XPath Attributes
XPath Absolute (not recommended)
- XPath Absolute enables users to mention the complete XPath location from the root HTML tag to the specific elements.
- It is not recommended because it takes the complete XPath location and if the location of the web element is changed or it belongs to some other parent then XPath will be unable to locate the desired element.
- Syntax: //html/body/tag1[index]/tag2[index]/…/tagN[index]
- Example: //html/body/div[2]/div/div[2]/div/div/div/fieldset/form/div[1]/input[1]
XPath Attributes
- XPath Attributes is always recommended when you don’t have a suitable id or name attribute for the element you want to locate.
- XPath Attributes allows us to locate web elements using their different attributes.
- Syntax: //htmltag[@attribute1=’value1′ and @attribute2=’value2′]
- Example: //input[@id=’passwd’ and @placeholder=’password’]
Let us consider an example on how to use XPath as a target for commands in Selenium IDE.
We will create a login test, so that you could get a better understanding of locating elements through XPath. Since, XPath Absolute is not recommended therefore we will use XPath Attributes in order to locate web elements.
- Launch Firefox browser.
- Click on the Selenium icon present on the top right corner on your browser.
- It will launch the default interface of Selenium IDE.
- Click on the command text box present on the Test Script Editor Box.
- Modify the properties of first command as:
- Command: open
- Target: https://www.testandquiz.com/user-login
- After execution, this command will load the Test and Quiz login page on your Firefox browser.
For second command to be entered, we need the identification elements for the “Username” text box which will help the IDE to identify the target location.
-
- Right click on the Username text box and select inspect elements.
-
- It will launch a window containing all the specific codes involved in the development of “Username” text box.
Thus, we can see that the identification elements involved in the development of “Username” text box include: id, class, name and placeholder.
Therefore, we will write the XPath Attribute for “Username” text box as: //input[@id=”email” and @placeholder=”Email”]
- Modify the properties of second command as:
- Command: click at
- Target: //input[@id=”email” and @placeholder=”Email”]
- Upon execution this command will click on the “Username” text box.
We will use the same XPath Attribute for our next command which involves typing the username as a login credential in the “Username” text box.
- Modify the properties of third command as:
- Command: type
- Target: //input[@id=”email” and @placeholder=”Email”]
- Value: username
- After execution, this command will type the username in the “Username” text box.
We will now add a command that will click on the “Password” text box. For this command, we need the identification elements for the “Password” text box which will help the IDE to identify the target location.
-
- Right click on the “Password” text box and select Inspect Element.
-
- It will launch a window containing all the specific codes involved in the development of “Password” text box.
Thus, we can see that the identification elements involved in the development of “Password” text box include: id, class, name and placeholder.
Therefore, we will write the XPath Attribute for “Password” text box as: //input[@id=”password” and @placeholder=”Password”]
- Modify the properties of fourth command as:
- Command: click at
- Target: //input[@id=”password” and @placeholder=”Password”]
- After execution, this command will click on the “Password” text box.
We will use the same XPath Attribute for our next command which involves typing the password as a login credential in the “Password” text box.
- Modify the properties of fifth command as:
- Command: type
- Target: //input[@id=”password” and @placeholder=”Password”]
- Value: ******* (User Login Password)
- After execution, this command will type the user login password in the “Password” text box.
At last, we need the identification elements for the Log in Submit button which will help the IDE to identify the target location.
-
- Right click on the “Log In” button and select Inspect Element.
-
- It will launch a window containing all the specific codes involved in the development of “Log In” submit button.
Therefore, we will write the XPath Attribute for “Log In” button as: //button[@class=”btn btn btn-primary” and @type=”submit”]
- Modify the properties of sixth command as:
- Command: click at
- Target: //button[@class=”btn btn btn-primary” and @type=”submit”]
- After execution, this command will click on the “Log In” submit button.
- Click on the “Run Current Test” button present on the tool bar menu of the IDE. It will execute all of your inserted commands on the browser and gives you an overall summary of the executed test script.
- The Log pane displays the overall summary of the executed test scripts.
Thus, you can see that we have used XPath locator as a target for most of our Selenium commands.