简体   繁体   English

如何在casperjs waitForSelector函数中将参数传递给成功回调?

[英]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. 我在CasperJS中使用waitForSelector函数,并希望将变量(indexNumber)传递给成功回调。 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. 从casper测试中调用该函数。

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). 一旦选择了选择框的选项,就会通过ajax加载按钮(这在函数的另一部分中发生)。 Then (see above) I'd like to get the value of a specific option (defined by indexNumber). 然后(请参见上文),我想获取特定选项的值(由indexNumber定义)。

The success callback of waitForSelector is the step function callback ( then ). waitForSelectorsuccess回调是waitForSelector函数的回调( 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 . 传递给此回调的是最后一个加载的页面资源,您不能通过使用不同的变量名称(如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;
})));

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

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