我为 CasperJS 编写了一个脚本,该脚本应该读取一个 json 文件,该文件包含一个站点列表,其中包含有关如何登录的详细信息,并为每个关键字运行搜索页面。
我已经用一个可以工作的站点测试了我的代码,但是对于第二个站点,它会登录,但是当它尝试转到搜索页面时,它会返回一个空页面。
这是我的 casper 设置:
casper = require('casper').create(
clientScripts: [
'jquery.min.js'
]
verbose: true
logLevel: "debug"
)
这是循环代码:
casper.start().each sites, (self, site)->
search_results[site.title] = {}
self.thenOpen site.login.path,
method: "get"
data: site.login.getdata
, ->
@echo "Visiting Login Page: #{site.login.path}"
self.then ->
self.then ->
@evaluate (ufield,user,pfield,pass)->
jQuery(ufield).val(user)
jQuery(pfield).val(pass)
,
ufield: site.login.form.username.element
user: site.login.form.username.value
pfield: site.login.form.password.element
pass: site.login.form.password.value
self.then ->
@echo "Added formdata"
self.then ->
@click "#{site.login.form.element} #{site.login.form.submit}"
self.then ->
@echo "Submitted form"
self.then ->
@waitUntilVisible site.login.form.wait.element, ->
@echo 'Page Loaded'
, ->
@echo "Login Failed"
@exit 1
, max_timeout
self.each keys, (self, skey)->
self.thenOpen site.search.path,
method: "get"
data: site.login.getdata
, ->
@echo "Visiting Search Page: #{site.search.path}, for #{skey}"
self.then ->
@echo @page.content
@waitUntilVisible "#{site.search.form.element} #{site.search.form.query.element}"
, ->
@echo "Search Form Visible"
, ->
@echo "Search Visit Failed"
@exit 1
, max_timeout
self.then ->
@evaluate (key, feild)->
jQuery(feild).val(key)
,
key: skey
feild: site.search.form.query.element
self.then ->
@echo "Added searchdata"
self.then ->
@click "#{site.search.form.element} #{site.search.form.submit}"
self.then ->
@echo "Submited Search"
self.then ->
@waitUntilVisible site.search.form.wait.element
, ->
@echo "Search Results Visible"
, ->
@echo "Search Failed"
@exit 1
, max_timeout
self.then ->
result_links = @evaluate (linktag)->
res = []
res_els = __utils__.findAll(linktag)
Array.prototype.forEach.call res_els, (e)->
href = e.getAttribute("href")
label = jQuery(e).text()
res.push {href: href, label: label} if href?
res
,
linktag: "#{site.search.threads.element} #{site.search.threads.link.element}"
self.then ->
utils.dump result_links
search_results[site.title][skey] = result_links
与失败有关的控制台输出如下:
[debug] [phantom] opening url: http://www.cyclingforums.com, HTTP GET
[debug] [phantom] Navigation requested: url=http://www.cyclingforums.com/, type=Other, lock=true, isMainFrame=true
[debug] [phantom] url changed to "http://www.cyclingforums.com/"
[debug] [phantom] Automatically injected jquery.min.js client side
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step 50/68 http://www.cyclingforums.com/ (HTTP 200)
Visiting Search Page: http://www.cyclingforums.com, for track
[info] [phantom] Step 50/68: done in 63268ms.
[info] [phantom] Step 51/68 http://www.cyclingforums.com/ (HTTP 200)
<html><head></head><body></body></html>
[info] [phantom] Step 51/68: done in 63352ms.
[info] [phantom] Step 52/69 http://www.cyclingforums.com/ (HTTP 200)
[info] [phantom] Step 52/69: done in 63452ms.
[warning] [phantom] Casper.waitFor() timeout
Search Visit Failed
为什么页面响应为空页面?
编辑:通过与其他站点的测试,似乎基本代码有效,但是这个站点似乎引起了问题。