1

I have a website with a donorbox button. When it's clicked, an iframe is added to the page as a modal. I want to access the elements in that iframe but I get an error Cannot read property 'click' of null when I try to click one of the buttons with a simple document.getElementsByClassName('db_button')[0].click(); command.

Debugging in the console, I also get the same message until I inspect the element and then it seems to work fine. What am I doing wrong?

ThatGuy
  • 1,191
  • 9
  • 27

1 Answers1

0

firstly, get your iframe element

document.getElementById('iframeId') or document.getElementsByTagName("iframe")

the second expression will get a list object, get the iframe you need with index.

secondly, get inner document by this

a.contentDocument || a.contentWindow.document

then do your search inside iframe's document.

Eason Yu
  • 199
  • 5
  • Thanks for this. But I get an "Uncaught DOMException: Blocked a frame with origin ___ from accessing a cross-origin frame" error when trying to access the inner document. – ThatGuy Dec 29 '20 at 03:05
  • if your iframe load third-party url content, it's called cross-origin. you can't get inner document from outside because this might cause security problem. reference this: https://stackoverflow.com/questions/25098021/securityerror-blocked-a-frame-with-origin-from-accessing-a-cross-origin-frame try other ways to do what you want! if you have to do so, not consider security, use reverse-proxy to load third-party site with same url as your main site to cheat browser! – Eason Yu Dec 29 '20 at 03:19
  • So there's no way to click an element in the iframe? – ThatGuy Dec 31 '20 at 01:43