Selenium IDE XPath Example

Home/Selenium/Selenium IDE XPath Example

Selenium IDE XPath Example

If you want to use XPath to locate elements with the Selenium IDE you have basically three options to figure out the XPath definition you have to use for your test.

  1. Use the built in record function of the Selenium IDE with XPath Locator Builders.
  2. Use a plugin to figure out the XPath of a specific element and use the result in the Selenium IDE.
  3. Analyse the HTML-structure and figure out the XPath yourself.

I will provide examples for the first two options since the third option is not really practical to work with.

 

Use the built in record function of the Selenium IDE with XPath Locator Builders

  1. Open Firefox and the Selenium IDE.
  2. Go to “Options”>”Options…” and switch to the tab “Locator Builders”.
  3. Here move via drag and drop the two entries “xpath:attributes” and “xpath:idRelative” to the top of the list as you see in the screenshot.
    Selenium IDE Locator Builders
  4. Click “OK”.
  5. Now click on the red record button on the right side of the Selenium IDE to start recording.Click To Record
  6. Switch to the main Firefox browser window and navigate to https://en.wikipedia.org
  7. Type into the search field: “Selenium” and hit enter or click on the search button.
  8. Now switch back again to the Selenium IDE window and click the red button to stop recording.
  9. Your recorded test should look something like you see the sample in the screenshot. As you can see the recorder used to locate the search box and the search button relative XPaths.Recorded XPath Results
  10. If you hit the button to play the current test cases, the test should pass by using all the XPaths to locate the elements.Test Passed Sample 1

 

Use a plugin to figure out the XPath of a specific element and use the result in the Selenium IDE

  1. First you need to install some kind of XPath plugin in the browser of your choice that can help you to find out XPaths of elements on any site you are currently surfing on. For this example we are using the XPath Helper with Google Chrome.
  2. After you installed XPath Helper in Chrome you should see the Icon with your other active plugins.
  3. In Chrome navigate to https://en.wikipedia.org
  4. Click on the icon of XPath helper to launch it.Start XPath Helper
  5. Hold the shift button while you hover with the mouse pointer over the search input field.
  6. Now you should see the full XPath of the search input field in the black box of the XPath helper.
  7. Starting at the end of the XPath select as much of it as you think is necessary to have a unique XPath to the desired element. It is important that your selected fraction of the XPath always starts with a slash (“/”). Since the site probably only has one “searchInput” we only select the small portion:”/input
[@id=’searchInput’]”Using XPath Helper
  • Copy it.
  • Switch to the Selenium IDE window and start a new test case.
  • In the Base URL field put “https://en.wikipedia.org/” and as first command use: “open” with the target: “/”
  • As second command use “sendKeys” and paste in the target-field your copied XPath fraction. At the beginning of the XPath fraction you need to add a second slash (“/”). So the full input for the target field should be: “//input[@id=’searchInput’]”
  • As third command use “clickAt”. For the target field you need to find out the XPath of the search submit button for the search input field. To do this switch back to your Chrome browser and use the XPath helper again in the same way as you did before for the search input field. Your target field should contain at the end: “//input[@id=’searchButton’]”
  • Now your test case is ready to run and should send you to the Wikpedia article of Selenium.
  • Note: The double slash at the beginning of the XPath means you are using relative XPaths. It will also work if you use longer fractions of the XPath than we chose in our example. Longer fractions will also be unique but the longer the fraction is the more likely it is to be subject to any changes on the site an break your test suits.

    By |September 28th, 2015|Selenium|1 Comment

    One Comment

    1. Dasagriva manu April 4, 2018 at 7:05 pm - Reply

      Nice post. Sometimes it’s very useful to create a complex script using IDE and then export it in webdriver format. It saves a lot of time.

    Leave A Comment Cancel reply

    Toggle Sliding Bar Area