Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QuerySelector for Web Elements Inside iframe

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-

like image 380
snug Avatar asked Oct 29 '14 12:10

snug


People also ask

How do I select an element in an iframe?

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.

Can you use querySelector on an element?

The querySelector() method is available on the document object or any Element object.


1 Answers

simple es6 adapted from h3manth:

document.querySelectorAll('iframe').forEach( item =>     console.log(item.contentWindow.document.body.querySelectorAll('a')) ) 
like image 163
cregox Avatar answered Oct 14 '22 08:10

cregox