简体   繁体   English

更改选择框选项的值并在CasperJS中选择它

[英]Changing the value of a select box option and selecting it in CasperJS

I am trying to circumvent some difficult query result pagination by hijacking a form element that controls the number of query results shown on a page. 我试图通过劫持控制页面上显示的查询结果数量的表单元素来规避一些困难的查询结果分页。 When I've tested my Javascript modification in the Firebug console against the live site it works like a champ but when the same Javascript is injected into the DOM via the casper.evaluate method I get inconsistent results. 当我在Firebug控制台中针对现场站点测试了Javascript修改后,它的工作原理就像冠军一样,但是当通过casper.evaluate方法将相同的Javascript注入DOM时,结果不一致。

My code is as follows: 我的代码如下:

var s = document.getElementById("requisitionListInterface.dropListSize");
s.options[4].value = 1000;
s.options[4].selected = true;
var e = document.createEvent("HTMLEvents");
e.initEvent("change", false, true );
setTimeout( function(s, e){ s.dispatchEvent(e); }, 2000, s, e );

I've had to create then event handler 'e' and attach it to element 's' to in order to replicated what was taking place on the form (submit page when select.change occurred). 我必须先创建事件处理程序'e'并将其附加到元素's'上,以便复制表单上发生的事情(发生select.change时提交页面)。

Again the above code functions as expected in Firefox every time. 同样,以上代码每次都能在Firefox中正常运行。

If the select box is firing Ajax you may not be giving the browser enough time to get the results. 如果选择框触发了Ajax,则您可能没有给浏览器足够的时间来获得结果。 I would put some wait statements in there to give things enough time to find and execute. 我会在其中放置一些wait语句,以使事情有足够的时间查找和执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM