Monday, 28 July 2014

Click menu element in Selenium is not always reliable

Have just gone through an issue that similar as this one reported on stackoverflow http://stackoverflow.com/questions/21028145/selenium-doesnt-click-menu-element-even-though-its-there and it is about click a menu element in Selenium for automating the web application testing. The element is found as per debug shows, however, the click action was not performed on the actual element but the menu option above. Googled through many sites and found this issue seems related to IE web driver and particularly to the menu that structured to show sub menu by clicking the parent menu option (same in my case).

It is not a timing issue as the click action works fine in other browsers and there was no exception on cannot find the element. The click action was not correctly performed at first click but it seems working if to be clicked more than twice and usually, the third time works correctly. However, as for building the automation testing, this solution is not good. Unfortunately, IE is our major browser to test the application against and cannot move away. So at the end, I found a solution is to use Actions class that to manually move the cursor several pixels down in order to perform the click action correctly at the first attempt. The code would look like below:

actions.moveToElement(menuElement, 0, 20).click().perform();

In my case, I only had problem with one menu option. So for a quick solution, I hard-coded the name of the menu option to perform the above special click action. It worked for me although it is not perfect. I will continue looking for better solution to cope with issue or hope the next version of IE web driver would fix this.