Javascript 如何使用CasperJS在iframe中选择具有特定值或内容的选项?
我正在用CasperJS爬行,但我首先需要单击一些东西来完成页面的搜索过程。但我无法根据其值或内容来选择spcific选项 1.单击父框架中的“全部”按钮后:Javascript 如何使用CasperJS在iframe中选择具有特定值或内容的选项?,javascript,jquery,iframe,casperjs,Javascript,Jquery,Iframe,Casperjs,我正在用CasperJS爬行,但我首先需要单击一些东西来完成页面的搜索过程。但我无法根据其值或内容来选择spcific选项 1.单击父框架中的“全部”按钮后: <select name="sort_id" id="sort_id" class="p9"; display: block;" onclick="javascript: window.setTimeout('Hide_Select("sort_id",false)',3); showDialog
<select name="sort_id" id="sort_id" class="p9"; display: block;" onclick="javascript: window.setTimeout('Hide_Select("sort_id",false)',3); showDialog('LIST','SearchList.aspx?ddl_id=sort_id&key=lar~sort_id&EncodingName=',460,360,true);return false;">
<option value="">All</option>
</select>
但是当我捕获页面时,代码似乎无法成功地选择OptionA。如果iFrame获得类似的内容,您可以忽略iFrame ID:
casper.then(function() {
this.click('#sort_id.p9');
});
casper.withFrame("IFrameID", function() {
casper.then(function() {
this.evaluate(function() {
var sel = document.querySelectorAll('#listb_sor option');
sel.val('001').onchange();
});
})
casper.then(function() {
// do other stuff in the IFrame
})
});
casper.then(function() {
// continue outside the IFrame
});
如果IFrame没有ID,则必须根据页面上的IFrame查看IFrame的索引:
...
casper.withFrame(1, function() {
casper.then(function() {
this.evaluate(function() {
var sel = document.querySelectorAll('#listb_sor option');
sel.val('001').onchange();
});
})
casper.then(function() {
// do other stuff in the IFrame
})
});
...
有关更多信息,请查看thx!你能告诉我如何得到iframe的索引吗?框架没有ID。在这种情况下,它是页面的第n个元素,即iframes。第一个iframe是索引0,第二个索引1,依此类推。如果站点上存在动态加载的iFrame,则可能会出现问题。;)
casper.then(function() {
this.click('#sort_id.p9');
});
casper.withFrame("IFrameID", function() {
casper.then(function() {
this.evaluate(function() {
var sel = document.querySelectorAll('#listb_sor option');
sel.val('001').onchange();
});
})
casper.then(function() {
// do other stuff in the IFrame
})
});
casper.then(function() {
// continue outside the IFrame
});
...
casper.withFrame(1, function() {
casper.then(function() {
this.evaluate(function() {
var sel = document.querySelectorAll('#listb_sor option');
sel.val('001').onchange();
});
})
casper.then(function() {
// do other stuff in the IFrame
})
});
...