简体   繁体   中英

How to pass argument to success callback in casperjs waitForSelector function?

I am using the waitForSelector function in CasperJS and would like to pass a variable (indexNumber) to the success callback. This doesnt seem to work. Is this possible at all?

casper.waitForSelector(x('//button[@class="addToShopcart"]'),
    function success(indexNumber) {
        casper.echo(stripLineBreaksTrim(casper.fetchText(x('//select[@id="artikel"]//option['
+ indexNumber + ']'))));
    },
    function fail() {});

I'd like to iterate the index of an option list and extract the data from it. The above snippet is encapsulated in a function. The function is called from within the casper test.

The button is loaded via ajax as soon as the option of the select box has been selected (this happens in another part of the function). Then (see above) I'd like to get the value of a specific option (defined by indexNumber).

The success callback of waitForSelector is the step function callback ( then ). What is passed into this callback is the last page resource that was loaded and you can't change this behavior by using a different variable name like indexNumber .

According to the comments, you want to fetch text of the selected option which is actually the last changed option. So the following should work:

casper.echo(stripLineBreaksTrim(casper.evaluate(function(){
    return document.querySelector('#artikel').selectedOptions[0].innerHTML;
})));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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