本文介绍了在casper.evaluate()中使用casper函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在casper.evaluate()内部使用casper函数,并在其中添加jquery代码?我需要以类似于jquery的方式来迭代元素.

it's possible using casper function inside casper.evaluate() with jquery code inside? I need to iterare elements in a way similar to how jquery does.

我正在加载jquery.js lib

I'm loading jquery.js lib

这是我的尝试脚本:

casper.evaluate(function(){
    $('#size-modal .size-panel-title a').each(function(){
        $(this).click();
        accordionTab = $(this).attr('href');
        casper.capture(screenShotOutput + "PDP-" + accordionTab +".png");
    });
});

在此页面中有2个手风琴,我想要每个打开的手风琴的屏幕截图.它似乎可行,但是没有给出反馈,并且它在第一次catch()迭代中退出了validate()的形式.无需截图即可通过测试.

in this page there are 2 accordion and i want a screenshot for each opened accordion.It seems to works but no feedback is given and it exit form evaluate() on the first capture() iteration.the test pass without making screenshot.

如果我在评估()之后添加

If i add after evaluate()

casper.capture(screenShotOutput + "PDP-accordion.png");

并在评价()内注释capture(),我可以看到之前的代码运行良好,已经完成了屏幕截图,并且每个手风琴都打开了.

and comment the capture() inside the evaluate(), i can see that the code before works well, the screenshot is made and each accordion is open.

问题是casper使用javascript选择器,所以如果我仅指定

The problem is that casper use javascript selector so if i specify only

casper.click('#size-modal .size-panel-title a');
casper.capture(screenShotOutput + "PDP-" + accordionTab +".png");

不使用casper.evaluate()只能打开一个手风琴.

without using casper.evaluate() only one accordion will be opened.

谢谢

推荐答案

您在"casper.evaluate"中所做的一切都类似于在浏览器的控制台中编写相同的代码.会知道您犯了什么错误.

What ever you do in "casper.evaluate" is simillar to writing the same piece of code in the console of the browser.Think of it this way and you will know what mistake you have made.

  1. "casper.capture"是特定于Casper的语法,浏览器无法理解.

这也是文档的参考

evaluate()方法作为CasperJS环境和您打开的页面之一之间的大门;

evaluate() method as a gate between the CasperJS environment and the one of the page you have opened;

每次将闭包传递给Evaluate()时,您都在进入页面并执行代码,就像使用浏览器控制台一样.

Everytime you pass a closure to evaluate(), you’re entering the page and execute code as if you were using the browser console.

我希望图片可能对您有帮助:

I hope the picture might help:

我同意@Artjom B.的建议.

And i Agree with whatever @Artjom B. has suggested.

这篇关于在casper.evaluate()中使用casper函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:18