CasperJS:下拉列表;选择一个选项,代码可以在 browser&slimer 中运行,但不能在 phantom 中运行

Fan*_*nch 5 javascript html-select phantomjs casperjs slimerjs

这是我的问题:我在一个特定的情况下尝试设置选择下拉列表的选项。我通常使用this.mouse.up() + this.mouse.down(),但在这种情况下不能,因为这种行为在带有 webkit 的网站上不起作用(您可以将两者与 google chrome 和 Firefox 进行比较)。

这里是 url:在我的示例中,我想将字段“ANNEE”设置为年份 2008

我的代码:(我的函数更改 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();
    });
});
Run Code Online (Sandbox Code Playgroud)

相当于我的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');
Run Code Online (Sandbox Code Playgroud)

所以它可以在 FF、Google Chrome、slimerJS 中工作,但不能在 PhantomJS 中工作...请帮忙,如果您有其他想法,只需在“ANNEE”字段中选择一个选项,使用 casper+phantom,我接受!

会不会是浏览器兼容性的问题?

这很奇怪,因为在同一个网站中,有时它与其他“选择”一起使用,与此相同......

sud*_*pto 2

由于该页面中已经包含 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(){
    }
});
Run Code Online (Sandbox Code Playgroud)

祝你好运。