AngleSharp / AngleSharp.Js

:angel: Extends AngleSharp with a .NET-based JavaScript engine.

Home Page:https://anglesharp.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

window.onload doesn't fire

DmitriiShamrikov opened this issue · comments

I have a page like this:

<html>
<head>
	<title>test page</title>
	<script>
	window.onload = function() {
		document.body.appendChild( document.createTextNode( 'test' ))
	}
	</script>
</head>
<body></body>
</html>

And code that loads it:

var config = Configuration.Default.WithDefaultLoader().WithJavaScript();
var browser = BrowsingContext.New( config );
var doc = browser.OpenAsync( url ).Result;
Console.WriteLine( doc.Body.OuterHtml );

The output supposed to be <body>test</body> but it is <body></body>.
Am I doing something wrong?

Maybe. Can you also defer the Console.WriteLine? The DOM events are fired asynchronously, i.e., it may take longer for the JS event handler to be triggered. As a simple test wait ~1s with your output. (the 1s is just a very illustrative example, in reality the task should be executed within O(ms), depending on the thread pool and other things like the OS scheduler)

Tried it. Didn't helped.

Have you solved it,I seemed to have the same question

@WeihanLi unfortunately, no. More problems popped up with AngleSharp later. As we needed very advanced functionality regarding scripting and web page lifecycle in our project we decided to use PhantomJS.

As we needed very advanced functionality regarding scripting and web page lifecycle in our project we decided to use PhantomJS.

I think this is a wise decision - keep in mind PhantomJS is abandoned, too, and potentially going to, e.g., Chrome headless (for instance using a wrapper like Puppeteer) makes sense in the long run.

Landed in devel.