在新窗口中打开链接时,无法在phantomjs中的queryselector.click中打开多个页面(target = _blank)

时间:2017-06-13 12:22:51

标签: phantomjs

The second page cannot be read in phantomjs --my code is below

var webPage = require('webpage');
var page = webPage.create();
page.onLoadFinished = function(){

var title = page.evaluate(function()
{
return document.querySelector("#link1").innerText;
});
console.log(title);

page.evaluate(function(args)
{
document.querySelector("#link1").click();

});

page.onLoadFinished = function(){

var title1 = page.evaluate(function()
{
return document.querySelector("#div1").innerText;
});
console.log(title1);
phantom.exit();
};
};

page.open('http://staging.eubookingsdata.com/scrape1.php', function(status) {

});
Here the first page is "http://staging.eubookingsdata.com/scrape1.php" I want to read the content of div in "http://staging.eubookingsdata.com/scrape2.php" Every thing is working fine ,but when i add target="_blank" in scrape1.php "link" it stopped working. scrape1.php <html>
<body>
<a id="link1" name="link1" href="scrape2.php" target="_blank" >click</a>
</body>
</html> scrape2.php <html>
<body>

<div id="div1" name="div1">hello world</div>
</body>

</html>

1 个答案:

答案 0 :(得分:0)

var webPage = require(&#39;网页&#39;);
var page = webPage.create();
      page.onLoadFinished = function()//第1级
      
      {
        
        var title = page.evaluate(function()
        {
          return document.querySelector(&#34;#link1&#34;)。innerText;
        });
        的console.log(标题); //第1级
  
  
      
    // var newPage = webPage.create();
    // var page1 = page.pages [0];
     
    page.evaluate(function(args){// 1st level
    
        document.querySelector(&#34;#LINK1&#34)。单击();
        
         });
       

};
page.onPageCreated = function(newPage){
     newPage.onLoadFinished = function(status){// 2nd level
    的console.log(状态);
        var title1 = newPage.evaluate(function()
         {
           return document.querySelector(&#34;#div1&#34;)。innerText;
          });
        console.log(title1); //第二级
        phantom.exit();
    };
    };
page.open(&#39; http://staging.eubookingsdata.com/scrape1.php&#39;,功能(状态)

{
 的console.log(&#34;第1页&#34);
 
});