本文介绍了CasperJS click()不加载新的html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用CasperJS' click()来关注在当前屏幕上生成模态的链接。当我查询正确的选择器并使用 document.querySelector()在浏览器控制台中单击它时。单击()它可以工作,但即使我 casper .evaluate()这不起作用。我找到了一个有类似问题的人,但他的问题仍然没有答案,我遇到了几乎相同的问题。
我正在使用的代码是

I'm trying to use CasperJS' click() to follow a link which generates a modal on the current screen. When I query the proper selector and click it in the browser console using document.querySelector().click() it works, but even when I casper.evaluate() this it doesn't work. I found someone who had a very similar problem, but his question remains unanswered, and I am experiencing almost identical problems. casperjs button click doesn't navigate to next pagethe code I'm currently using is

this.waitForSelector('div.talk-sharing__tools a.rate-button', function() {
    this.then(function() {
        this.evaluate(function() {
            document.querySelector('a.rate-button').click();
});

我要抓的页面是

推荐答案

使用phantomjs引擎执行这些导航步骤真的是不可能的。看来幻像的QtWebkit分支(版本1.9.7)不再适用于任务了。虽然你的代码工作正常> 。你可以通过npm舒适地安装slimerjs:

It is really impossible to do those navigation steps with the phantomjs engine. It seems that the QtWebkit fork of phantom (version 1.9.7) is not up to the task anymore. Although your code works fine as is with slimerjs. You can comfortably install slimerjs through npm:

npm install -g slimerjs

并运行你的脚本

casperjs --engine=slimerjs script.js






我用phantomjs尝试了几件事无法正常工作。我为 remote.message resource.error casper.on 监听器$ c>,但是在幻像中运行脚本时没有显示任何错误。 logLevel:debug也没有显示任何内容。


I tried several things with phantomjs that did not work. I added casper.on listeners for remote.message and resource.error, but those didn't show any errors when running the script in phantom. logLevel: "debug" also didn't show anything.

第一:使用一个casperjs函数。

First: Using a casperjs function.

casper.thenClick("div.talk-sharing__tools a.rate-button");

第二:尝试明确显示按钮的模态(特定于页面)。

Second: Trying to explicitly show the modal for the button (specific to the page).

casper.then(function(){
    var name = this.evaluate(function(){
        var modal = document.querySelectorAll(".modal-wrapper > .modal")[0];
        modal.className += " modal--show";
        return modal.className;
    });
    this.echo("name: "+name); // null
});

casper.waitUntilVisible(".modal__head__title");

第三:让jQuery完成工作。

Third: Let jQuery do the work.

var casper = require('casper').create({
    remoteScripts: [ "http://code.jquery.com/jquery-1.11.1.min.js" ]
});

casper.waitForSelector(selector, function() {
    this.evaluate(function() {
        jQuery("a.rate-button").click();
    });
});

这篇关于CasperJS click()不加载新的html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:20
查看更多