Phantomjs投掷“此页面使用框架,但您的浏览器不支持它们”错误甚至在设置正确的useragent之后

时间:2014-11-20 05:52:16

标签: javascript phantomjs frames

当我尝试使用phantomjs打开我们内部应用程序的一个网页(适用于IE8)时,' page.content'显示在html消息下面。但是,page.render(" test.png")显示完全加载的页面。 我已经将page.settings.useragent设置为为什么.render()和.content之间的差异?如何调试/解决此问题?

HTML输出:

<html>
<frameset id="mainFrameSet" border="0" frameborder="yes" framespacing="1" rows="37px,*" topmargin="0" leftmargin="0" bordercolor="#cdcdcd">
    <frame name="header" src="<src>" frameborder="0" marginheight="0" bordercolor="#cdcdcd" marginwidth="0" noresize="" scrolling="no" bor
der="0">
    <frame frameborder="0" border="0" name="main" id="main" src="<src>" bordercolor="#cdcdcd" scrolling="auto" leftmargin="0">
    <noframes>
        &lt;BODY&gt;
        &lt;P&gt;This page uses frames, but your browser doesn't support them.&lt;/P&gt;
        &lt;/BODY&gt;
    </noframes>
</frameset>
</html>

我的代码:

var page = require("webpage").create(), testindex = 0, loadInProgress = false;
page.settings.javascriptEnabled = true;
page.settings.localToRemoteUrlAccessEnabled = true;
page.settings.XSSAuditingEnabled = true;
page.viewportSize = { width: 1366, height: 768 };
page.settings.userAgent = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)';
//Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)
//Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)
//Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.8 Safari/534.34
//Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
//Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0)
//Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)
//SpecialAgent

    //Load Login Page
    page.open("<URL>");
    window.setTimeout(function(){
    enterLoginCredentials();
        },10000);

  function enterLoginCredentials (){
    //Enter Credentials
    page.evaluate(function() {
      console.log("Loading login page..:  " + url);
      var uName = document.getElementsByName("username");
      var uPass = document.getElementsByName("password");
      uName[0].value="username";
      uPass[0].value="password";
    });
    window.setTimeout(function(){
    clickLogin();
        },1000);
  }

  function clickLogin(){
    //Login
    page.evaluate(function() {
    var formSubmit = document.forms["loginForm"];
    formSubmit.submit();
    });
    window.setTimeout(function(){
    search();
        },10000);
    page.render("LRMLogin111.png");
  }

function search(){
    page.render("Homepage.png");
    console.log("Homepage "+page.content);
    phantom.exit();
}

1 个答案:

答案 0 :(得分:1)

有些页面甚至在今天使用框架。与您选择的浏览器的开发人员工具相比,PhantomJS不会在主DOM中包含子帧的DOM。例如,请参阅此问题,试图克服这个问题:Dump HTML of page including iframes

如果您确实想要打印和操作子框架,则必须切换到它们。这通常使用page.switchToFrame完成。要获取内容,您可以记录page.frameContent属性。

page.render("Homepage.png");
console.log(page.frameContent); // same as page.content here
console.log("############################################");
page.switchToFrame(0); // first child frame
console.log(page.frameContent); // different as page.content here
console.log("############################################");
page.switchToMainFrame(); // switch back up before switching to the second child
page.switchToFrame(1); // second child frame
console.log(page.frameContent); // different as page.content here
console.log("############################################");
page.switchToMainFrame();
console.log(page.frameContent); // same as page.content here