Clicking on an element in Phantom.Js using Javascript and Webdriver

We came across a problem where it was not possible to click on an element on a webpage using normal Selenium Webdriver as, for some reason, it was present but not visible. In Firefox the element was visible but for some reason when running Phantom.Js the element was treated by Webdriver as if it was not visible.

We decided to try and click on the same element using javascript. Initially we tried using the .click method. However this would not work – when we ran the code and stepped through then we got an error stating :

 undefined is not a function

Some googling revealed that the click method appears to be unsupported in phantom.js. so we could not just issue a typical

 document.getElementById(‘elementId’).click

Some further Googling and help from a Developer yielded the following :

IJavaScriptExecutor js = Driver as IJavaScriptExecutor;

string script = string.Format(“$(‘#{0}{1}’).click();”, UpdateLinkId,row);

js.ExecuteScript(script);

Without the string operation the main line of code is (for example)  :

string script = string.Format(“$(‘#elementId’).click();”, UpdateLinkId,row);

This now works – job done!

Further info :

Regarding dollar / hash in JQuery : http://stackoverflow.com/questions/4368582/hash-sign-in-jquery-calls

Regarding elements being set to not visible is Phantom.Js : https://github.com/ariya/phantomjs/issues/11637

https://groups.google.com/forum/#!topic/phantomjs/5aXIUB1Pi_4

Leave a comment