javascript - 在 casper.evaluate() 中使用 casper 函数

标签 javascript jquery phantomjs casperjs

可以在 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”中所做的一切都类似于在浏览器控制台中编写同一段代码。这样想一下,你就会知道你犯了什么错误。

  1. “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.

我希望图片可以帮助:

Evaluate, the way it interacts with browser

我同意 @Artjom B. 的建议。

关于javascript - 在 casper.evaluate() 中使用 casper 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28763466/

相关文章:

javascript - JSON 输入意外结束

javascript - 我怎样才能模仿 stopImmediatePropagation() 的行为(不使用 jQuery)

jquery 克隆元素 block 。选择行为怪异的元素

jquery - YouTube视频在WordPress中播放完后的重叠窗口

javascript - 使用 JQuery 单击元素

javascript - CasperJS 中是否有任何 onResourceTimeout 等效项?

javascript - 如何在多个文本节点中搜索一个字符串

c# - 字符加扰(C# 和 Javascript)

python - 在 Python 中为 CSS 属性值解析呈现的 HTML

javascript - 使用 PhantomJS 包含 js 文件