标签: casperjs

(Chrome DevTools) 即使元素明显存在于页面上,querySelector 返回 null

我正在尝试使用 CasperJS 抓取网站,但该casper.waitForSelector()函数总是超时,这意味着它永远找不到我需要的给定元素。

然后我在 Google Chrome 中执行了以下步骤:

  1. 打开网页和 Chrome DevTools 控制台。
  2. 等待该元素在页面上对我的眼睛可见。
  3. 在控制台中输入:document.querySelector(".dropdown-menu")
  4. Chrome 给出null元素不存在)。

但是,当我切换到元素 (DevTools),然后在检查元素模式(或其子元素之一)中单击所需的元素时,Chrome 会使用相同的document.querySelector(".dropdown-menu")命令在控制台中返回该元素。

我怀疑该页面包含无效的 HTML 代码(未封闭的标签)。

当我在“检查元素模式”下单击该元素时,Chrome 会修复 HTML DOM,这意味着 JavaScript 现在会按预期返回该元素。

如果这是真的,我可以在CasperJS中做些什么来触发相同的 DOM 修复事件吗?

如果这不是真的,那可能是什么?

javascript google-chrome phantomjs casperjs

5
推荐指数
1
解决办法
1803
查看次数

将变量从this.evaluate传递给casper.then

我一定是失去了我的脑海里就这一点,但它为什么没有打印出来"1: Google Search",并"2: Google Search"?基本上:如何在this.evaluate中获取变量并在casper.js范围的其余部分中使用它?

var casper = require("casper").create();
var buttonText;

casper.start("http://google.com");

casper.then(function() {
  buttonText = this.evaluate(function () {
    var myTxt = document.querySelector('#gbqfsa').innerText;
    console.log('1: ' + myTxt);

    return myTxt;
  }); 
});

casper.then(function() {
  this.echo('2: ' + buttonText);
});

casper.on('remote.message', function(msg) {
    this.echo('remote message caught: ' + msg);
});

casper.run();
Run Code Online (Sandbox Code Playgroud)

我在这里使用这些库:

https://github.com/ariya/phantomjs

http://casperjs.org/index.html

javascript scope webkit phantomjs casperjs

4
推荐指数
1
解决办法
6252
查看次数

casperjs - 使用casper.options并使用casper.tester专门注入casper clientScripts

文档STRATE你可以传递一个options对象casperjs.create()包括JS clientScripts注入到客户端页面.

文件还规定,你不应该在一个测试文件casper.create实例.

文档没有说明(至少我找不到)如何使用optionscasper.tester类的对象.我确实尝试过这样的事情:

casper.options = {
    clientScripts:[
        '../testlib/sinon-1.7.3.js'
        ],
    logLevel:"warning",
    verbose:true

};  
Run Code Online (Sandbox Code Playgroud)

之前casper.test.begin但它打破了测试.

把它放在test.begin和casper.start之间.

