1

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;

Btnplay.click();

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?

Helping Hands
  • 5,292
  • 9
  • 60
  • 127
  • I've had similar issues. I stopped using Phantom in the end as I simply couldn't get it to work as expected – Liam Oct 07 '16 at 07:24
  • @Liam - any other headless browser solution then? I will need to upload everything on linux and will run build using jenkins so really require headless browser solution. – Helping Hands Oct 07 '16 at 07:28
  • I was trying to use a team city build. In the end I gave up and fired a standard chrome (non-headless) version using Powershell and a scheduled task. I couldn't find a good headless solution to my issues, sorry. I'll watch this question with interest though as it mirrors my experience. I did have a question but I didn't get much response so deleted it. – Liam Oct 07 '16 at 07:41
  • @Liam - I see. Let us wait for helpful answer :) – Helping Hands Oct 07 '16 at 08:13
  • What is supposed to happen when you click play? If you expect to see a video, then you're out of luck, because PhantomJS doesn't support video. – Artjom B. Oct 07 '16 at 17:14
  • @ArtjomB. - When I click on Play, It should just change screen. No video here. website is like survey. On play it starts with question and answer. On click on Next it shows next question. – Helping Hands Oct 08 '16 at 07:47
  • Could you share your website URL so we can test it in live scenario?? Thanks – Saurabh Gaur Oct 09 '16 at 12:54
  • @SaurabhGaur - Everything is in private network. I can not share. I can share relevant html code. But almost related code I added to question. – Helping Hands Oct 09 '16 at 13:09
  • Ok I understand your situation, but without seeing any live scenario it is hard to say why your code is not working and also hard to resolve your problem. Thanks – Saurabh Gaur Oct 09 '16 at 15:15
  • Have you tried adding a wait to finding the element on the next page? It might be looking for this element before the element is loaded on the DOM – RemcoW Oct 10 '16 at 09:37
  • Are you sure the button `onclick` handler is set on the button before you're doing the click? – Len Oct 10 '16 at 10:21
  • @RemcoW - I have updated informesation in question. – Helping Hands Oct 11 '16 at 13:40

1 Answers1

1

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.

Community
  • 1
  • 1
Saifur
  • 16,081
  • 6
  • 49
  • 73