我的问题是:我在一个特定的情况下,我试图设置一个选择下拉列表的选项。我通常使用this.mouse.up() + this.mouse.down()
,但我不能在这种情况下,因为这种行为不工作的网站与webkit(你可以比较两者与谷歌chrome和Firefox)。
我的代码:(我的函数更改HTML并启动change()事件)
//custom function
casper.fillSelect = function(selectSelector, optionText){
this.evaluate(function(sel,setByText) {
if ("createEvent" in document) {
var evt = document.createEvent("HTMLEvents")
,x = document.querySelectorAll(sel + ' > option')
,l = x.length
;
evt.initEvent("change", false, true);
for (i=0; i<l; i++){
if(x[i].textContent.indexOf(setByText) !== -1){
console.log(x[i]);
console.log(x[i].getAttribute('value'));
x[i].setAttribute('selected', true);
x[i].parentNode.dispatchEvent(evt);
}
}
}
else {console.log("error with fillSelect");}
},selectSelector, optionText);
};
//event
casper.test.on('fail', function(failure) {
casper.capture('fail.png');
});
/*************************************** Tests *****************************************************/
casper.test.begin('n********* Compare : ***********', function (test) {
"use strict";
casper.start()
.thenOpen("http://www.linternaute.com/voyage/climat/paris/ville-75056",function(){
casper.fillSelect('fieldset.fcNeutre > div.odSelect:nth-of-type(2) > select', '2008');
})
.waitForUrl(/2008/, function(){
this.capture('fail2.png');
this.test.assertSelectorHasText("h2", "maximales");
this.test.assertSelectorHasText("h2", "minimales");
this.test.assertSelectorHasText("h2", "Paris");
this.test.assertSelectorHasText("h2", "Le soleil");
this.test.assertSelectorHasText("h2", "La pluie");
this.test.assertExists("tspan");
this.test.assertExists("div.marB20");
this.test.assertNotEquals(this.fetchText("div.marB20 > table > thead > tr > th"), "", "Table first data not empty");
})
.run(function() {
this.test.comment('--- Done ---n');
test.done();
});
});
与我的casper自定义函数等效,您可以在浏览器中执行它:
var fillSelect = function(sel,setByText) {
if ("createEvent" in document) {
var evt = document.createEvent("HTMLEvents")
,x = document.querySelectorAll(sel + ' > option')
,l = x.length
;
evt.initEvent("change", false, true);
for (i=0; i<l; i++){
if(x[i].textContent.indexOf(setByText) !== -1){
//console.log(x[i]);
//console.log(x[i].getAttribute('value'));
x[i].setAttribute('selected', true);
x[i].parentNode.dispatchEvent(evt);
}
}
}
else {console.log("error with fillSelect");}
};
fillSelect('fieldset.fcNeutre > div.odSelect:nth-of-type(2) > select', '2008');
所以它适用于FF,谷歌浏览器,与slimerJS,但不是与PhantomJS…请帮助,如果你有另一个想法,只是选择一个选项在这个'ANNEE'字段,与卡斯珀+幻影,我采取!
可能是浏览器兼容性的问题吗?
这很奇怪,因为在同一个网站中,有时它与其他'select'一起工作,与这个相同…
所以我的最终解决方案(感谢sudipto)
casper.fillSelect = function(selectSelector, optionText){
this.evaluate(function(sel, val) {
jQuery(sel).find("option:contains('" + val + "')").attr('selected', true).change();
}, selectSelector, optionText);
};
casper.fillSelect('fieldset.fcNeutre > div.odSelect:nth-of-type(2) > select', '2008');
由于页面中已经有jQuery,所以我编写了这段代码,并且捕获以及currentUrl显示了正在发生的更改。Jquery能够正确地引发事件。我猜。
我希望你能从中提取必要的代码:
casper.on("load.finished", function (status) {
if (status !== "success") {
console.log('Failed to load page.');
}
else {
var thisurl = casper.getCurrentUrl();
window.count = (window.count || 0)+1;
casper.capture('loaded'+window.count+'.png');
if (window.count ==1) {
casper.evaluate(function(sel, val){
jQuery(sel).find("option:contains('"+val+"')").attr('selected', true);
jQuery(sel).change();
}, 'fieldset.fcNeutre > div.odSelect:nth-of-type(2) > select', 2008);
}
console.log('Page loaded.' + thisurl);
//casper.wait(2000, function(){
}
});
祝你好运。