Phantom JS不点击按钮,但如果我使用其他浏览器,它可以工作

时间:2022-11-19 09:32:17

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

我想使用selenium webdriver和Phantom JS在自动化中单击一个按钮。但它没有点击按钮。

HTML Code :

HTML代码:

<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.

同样的,我也尝试了ID和XPATH,但不使用phantomJS。当我使用任何浏览器时,它都可以工作。

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.

注意:网站是用Html/Css和JS构建的。点击按钮,它只是通过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?

更新:它点击元素并进入下一个屏幕。问题是在下一个屏幕上有4个元素但是当我通过截图检查时,它只显示了页面上的2个元素。我应该使用元素可见性等待吗?

1 个解决方案

#1


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.

由于您还没有提供详细的HTML,因此我将假设应用程序也是使用AJAX构建的。我们都知道,当涉及到AJAX时,几乎没有什么事情会变得非常重要。

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
  • 使用@FindBy属性在一个重量级的Ajax web页面中总是不是最好的情况,尤其是角。使用driver.findElement()调用在需要的时候找到元素
  • 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
  • 即使使用直接的driver.findElement(无论什么)调用,您也希望使用显式等待,并确保元素状态准备好接受单击,然后执行单击
  • 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.
  • 您肯定希望使用Firebug甚至Chrome developer Console来测试选择器,以确保Selenium不是对其他元素执行单击操作,而不是您可能已经完成的操作。
  • 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
  • 在最坏的情况下,如果您*使用JavascriptExecutor,您需要非常小心。我遇到过这样的情况:应用程序在单击时使用Jquery事件,在这种情况下,简单的Javascript单击将背叛您。它将显示在UI上做了一些事情,而预期的事件没有被触发。在这种情况下,您需要确保使用JavascriptExecutor触发事件。一个例子是

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.

我强烈怀疑最后一个场景是这里的情况,您可能希望在手动执行操作或深入研究应用程序代码时调查UI触发的事件。

#1


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.

由于您还没有提供详细的HTML,因此我将假设应用程序也是使用AJAX构建的。我们都知道,当涉及到AJAX时,几乎没有什么事情会变得非常重要。

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
  • 使用@FindBy属性在一个重量级的Ajax web页面中总是不是最好的情况,尤其是角。使用driver.findElement()调用在需要的时候找到元素
  • 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
  • 即使使用直接的driver.findElement(无论什么)调用,您也希望使用显式等待,并确保元素状态准备好接受单击,然后执行单击
  • 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.
  • 您肯定希望使用Firebug甚至Chrome developer Console来测试选择器,以确保Selenium不是对其他元素执行单击操作,而不是您可能已经完成的操作。
  • 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
  • 在最坏的情况下,如果您*使用JavascriptExecutor,您需要非常小心。我遇到过这样的情况:应用程序在单击时使用Jquery事件,在这种情况下,简单的Javascript单击将背叛您。它将显示在UI上做了一些事情,而预期的事件没有被触发。在这种情况下,您需要确保使用JavascriptExecutor触发事件。一个例子是

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.

我强烈怀疑最后一个场景是这里的情况,您可能希望在手动执行操作或深入研究应用程序代码时调查UI触发的事件。