2017-07-24 299 views
0

试图从这个网页https://weather.com/weather/radar/interactive/l/87562:4:US?layers=rwi在Chrome控制台querySelector工作,但不PhantomJS使用page.evaluate

我在页面上的Chrome浏览器控制台跑document.querySelector('.modal').style="display: none;"截图雷达,摆脱DIV弹出广告拦截雷达图片。但无论出于何种原因,PhantomJS不会让斯巴鲁广告消失。 Current PhantomJS screenshot

我也试着运行document.querySelector('.close').click();来关闭页面上的广告,它使用控制台在Chrome中工作,但不是在PhantomJS中。

我可以在Chrome控制台中运行querySelector并摆脱Subaru广告并截取div #imap,但无论出于何种原因,它都不适用于PhantomJS。

我试着在超时时间内移动querySelector,但没有任何变化。

var page = require('webpage').create(); 
page.viewportSize = { width: 1280, height: 720 }; 


page.open('https://weather.com/weather/radar/interactive/l/87562:4:US?layers=rwi', function (status) { 

page.evaluate(function() { 
    document.querySelector('.modal').style="display: none;" 
    }); 

    setTimeout(function() { 

    var clipRect = page.evaluate(function(){ 

    return document.querySelector('#imap').getBoundingClientRect(); 

     }); 


     page.clipRect = { 
      top: clipRect.top, 
      left: clipRect.left, 
      width: clipRect.width, 
      height: clipRect.height 
     }; 

     console.log(JSON.stringify(clipRect)); 


     page.render('imap2.png'); 

     phantom.exit(); 
    }, 6000); 

任何人都可以指出我在这里失踪的方向吗?

感谢,

大卫

回答

0

我认为你应该这样做:

return document.querySelector('#imap').getBoundingClientRect(); 

这样的:

page.evaluate(function(){ 
     return document.querySelector('#imap').getBoundingClientRect(); 
}) 
相关问题