casper.test.begin('Basic index.html elements test',14, function suite(test){
    casper.options..etc
    casper.start(url, function(){
    //also tried here
Run Code Online (Sandbox Code Playgroud)

而在它下面也打破了测试

我会为这方面的任何方向感到高兴.特别是注射部件

casperjs

4
推荐指数
1
解决办法
2816
查看次数

不能使用CasperJS的Underscore

我正在使用CasperJS自动前端测试,但在我的测试中遇到了使用其他npm模块的问题.我知道patchRequire但是我相信只会在测试环境之外调用,因为测试运行器补丁需要自动调试.我确实包含了它,但结果是一样的.它说它找不到模块.我已确认下划线模块已安装在node_modules项目根文件夹中.

'use strict'

_ = require 'underscore'

testConfig =
    testPageUrl: ''
    testSearchTerm: 'the'

config = _.extend testConfig, require 'common/config'
Run Code Online (Sandbox Code Playgroud)

Javascript中的代码

'use strict';

_ = require('underscore');

testConfig = {
  testPageUrl: '',
  testSearchTerm: 'the'
};

config = _.extend(testConfig, require('common/config'));
Run Code Online (Sandbox Code Playgroud)

错误

CasperError:找不到模块下划线

javascript node.js phantomjs casperjs

4
推荐指数
1
解决办法
1866
查看次数

通过CLI将URL传递给CasperJS

我正在使用CasperJS来评估网页.我想做的是让我传递一个URL参数,让CasperJS下载并评估页面,然后输出到标准网页,这样我就可以在BaSH脚本中使用它.这是我到目前为止Casper的代码:

var casper = require('casper').create();
var url = casper.cli.args;

casper.start(url, function() {
    this.evaluate(function() {
        return document;
    });
    this.echo(this.getHTML());
});
casper.run();
Run Code Online (Sandbox Code Playgroud)

一旦我运行它,这就是我所看到的:

@:~/spider/casperjs$ casperjs viewsource.js google.com
CasperError: No steps defined, aborting                                         
  /usr/local/src/casperjs/modules/casper.js:1510 in run
  ~/spider/casperjs/viewsource.js:10
Run Code Online (Sandbox Code Playgroud)

请帮忙.

javascript casperjs

4
推荐指数
1
解决办法
4597
查看次数

将参数从php传递给casperjs/phantomjs

编辑:我回答了我自己的问题,请参阅下面的编辑.

原文:我的网络服务器上安装了phantomjs和casperjs,它们都运行正常.我计划创建的脚本依赖于我网站上的用户输入,然后将其传递给casperjs脚本.在摆弄了一下之后,我注意到我被困在用户输入的基本任务上.如何将变量从php传递给casperjs?

请注意,以下只是测试脚本.

我的PHP脚本

$user_input = $_POST['user_input'];

putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs");
exec('/usr/local/bin/casperjs hello.js 2>&1',$output);
print_r($output);
Run Code Online (Sandbox Code Playgroud)

hello.js

var user_input = "http://example.com/";
var casper = require('casper').create({
  verbose: true,
  logLevel: 'error',
  pageSettings: {
    loadImages: false,
    loadPlugins: false
  }
});

casper.start(user_input, function() {
    this.echo(this.getTitle());
});

casper.run();
Run Code Online (Sandbox Code Playgroud)

那么我如何将$ user_input传递给hello.js.我的目标是用户可以输入一个随后被抓取的网址.

php parameters exec phantomjs casperjs

4
推荐指数
1
解决办法
2881
查看次数

CasperJS截图只是页面的一小部分

CasperJS文档captureSelector()没有说明如何设置屏幕截图的大小.默认情况下(至少在我的系统上使用webkit,Windows 8)似乎是截取页面左上角的一个小屏幕截图.

我在找错了地方吗?

我找到了viewportSize.我认为这是我需要的,但有没有人有代码可以将其设置为合理的默认值(如100%)?

仅供参考this.viewport('100%', '100%');,我认为不需要%.

我是否必须注入将窗口宽度和高度返回到页面中的代码并将其传回以提取它或者是否有更简单的方法?

javascript casperjs

4
推荐指数
1
解决办法
3388
查看次数

等待CasperJS中的子进程

我有一个CasperJS进程加载一些页面,然后它需要调用go-process来分析页面并决定接下来应该加载哪个页面.go-process需要一段时间才能执行.我的问题是CasperJS不会等待go-process完成并退出.

casper.then(function(){
  var p = cp.execFile('/path/parse', [], {}, function(error, stdout, stderr) {
    console.log(stdout);
  });

});
Run Code Online (Sandbox Code Playgroud)

我怎样才能等待我的子进程完成?

javascript phantomjs casperjs

4
推荐指数
1
解决办法
2039
查看次数

CasperJS按类获取元素的innerHTML

我是CasperJS的新手,我在使用innerHTML时遇到了一些问题 <p class="city">Data I Need</p>

我尝试了一些东西,但似乎没有任何东西可以得到它.

var city_name= casper.evaluate(".//*[@class='city_name']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
var friend_username = city_name.innerHTML;
Run Code Online (Sandbox Code Playgroud)

var city_name = this.evaluate(function() {
    return document.querySelector(".//*[@class='city_name']").innerHtml;
});
Run Code Online (Sandbox Code Playgroud)

javascript xpath web-scraping casperjs

4
推荐指数
1
解决办法
8349
查看次数

如何使用CasperJS获取innerHTML?

我想在<em>HTML页面的标签中获取仅字符串的属性

在此输入图像描述

我想得到"(868)"

1.

casper.then(function() {
     var word = require('utils').dump(this.getElementAttribute(x('//*[@id="content"]/div[2]/h4/em'), 'em'));
     console.log(word)
});
Run Code Online (Sandbox Code Playgroud)

2.

casper.then(function() {
    var word = require('utils').dump(this.getElementAttribute(h4[class="head"], 'em'));
    console.log(word)
});
Run Code Online (Sandbox Code Playgroud)

我试过两个但它返回"null"如何解决问题?

javascript innerhtml casperjs

4
推荐指数
1
解决办法
1万
查看次数