当queryselector传递变量时,CasperJS找不到链接

问题描述 投票:2回答:1

我正试图用casperjs刮一个网页。当我将链接ID作为变量传递时,它无法单击链接,但是当我手动输入文本字符串时,它可以正常工作。

这很好用

console.log(this.evaluate( function() {document.querySelector("#ctl00_ContentPlaceHolder1_Name_Reports1_TabContainer1_TabPanel1_dgReports_ctl03_View").click(); } ));

这无法单击该链接

var id = "#ctl00_ContentPlaceHolder1_Name_Reports1_TabContainer1_TabPanel1_dgReports_ctl03_View";
console.log(this.evaluate( function() {document.querySelector(id).click(); } ));

除了我在第二项中使用变量之外,一切都完全相同。

为了完整性,这里是注释掉变量方法的完整函数

casper.then(function () {
    //var id = "#ctl00_ContentPlaceHolder1_Name_Reports1_TabContainer1_TabPanel1_dgReports_ctl03_View";
    //console.log(this.evaluate( function() {document.querySelector(id).click(); } ));
    console.log(this.evaluate( function() {document.querySelector("#ctl00_ContentPlaceHolder1_Name_Reports1_TabContainer1_TabPanel1_dgReports_ctl03_View").click(); } ));
    console.log("Clicked: " + id);
});

这是我正在寻找的元素

<a id="ctl00_ContentPlaceHolder1_Name_Reports1_TabContainer1_TabPanel1_dgReports_ctl03_View" class="lblentrylink" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$Name_Reports1$TabContainer1$TabPanel1$dgReports$ctl03$View','')">View Report</a>
javascript phantomjs casperjs
1个回答
1
投票

您可以使用以下方法将参数传递给casper.evaluate()

casper.then(function () {
  var id = '#ctl00_ContentPlaceHolder1_Name_Reports1_TabContainer1_TabPanel1_dgReports_ctl03_View';

  this.evaluate(function (id) {
    document.querySelector(id).click();
  }, id);

  console.log('Clicked: ' + id);
});
© www.soinside.com 2019 - 2024. All rights reserved.