javascript - How to simulate a *real* mouse click on element in iframe - Stack Overflow

$(selector).click() results in nothing happening.this answer works in the browser console with the jav

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

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

Share Improve this question edited Nov 14, 2018 at 14:58 Flutter Shy asked Nov 13, 2018 at 12:01 Flutter ShyFlutter Shy 511 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

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();

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 :)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744903295a4600105.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信