带有iframe或frameset框架的情况下,如何对内部元素进行操作,要先找到这个框架,再其基础上进行操作,代码如下:
- var frame = document.querySelector("body > frameset");
- //var frame = document.querySelector("#myFrameset");
- var options = frame.childNodes[1].contentWindow.document.querySelectorAll("select");
- for(let i=0;i<options.length; i++){
- let n = Math.floor(Math.random() * 3) + 1;
- options[i].children[n].selected=true;
- }
- frame.childNodes[1].contentWindow.document.querySelectorAll("input")[1].click();
解释:
找到此框架,如果框架内部有多个子框架,需要选择其子框架,然后借用contentWindow过渡一下,然后就是正常的document了。