如何使用CasperJS

时间:2016-04-25 18:23:14

标签: javascript asp.net forms phantomjs casperjs

我正在尝试提交.Net表单,然后从响应页面捕获元素。 .Net表单似乎无法正确提交,因为CasperJS只是重新加载表单页面。存储Google搜索结果的CasperJS示例对我来说很好,但此表单根本不会加载响应页面。

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

casper.start("https://jobs.ca.gov/Public/JobVacancySearch.aspx", function(){
    this.fill('#form1', {'ctl00$cphMainContent$txtJobTitle': 'Programmer'}, true);

    this.waitForSelector("#cphMainContent_gvSearchResults");
});

casper.run(function(){
    console.log("success");
});

这是输出:

casperjs so-test.js --config=<(echo '{"sslProtocol": "any"}') --ignore-ssl-errors=true
[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[debug] [phantom] opening url: https://jobs.ca.gov/Public/JobVacancySearch.aspx, HTTP GET
[debug] [phantom] Navigation requested: url=https://jobs.ca.gov/Public/JobVacancySearch.aspx, type=Other, lock=true, isMainFrame=true
[debug] [phantom] url changed to "https://jobs.ca.gov/Public/JobVacancySearch.aspx"
[debug] [phantom] Navigation requested: url=https://jobs.ca.gov/MasterContent/headerSiteSearch.html, type=Other, lock=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=https://jobs.ca.gov/MasterContent/headerSiteSearch.html, type=Other, lock=true, isMainFrame=false
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step 2/2 https://jobs.ca.gov/Public/JobVacancySearch.aspx (HTTP 200)
[info] [remote] attempting to fetch form element from selector: '#form1'
[debug] [remote] Set "ctl00$cphMainContent$txtJobTitle" field value to Programmer
[info] [remote] submitting form to ./JobVacancySearch.aspx, HTTP POST
[info] [phantom] Step 2/2: done in 2070ms.
[debug] [phantom] Navigation requested: url=https://jobs.ca.gov/Public/JobVacancySearch.aspx, type=FormSubmitted, lock=true, isMainFrame=true
[debug] [phantom] url changed to "https://jobs.ca.gov/Public/JobVacancySearch.aspx"
[debug] [phantom] Navigation requested: url=https://jobs.ca.gov/MasterContent/headerSiteSearch.html, type=Other, lock=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=https://jobs.ca.gov/MasterContent/headerSiteSearch.html, type=Other, lock=true, isMainFrame=false
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step 3/3 https://jobs.ca.gov/Public/JobVacancySearch.aspx (HTTP 200)
[info] [phantom] Step 3/3: done in 2355ms.
[warning] [phantom] Casper.waitFor() timeout
[error] [phantom] Wait timeout of 5000ms expired, exiting.
Wait timeout of 5000ms expired, exiting.

1 个答案:

答案 0 :(得分:1)

填写表单后单击“提交”按钮似乎已解决了此问题。

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

casper.start("https://jobs.ca.gov/Public/JobVacancySearch.aspx");

casper.then(function(){
    this.fill('#form1', {'ctl00$cphMainContent$txtJobTitle': 'Programmer'}, false);
});

casper.then(function(){
    this.click("#cphMainContent_btnFindJobs"); 
});

casper.waitForSelector("#cphMainContent_gvSearchResults", function(){
    console.log("Found search results");
});

casper.run(function(){
    console.log("success");
    this.exit();
});