QuerySelector for Web Elements Inside iframe(iframe 内 Web 元素的 QuerySelector)
问题描述
新标题.我正在寻找的是 iframe 内元素的 document.querySelector.
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.
我正在尝试在 iframe 中进行查询.我正在构建要在 Selenium 中使用的字符串选择器,通常我只是用 Firebug 检查元素,然后使用 document.querySelectorAll("theStringIBuid");
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");
但它不适用于 iframe 中的元素.我已经尝试了以下所有方法来在page-iframe"iframe 中获取一个元素radiobutton1".
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
谢谢-
推荐答案
简单 es6 改编自 h3manth:
simple es6 adapted from h3manth:
document.querySelectorAll('iframe').forEach( item =>
console.log(item.contentWindow.document.body.querySelectorAll('a'))
)
这篇关于iframe 内 Web 元素的 QuerySelector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!