setContent not working when HTML has assets #11656

Open
tamlyn opened this Issue Oct 2, 2013 · 4 comments

Comments

Projects
None yet
3 participants

tamlyn commented Oct 2, 2013

This script works:

var page = require('webpage').create(); 

var html = '<h1>Test</h1><img>'; //works with page.setContent and page.content
//var html = '<h1>Test</h1><img src=".">'; //only works with page.content

page.setContent(html, 'http://github.com');
//page.content = html;

page.render('test.png');
phantom.exit();

but adding a src attribute to the img makes it fail silently (page.render returns false and no image is generated).

Setting page.content directly works in both cases but then relative URLs don't. The same thing happens with other tags that load a resource such as link. It doesn't matter whether the linked resource exists or not. Tested in 1.8.1 and 1.9.2.

Is this a bug or have I misunderstood the API?

Did you ever find a workaround or solution @tamlyn ?

tamlyn commented Oct 26, 2014

No solution. My workaround was to save the HTML to a web-accessible file and load it using page.open.

I did end up finding a solution the other day - the key was to wait for the onLoadFinished event. Here's a snippet from our code:

        ph.createPage(function (page) {
            page.set('onLoadFinished', function(status) {
                deferred.resolve(page);
            });

            page.set('content', content);
        });

content can have images with remote src attributes and everything works great.

r4j4h commented Feb 4, 2016

Wanted to add there is now a setContent function that attempts to also set a url.

What we had to do though was make sure the url given to it starts with a protocol, e.g. http:// or file:// for requests to trigger.

Also see #12271

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment