Selenium IDE Click Doesn’t Work?

Did Selenium ID record all the clicks, but one or more of your click commands are not working? We’ve written this guide to help.

Published Categorized as Web & Code

Sometimes, when playing back a recorded test case in Selenium IDE, the click command may not work on one or more elements, even if the test case was recorded correctly and all of the clicks were captured in steps.

This issue is very common in Software-as-a-Service (SaaS) apps where the HTML structure and the DOM element IDs tend to change after every refresh. The good news is that there is a way to work around this, and I’ll show you how to do it in this tutorial.

Why Click Commands Don’t Always Work

Suppose you and I were creating a test case for Google’s search results page where the user:

  1. Visits google.com
  2. Searches for “what happened today”
  3. Navigates to the “News” tab
  4. Sees news results

This is what the user journey looks like:

1. Open google.com
2. Search for “what happened today”
3. Navigate to the “News” tab
4. See results for news

And this is what our recorded Selenium IDE test case looks like:

Example test case in Selenium IDE

At first glance, Selenium IDE recorded everything. So why is the click command not executing when we play the test case again?

Look at the “Target” field carefully. Selenium IDE recorded a relative XPath for the DOM element of the “News” tab. But at a closer look, there’s one problem: the ID of that DOM element changes with every page view.

In other words, the execution of our test case assumes that the ID of that element will always be the same, when it actually changes every run. If we want the test case to execute consistently, we need to target that DOM element differently.

How to Get Click Commands to Work

Target the Element Differently

Nine times out of ten, the solution is simple: When the click command in a step in your Selenium IDE test case is failing, simply select an alternative way to target the DOM element being clicked from the “Target” dropdown.

Selecting alternative ways to target a DOM element

Since the cause of the problem is almost always a front-end with changing IDs and Class Names, select an alternative that’s most likely to stay consistent from page view to page view and try again.

In the case of our example, the two approaches that worked were:

  • linkText (linkText=News)
  • xpath:link (xpath=//a[contains(text(),'News')])
  • xpath:position (xpath=//div[2]/a)

Those that didn’t work, on the other hand, were approaches that relied on IDs, Class Names, or URLs — all of which change as the page opens anew (or refreshes) in the browser.

Target the Element With a CSS Selector

If you’re fluent in CSS, you can also target the DOM element with a CSS selector by entering the following into the “Target” field:

css=SELECTOR

Where “css=” is the prefix that tells Selenium IDE that this is a CSS selector, and “SELECTOR” is your selector.

If you want to become fluent in CSS selectors, we’ve written two guides that will help:

By Dim Nikov

Editor of Maker's Aid. Part programmer, part marketer. Making things on the web and helping others do the same since the 2000s. Yes, I had Friendster and Myspace.

Leave a comment

Your email address will not be published. Required fields are marked *