Search code examples
javascriptweb-scrapingphantomjszombie.js

Can Zombie.js/Phantom.js be used to get HTML of newly open window by window.open?


I am trying to get html of newly open window after activating a link that uses javascript by zombie.js.

Here is the html code

<html>
<head>
    <script type="text/javascript">
        function newin(id)
        {
            var url="page.php?id="+id;
            window.open(url,id,"toolbar=no,location=top,directories=no,status=no,scrollbars=yes,hscroll=no,resizable=yes,copyhistory=no,width=1025,height=1250");
        }
    </script>
</head>

<body>
    <div>
        <a href="javascript:newin(123)">123</a><br/>
        <a href="javascript:newin(234)">234</a><br/>
        <a href="javascript:newin(345)">345</a><br/>
    </div>
</body>

The Script I am using is:

var Browser = require("zombie");
var browser = new Browser();
browser.visit("http://localhost:8000/testpage.html", function () {
    browser.wait(function(){
        var selector = "a[href*='newin']";
        var elements = browser.queryAll(selector);
        for (var e=0;e<elements.length;e++){
            browser.clickLink(elements[e],function(){
                browser.wait(function(){
                    console.log(browser.html());
                });
             });
         }
    });
});

I am not able to get HTML of any window.Any ideas what is wrong in this code ? Or is this possible with phantomjs??


Solution

  • Finally I come to know that if a link contains JavaScript directly in the href or action, Zombie seems to understand that as opening a new page like a normal hyperlink would. While the JavaScript is still executed correctly, the DOM is lost as a result of Zombie trying to load the invalid target as a new page.

    A problematic link would be e.g.

    <a href="javascript:void(0)">test</a>
    

    There’s no support for javascript:links, it is still an open issue:

    https://github.com/assaf/zombie/issues/700