Selenium Said it clicked, but nothing happened?

I had this happening to me couple of days back.

The Issue

There is a check box in my web application. I have to check that in order of the submit button to get enabled. Click on the submit button after that and done. Simple isnt it? I know you will not say yes – because I kind of already given out the hint in my post title itself.

No, before you start assuming that the problem could be with identifying the webelement property in DOM, let me tell you that both the elements – checkbox as well as the Submit button had the proper  “ID” property associated with it. No issues in object identification at all.

My Test script had the object property added as well as necessary steps to click on checkbox and click on submit button. I ran the script. It failed!!

When i saw the report, it was mentioned that the script clicked on checkbox and then on Submit button (i was so confident that the submit button will get enabled that i had not put a precondition to the script to check if button is enabled or not before clicking on it). Script assumed that it is in next screen now and tried clicking some link in that new screen, as told to it in next step. But it timed out waiting for the presence of that link! I checked the screenshot of failure step and found that it has not moved to next screen. Instead it is still in the same screen where the submit button is present. Neither the checkbox got clicked, nor the Submit button is enabled!!

Assumptions…. what went wrong…?

I read and re-read the HTML report again. Selenium has promptly printed that it is done with clicking on the 2 elements. Then why in application nothing happened?? I was puzzled. object with standard ID property. So there cannot be any possibility in identification of object, right??

No. That is exactly where i went wrong.

Finally, here it is… the root cause for the issue…

The problem was with object identification!!

I spent some 30 mins debugging script. Everything seemed to be fine. Reluctantly, i then decided to test the object in Chrome’s console. I composed a xpath using the id property of checkbox. When i tested the xpath, Chrome returned me 5 objects!!! Not 1 or 2.. 5 objects in the same screen had same id property!!

The checkbox i wanted to click was the 3rd in that list. What about the rest? There was only one checkbox in the app screen that was visible to me. It was the  “I accept” checkbox. No other check box visible to me. All are hidden objects. So my test script during execution clicked on first check box(hidden) and told me it did the job!!

So finally i learnt a new thing on that day. Even if you see that the object has any of the standard 9 property associated with it, better test it out in chrome console before you add it into your repository.

Ok… let’s summarize now…

Below i’m giving the tech details of the object. I would say this can be best practice to identify the object. Instead of directly adding the object to the repository after analyzing the DOM, it’s always better to test it out in console or firepath…

  1. The object had id=”someCheckBox” in DOM
  2. I created xpath as //div[@id=’someCheckBox’]
  3. Tested on chrome console
  4. Chrome returned 5 objects matching the xpath

So finally, in order to select the one that i wanted the script to click, i created the xpath for the 3rd checkbox as  (//div[@id=’someCheckBox’])[3].

Now my script clicked on the desired checkbox and all was well.

Hope you find this interesting and helpful in your selenium journey. Happy Testing!