可以在 casper.evaluate() 中使用 casper 函数并在其中包含 jquery 代码吗?我需要以类似于 jquery 的方式迭代元素。
我正在加载 jquery.js 库
这是我的尝试脚本:
casper.evaluate(function(){
$('#size-modal .size-panel-title a').each(function(){
$(this).click();
accordionTab = $(this).attr('href');
casper.capture(screenShotOutput + "PDP-" + accordionTab +".png");
});
});
在此页面中有 2 个 Accordion ,我想要每个打开的 Accordion 的屏幕截图。 它似乎有效,但没有给出反馈,并且它在第一次 capture() 迭代时退出评估()。 测试通过,无需截图。
如果我在evaluate()之后添加
casper.capture(screenShotOutput + "PDP-accordion.png");
并在evaluate()中注释capture(),我可以看到之前的代码运行良好,屏幕截图已制作并且每个 Accordion 都打开了。
问题是 casper 使用 javascript 选择器,所以如果我只指定
casper.click('#size-modal .size-panel-title a');
casper.capture(screenShotOutput + "PDP-" + accordionTab +".png");
不使用 casper.evaluate() 只会打开一个折叠面板。
谢谢
最佳答案
你在“casper.evaluate”中所做的一切都类似于在浏览器控制台中编写同一段代码。这样想一下,你就会知道你犯了什么错误。
- “casper.capture”是 casper 特定语法,没有浏览器能够理解它。
这也是文档的引用
The concept behind this method is probably the most difficult to understand when discovering CasperJS. As a reminder, think of the
evaluate() method as a gate between the CasperJS environment and the one of the page you have opened;
Everytime you pass a closure to evaluate(), you’re entering the page and execute code as if you were using the browser console.
我希望图片可以帮助:
我同意 @Artjom B. 的建议。
关于javascript - 在 casper.evaluate() 中使用 casper 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28763466/