1

我正在使用 CasperJS(带有slimerjs引擎)来填写表格。然而,这是行不通的。

我用一个简单的 google.com 脚本试了一下:

var casper = require("casper").create({
    verbose: true,
    logLevel: "debug"
});

var fs = require("fs");

phantom.cookiesEnabled = true;

casper.options.viewportSize = { width: 1024, height: 768 };

casper.start();

casper.userAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");

casper.thenOpen("http://www.google.com", function(response) {
    casper.fill("form[id='gbqf']", {
        q: "test"
    }, true);
}).run();

表单未填写,页面加载后没有任何反应。详细日志指出:

[info] [remote] attempting to fetch form element from selector: 'form[id='gbqf']'
[info] [phantom] Done 2 steps in 1086ms

(然后结束)

我究竟做错了什么?表单 ID 100% 正确..

4

1 回答 1

2

Google 根据用户代理、视口大小、cookie 和其他指标提供不同的页面。您必须确保该元素确实存在。您可以使用该casper.exists()功能进行检查。

我发现一个很好的 google 搜索选择器"form[action='/search']"与引擎(PhantomJS 或 SlimerJS)和用户代理字符串(有或没有声明为 IE)无关。

于 2015-02-10T20:24:00.287 回答