我为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
任何人对页面为什么以空白页面做出回应都有任何想法,请帮助
谢谢。
编辑:通过对其他网站的测试,似乎基本代码有效,但这个网站似乎引起了问题。