Edit: New title. What I'm looking for is a document.querySelector for elements inside an iframe.
I've done quite a bit of Googling for an answer and finally I'm stumped.
I'm trying to query inside an iframe. I'm building string selectors to be used in Selenium and usually I just inspect the element with Firebug, and use document.querySelectorAll("theStringIBuid");
But it doesn't work with elements inside iframes. I've tried all of the below to get an element "radiobutton1" inside the "page-iframe" iframe.
var elem1 = ".page-iframe"; console.log(elem1); var elem2 = ".radiobutton1"; console.log(elem2); document.querySelectorAll(elem1+elem2+""); document.querySelectorAll('.page-iframe').contentWindow.document.body.querySelectorAll('.radiobutton1') document.getElementById('.page-iframe').contentWindow.document.body.innerHTML; [].forEach.call( document.querySelectorAll('.page-iframe'), function fn(elem){ console.log(elem.contentWindow.document.body.querySelectorAll('.radiobutton1')); }); var contentWindow = document.getElementById('.page-iframe').contentWindow var contentWindow = document.querySelectorAll('.page-iframe') var contentWindow = document.querySelectorAll('.page-iframe')[0].contentWindow
Thanks-
Getting the element in Iframeconst iframe = document. getElementById("myIframe"); Now, it has and contentWindow property which returns the document object by using that we can access the elements from an Iframe. const iWindow = iframe.
The querySelector() method is available on the document object or any Element object.
simple es6 adapted from h3manth:
document.querySelectorAll('iframe').forEach( item => console.log(item.contentWindow.document.body.querySelectorAll('a')) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With