WebDriver click() vs JavaScript click()

后端 未结 3 1144
慢半拍i
慢半拍i 2020-11-22 09:18

The Story:

Here on StackOverflow, I\'ve seen users reporting that they cannot click an element via selenium WebDriver \"click\" command and can work

3条回答
  •  我在风中等你
    2020-11-22 09:54

    NOTE: let's call 'click' is end-user click. 'js click' is click via JS

    Why is clicking "via JavaScript" works when a regular WebDriver click does not?

    There are 2 cases for this to happen:

    I. If you are using PhamtomJS

    Then this is the most common known behavior of PhantomJS . Some elements are sometimes not clickable, for example

    . This is because PhantomJS was original made for simulating the engine of browsers (like initial HTML + CSS -> computing CSS -> rendering). But it does not mean to be interacted with as an end user's way (viewing, clicking, dragging). Therefore PhamtomJS is only partially supported with end-users interaction.

    WHY DOES JS CLICK WORK? As for either click, they are all mean click. It is like a gun with 1 barrel and 2 triggers. One from the viewport, one from JS. Since PhamtomJS great in simulating browser's engine, a JS click should work perfectly.

    II. The event handler of "click" got to bind in the bad period of time.

    For example, we got a

    • -> We do some calculation

    • -> then we bind event of click to the

      .

    • -> Plus with some bad coding of angular (e.g. not handling scope's cycle properly)

    We may end up with the same result. Click won't work, because WebdriverJS trying to click on the element when it has no click event handler.

    WHY DOES JS CLICK WORK? Js click is like injecting js directly into the browser. Possible with 2 ways,

    Fist is through devtools console (yes, WebdriverJS does communicate with devtools' console).

    Second is inject a

    提交回复
热议问题