Search code examples
google-chromedelayheadless

How to delay capture of DOM using Chrome Headless mode


I am using the following command line with Chrome 70 on Windows 10:

chrome --headless --enable-logging --dump-dom http://localhost/test.html

The page contains the following:

<!DOCTYPE html>
<html>
<head>
    <script>
        setTimeout(function () { document.write("This is a test"); }, 10);
    </script>
</head>
<body>
    Content to replace.
</body>
</html>

Notice that I have script simulating an async process to query data. With the timeout set to 10ms the page output from the Chrome command line is:

<html><head></head><body>This is a test</body></head></html>

When I increase the timeout to 50 or more the output is the original page.

How do I tell Chrome to wait for the completion of an async process and how do I tell it when its complete ?


Solution

  • You can use the --virtual-time-budget parameter which allows you to specify the time you are willing to wait in milliseconds.

    chrome --headless --enable-logging --dump-dom --virtual-time-budget=10000 http://localhost/test.html