【发布时间】:2015-07-23 16:04:07
【问题描述】:
我试图在一个用 ASP 构建的网站上模仿浏览器行为,它似乎使用了很多基于 javascript 的链接和使用 CasperJS 的 UI。我很困惑,不知道下一步该怎么做。
我用的是:casperjs1.1.0-beta3,phantomjs1.9.8,网址是https://icsid.worldbank.org/apps/ICSIDWEB/cases/Pages/AdvancedSearch.aspx
这是我要点击的 HTML 链接:
<td>
<a href="javascript:__doPostBack('ctl00$m$g_ba040fcb_44f7_44fa_92d0_d088c5679794$ctl00$gvCasedetails','Page$3')">3</a>
</td>
该站点存在一些 SSL 配置问题,因此 CasperJS 运行时需要使用一些额外的标志:casperjs --ignore-ssl-errors=true --ssl-protocol=tlsv1 icsid.js
icsid.js 只是尝试打开站点并单击链接以进入下一页结果。我想检查所有结果。
var casper = require('casper').create({
clientScripts: ["./jquery.min.js"],
verbose: true,
logLevel: 'debug',
pageSettings: {
loadImages: false,
loadPlugins: false,
userAgent: 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36',
}
});
casper.start('https://icsid.worldbank.org/apps/ICSIDWEB/cases/Pages/AdvancedSearch.aspx', function(){});
casper.then(function() {
this.wait(5000);
this.capture('screenshot0.png');
casper.then(function(){
var text = this.evaluate(function(){
return jQuery('.gdcol a')[0].text;
});
console.log('text: ' + text);
this.evaluate(function(){
// try to go to second page
return jQuery('a').filter(function(index) { return $(this).text() === "2"; })[0].click();
});
});
casper.then(function(){
this.wait(5000);
var size = this.evaluate(function(){
return jQuery('.gdcol a').size();
});
console.log('size: ' + size);
// if successfully clicked and changed url, the link text will change
var text = this.evaluate(function(){
return jQuery('.gdcol a')[0].text;
});
console.log('text: ' + text);
// if it's still on the first page, this will be null
var page = this.evaluate(function(){
return jQuery('a').filter(function(index) { return $(this).text() === "1"; })[0].text;
});
console.log('page: ' + page);
// if it's on the second page, this will be null
var page = this.evaluate(function(){
return jQuery('a').filter(function(index) { return $(this).text() === "2"; })[0].text;
});
console.log('page: ' + page);
this.capture('screenshot1.png');
});
});
casper.run();
这是生成的日志:
[info] [phantom] Starting...
[info] [phantom] Running suite: 3 steps
[debug] [phantom] opening url: https://icsid.worldbank.org/apps/ICSIDWEB/cases/Pages/AdvancedSearch.aspx, HTTP GET
[debug] [phantom] Navigation requested: url=https://icsid.worldbank.org/apps/ICSIDWEB/cases/Pages/AdvancedSearch.aspx, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://icsid.worldbank.org/apps/ICSIDWEB/cases/Pages/AdvancedSearch.aspx"
2015-07-23 11:48:31.255 phantomjs[10699:d13] CoreText performance note: Client called CTFontCreateWithName() using name "Arial" and got font with PostScript name "ArialMT". For best performance, only use PostScript names when calling this API.
2015-07-23 11:48:31.256 phantomjs[10699:d13] CoreText performance note: Set a breakpoint on CTFontLogSuboptimalRequest to debug.
2015-07-23 11:48:31.278 phantomjs[10699:d13] CoreText performance note: Client called CTFontCreateWithName() using name "Arial" and got font with PostScript name "ArialMT". For best performance, only use PostScript names when calling this API.
2015-07-23 11:48:31.279 phantomjs[10699:d13] CoreText performance note: Client called CTFontCreateWithName() using name "Arial" and got font with PostScript name "ArialMT". For best performance, only use PostScript names when calling this API.
2015-07-23 11:48:31.280 phantomjs[10699:d13] CoreText performance note: Client called CTFontCreateWithName() using name "Arial" and got font with PostScript name "ArialMT". For best performance, only use PostScript names when calling this API.
2015-07-23 11:48:31.280 phantomjs[10699:d13] CoreText performance note: Client called CTFontCreateWithName() using name "Arial" and got font with PostScript name "ArialMT". For best performance, only use PostScript names when calling this API.
2015-07-23 11:48:31.479 phantomjs[10699:d13] CoreText performance note: Client called CTFontCreateWithName() using name "Arial" and got font with PostScript name "ArialMT". For best performance, only use PostScript names when calling this API.
2015-07-23 11:48:31.480 phantomjs[10699:d13] CoreText performance note: Client called CTFontCreateWithName() using name "Arial" and got font with PostScript name "ArialMT". For best performance, only use PostScript names when calling this API.
[debug] [phantom] Automatically injected ./jquery.min.js client side
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/3 https://icsid.worldbank.org/apps/ICSIDWEB/cases/Pages/AdvancedSearch.aspx (HTTP 200)
[info] [phantom] Step anonymous 2/3: done in 1886ms.
[info] [phantom] Step anonymous 3/3 https://icsid.worldbank.org/apps/ICSIDWEB/cases/Pages/AdvancedSearch.aspx (HTTP 200)
[debug] [phantom] Capturing page to /Users/yubrew/app/lib/tasks/screenshot0.png
[info] [phantom] Capture saved to /Users/yubrew/app/lib/tasks/screenshot0.png
[info] [phantom] Step anonymous 3/3: done in 2347ms.
[info] [phantom] Step _step 4/6 https://icsid.worldbank.org/apps/ICSIDWEB/cases/Pages/AdvancedSearch.aspx (HTTP 200)
[info] [phantom] Step _step 4/6: done in 2351ms.
[info] [phantom] wait() finished waiting for 5000ms.
[info] [phantom] Step anonymous 5/6 https://icsid.worldbank.org/apps/ICSIDWEB/cases/Pages/AdvancedSearch.aspx (HTTP 200)
text: ARB/15/30
[info] [phantom] Step anonymous 5/6: done in 7377ms.
[info] [phantom] Step anonymous 6/6 https://icsid.worldbank.org/apps/ICSIDWEB/cases/Pages/AdvancedSearch.aspx (HTTP 200)
size: 50
text: ARB/15/30
page: null
page: 2
[debug] [phantom] Capturing page to /Users/yubrew/app/lib/tasks/screenshot1.png
[info] [phantom] Capture saved to /Users/yubrew/app/lib/tasks/screenshot1.png
[info] [phantom] Step anonymous 6/6: done in 7491ms.
[info] [phantom] Step _step 7/7 https://icsid.worldbank.org/apps/ICSIDWEB/cases/Pages/AdvancedSearch.aspx (HTTP 200)
[info] [phantom] Step _step 7/7: done in 7493ms.
[info] [phantom] wait() finished waiting for 5000ms.
[info] [phantom] Done 7 steps in 12493ms
请注意,屏幕截图和控制台日志表明页面内容没有改变。
【问题讨论】:
标签: javascript click phantomjs casperjs