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

时间:2017-08-15 22:35:54

标签: javascript phantomjs casperjs

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

这很好用

New list: []
Rev list: []
New list: [(0 => 5)]
Rev list: [(0 => 5)]
New list: [(1 => 3),(0 => 6)]
Rev list: [(0 => 6),(1 => 3)]
New list: [(2 => 1),(1 => 4),(0 => 7)]
Rev list: [(0 => 7),(1 => 4),(2 => 1)]
New list: [(3 => 9),(2 => 2),(1 => 5),(0 => 8)]
Rev list: [(0 => 8),(1 => 5),(2 => 2),(3 => 9)]
New list: [(4 => 7),(3 => 0),(2 => 3),(1 => 6),(0 => 9)]
Rev list: [(0 => 9),(1 => 6),(2 => 3),(3 => 0),(4 => 7)]
New list: [(5 => 5),(4 => 8),(3 => 1),(2 => 4),(1 => 7),(0 => 0)]
Rev list: [(0 => 0),(1 => 7),(2 => 4),(3 => 1),(4 => 8),(5 => 5)]
New list: [(6 => 3),(5 => 6),(4 => 9),(3 => 2),(2 => 5),(1 => 8),(0 => 1)]
Rev list: [(0 => 1),(1 => 8),(2 => 5),(3 => 2),(4 => 9),(5 => 6),(6 => 3)]
New list: [(7 => 1),(6 => 4),(5 => 7),(4 => 0),(3 => 3),(2 => 6),(1 => 9),(0 => 2)]
Rev list: [(0 => 2),(1 => 9),(2 => 6),(3 => 3),(4 => 0),(5 => 7),(6 => 4),(7 => 1)]
New list: [(8 => 9),(7 => 2),(6 => 5),(5 => 8),(4 => 1),(3 => 4),(2 => 7),(1 => 0),(0 => 3)]
Rev list: [(0 => 3),(1 => 0),(2 => 7),(3 => 4),(4 => 1),(5 => 8),(6 => 5),(7 => 2),(8 => 9)]

无法点击链接

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);
});

1 个答案:

答案 0 :(得分: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);
});