【发布时间】:2014-12-25 04:00:55
【问题描述】:
编辑:新标题。 我正在寻找的是 iframe 内元素的 document.querySelector。
我在谷歌上搜索了很多答案,最后我被难住了。
我正在尝试在 iframe 中进行查询。我正在构建要在 Selenium 中使用的字符串选择器,通常我只是用 Firebug 检查元素,然后使用 document.querySelectorAll("theStringIBuid");
但它不适用于 iframe 中的元素。我已经尝试了以下所有方法来在“page-iframe”iframe 中获取一个元素“radiobutton1”。
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
谢谢-
【问题讨论】:
-
您是否可能在 iframe 加载完成之前运行脚本?如果您 console.log 内容窗口会得到什么?
-
如果你使用 selenium,你需要调用 switchTo().frame("id of frame") 然后运行你的代码。
-
好吧,我已经为此工作了两个小时,所以我认为页面已完成加载:)
-
而且我没有使用 Selenium 编写查询,而是尝试在 Firebug 控制台中编写查询。知道如何编写控制台查询来访问 iframe 中的元素吗?
标签: javascript html iframe selenium-webdriver css-selectors