当在Chrome浏览器上执行用于点击按钮的jQuery代码$('.reply_button').click()
时,菜单会按预期弹出。但是,当在CasperJS中的this.evaluate
内执行相同的jQuery代码时,网页会显示加载屏幕。
为什么页面反应不同,我们如何才能像使用Chrome浏览器一样获取弹出窗口?
CasperJS代码
var casper = require('casper').create()
casper.options.pageSettings = {
loadImages: true,
loadPlugins: false,
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X)'
}
casper.start("http://raleigh.craigslist.org/apa/5167043093.html", function() {
this.evaluate(function() {
$('.reply_button').click()
})
})
casper.wait(5000, function() {
this.capture('1.png')
})
casper.run()
包含debug
和ResourceError
[info] [phantom] Starting...
[info] [phantom] Running suite: 3 steps
[debug] [phantom] opening url: http://raleigh.craigslist.org/apa/5167043093.html, HTTP GET
[debug] [phantom] Navigation requested: url=http://raleigh.craigslist.org/apa/5167043093.html, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://raleigh.craigslist.org/apa/5167043093.html"
[debug] [phantom] Navigation requested: url=http://www.craigslist.org/static/localstorage.html?v=51a29e41f8e978141e4085ed4a77d170, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/3 http://raleigh.craigslist.org/apa/5167043093.html (HTTP 200)
[info] [phantom] Step anonymous 2/3: done in 1264ms.
[info] [phantom] Step _step 3/3 http://raleigh.craigslist.org/apa/5167043093.html (HTTP 200)
[info] [phantom] Step _step 3/3: done in 1279ms.
ResourceError: {
"errorCode": 6,
"errorString": "SSL handshake failed",
"id": 28,
"url": "https://www.google.com/recaptcha/api.js?onload=gRecaptchaCallback&render=explicit&_=1439307138317"
}
[info] [phantom] wait() finished waiting for 5000ms.
[debug] [phantom] Capturing page to /Users/x/test/1.png
[info] [phantom] Capture saved to /Users/x/test/1.png
[info] [phantom] Done 3 steps in 6523ms
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///usr/local/lib/node_modules/casperjs/bin/bootstrap.js. Domains, protocols and ports must match.
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///usr/local/lib/node_modules/casperjs/bin/bootstrap.js. Domains, protocols and ports must match.
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///usr/local/lib/node_modules/casperjs/bin/bootstrap.js. Domains, protocols and ports must match.
尝试原生点击而不是合成点击
仍然触发验证码
casper.start("http://raleigh.craigslist.org/apa/5167043093.html", function() {
var rect = casper.page.evaluate(function() {
return $('.reply_button')[0].getBoundingClientRect();
});
casper.page.sendEvent('click', rect.left + rect.width / 2, rect.top + rect.height / 2);
})