6

I want to get an element from an iFrame and click it. Getting the element succeeded. Unfortunately i cant get the element within the iframe clicked. Anyone with a solution?

Thanks!

    $(document).ready(function(){
    var frame = $('#f314dbebb8'); //ID from the frame
    console.log(frame);
    });
Apostolos
  • 10,033
  • 5
  • 24
  • 39
Donny van V
  • 921
  • 1
  • 10
  • 22

3 Answers3

10

Is this what you are trying with

document.getElementById('iframeResult').contentWindow.document.getElementById('buttonId').click()

where iframeResult is Id of iframe and buttonId is Id of element to be clicked ??

Piyush Ashtt
  • 340
  • 1
  • 5
  • Yeah it is what i try... The only point is that the buttonId doesnt contain an ID but a class – Donny van V Jan 03 '14 at 19:50
  • document.getElementById('google_ads_iframe_/16833175/MainLeaderboard_0').contentWindow.document.getElementsByClassName('rhbutton')[0].click() this works good for me – Piyush Ashtt Jan 03 '14 at 19:56
  • 4
    Retrieving an error: Uncaught TypeError: Cannot read property 'contentWindow' of null – Donny van V Jan 03 '14 at 20:02
  • You can't do this with modern browsers unless the iframe is on the same host as the page it's on, which means you have to serve your directory locally, if you're just looking at html files on your disk, see https://stackoverflow.com/questions/29983786/blocked-a-frame-of-origin-null-from-accessing-a-cross-origin-frame-chrome – Boris Verkhovskiy Jul 22 '20 at 21:51
3

Do you mean you are trying to "click it" programmaticly, like via JavaScript? Or a user interaction like with your mouse and cant?

If you are trying via JavaScript its not going to work. Look up something called "same origin security". JavaScript cannot access other pages load via an iframe for security purposes. http://javascript.info/tutorial/same-origin-security-policy

Cyph
  • 623
  • 1
  • 7
  • 25
  • I know how to use the click function, i gave it a try with a small script and it worked. Now i started to click something in the iframe and that doesnt work. – Donny van V Jan 03 '14 at 19:41
  • Yep, saw your sample code. Not do-able. It violates same origin policy, unless you happen to be running the iframe from the same domain. – Cyph Jan 03 '14 at 19:42
  • Is it possible to get all elements from the iframe, and transform it to clickable? – Donny van V Jan 03 '14 at 19:45
  • it is not allowed to read from another origin. you wont be able to read anything from the iframe using JS running on your domain. – Cyph Jan 03 '14 at 19:53
-1

Have you tried document.getElementById('elementid').click();?

Christian Dechery
  • 876
  • 10
  • 31