选取了好几种代码,第一种是我自己使用成功的一种。其他几个没有成功,等以后学会了javascript之后再来分析一下为什么。
(function(){
var done = false;
var length = document.getElementById("zhuti").contentWindow.document.getElementById("pjkc").getElementsByTagName("option").length;
var count = 0;
try{
var setAll = function(){
var selects = document.getElementById("zhuti").contentWindow.document.getElementsByClassName("datelist")[0].getElementsByTagName("select");
var randI=Math.floor(Math.random()*8);
for(var i =0; i < selects.length;i++){
if(i==randI){
selects[i].value="良好";
}else{
selects[i].value="优秀";
}
};
};
var submitData = function(){
if(done) return;
if(count >= length) {
console.log("all done, have fun!");
done = true;
document.getElementById("zhuti").contentWindow.document.getElementById("Button2").click();
return;
}
count ++;
document.getElementById("zhuti").contentWindow.document.getElementById("Button1").click();
};
document.getElementById("zhuti").addEventListener("load", function(){
setAll();
submitData();
});
setAll();
submitData();
}
catch(e){
done = true;
}
})();
var iframe = document.getElementById('iframeautoheight');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
var selects = innerDoc.getElementsByTagName('select');
var scorelist = [];
// 找到下拉菜单列表
for (var i = 0; i < selects.length; i++) {
if (selects[i].id.indexOf("DataGrid1__") > -1) {
scorelist.push(selects[i]);
}
}
// 选取下拉菜单项
for (var i = 0; i < scorelist.length; i++) {
var ops = scorelist[i].options;
var rid = Math.floor(Math.random()*scorelist.length);
for (var j = 0; j < ops.length; j++) {
var tempValue = ops[j].value;
if (i == rid) {
if (tempValue == '4(良好)') {
ops[j].selected = true;
}
} else {
if (tempValue == '5(优秀)') {
ops[j].selected = true;
}
}
}
}
var btn_save = innerDoc.getElementById('Button1');
btn_save.click();