Helping Hands Helping Hands - 1 year ago 107
HTML Question

Phantom JS not clicking on button but it works if I use any other browser

I want to click on one button in automation using selenium webdriver & Phantom JS. But it is not clicking on button.

HTML Code :

<button id="play" class="next-play"><span>play</span></button>

I tried :

@FindBy(css = "#play")
private WebElement Btnplay;;

I also tried :

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", Btnplay);

Above same things I tried with ID and XPATH as well but not working with phantomJS. It works when I use any browser.

I took screenshots run time and I can see it is not clicking on button.

When I see error in console, It says unable to find element [which is in next page after click on play]. so it seems might be it is clicking but not going to next page.

Note : Website is built using Html/Css and JS. On click on button,it just changes screens by JS.

UPDATE : It is clicking on element and going to next screen. Issue is in next screen there are 4 elements but When I checked via screenshot , It shows only 2 Elements on page. Should I use element visibility wait?

Answer Source

Since you have not provided detail HTML, I am going to assume that the application is build using some AJAX as well. There are few things, as we all know, become very important when AJAX is involved.

Problems and possible solutions:

  • Using @FindBy attribute is always not the preferable case in a heavy Ajax web page, especially Angular. Find the element right there when you need it using driver.findElement() call
  • Even with a straight driver.findElement(whatever) call you want to use Explicit wait and make sure the element state is ready to accept the click and then perform the click
  • You definitely want to test the selector using Firebug or even Chrome developer Console just to make sure Selenium is not performing click action on something other than the intended element which you may have done already.
  • In worst case scenario and if you are forced to use JavascriptExecutor, you want to be very careful. I have come across the situation where the application was using Jquery event on click in which case simple Javascript click will betray you. It will show you that something was done on UI as expected whereas the intended event was not triggered. In such case, you need to make sure the event is triggered using JavascriptExecutor as well. An example is here

I strongly suspect the last scenario is the case here and you may want to investigate the event triggered by the UI when you manually perform the action or dig into the application code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download