Flutter Shy
Flutter Shy

Reputation: 51

How to simulate a *real* mouse click on element in iframe

$(selector).click() results in nothing happening.
this answer works in the browser console with the javascript context set to the iframe, but not the main page: simulateMouseClick($("iframe").contents().find(selector)) results in:

Uncaught TypeError: targetNode.dispatchEvent is not a function
at triggerMouseEvent (:5:20)
at :8:9
at Array.forEach ()
at simulateMouseClick (:7:52)
at :1:1

$("iframe").contents().find(selector).text() gives me what's expected so it's the correct element.

how can I achieve this?

Edit: adding this as people apparently can't read:
$("iframe").contents().find(selector).click()
has absolutely no effect as .click() does not simulate a REAL mouse click.

Upvotes: 5

Views: 8446

Answers (2)

Socrapop
Socrapop

Reputation: 206

Get the element inside your jQuery Object, and use the vanillaJS .click() method:

$("iframe").contents().find(selector)[0].click();

That's it.

PS: I know this is an old question, but I had the same problem and solved it that way :)

Upvotes: 0

AllirionX
AllirionX

Reputation: 1143

With jQuery :

$("iframe").contents().find(selector).click();

With Vannila JS :

Using window.frames gives you access to the iframes' window object, as mentionned in the Mozilla doc

You can use this object to find elements in the iframe and use them in your script. For example :

  var iframeWindow = window.frames[0];
  var element = iframeWindow.document.getElementsByClassName("selector")[0];
  element.click();

Upvotes: 2

Related Questions