My aim is to change the DOM of a page before the DOMContentLoaded event. Let's say my JS would look like the code below and I want to change a value of an element:
document.addEventListener("DOMContentLoaded", function(event) {
console.log("Value of element foo: " + document.getElementById('foo').value);
});
I know to change the DOM with the JavascriptExecutor of Selenium WebDriver, but I'm missing how to get it to to execute right before 'DOMContentLoaded' (and maybe that's not the right approach).
// some hook or whatever to execute right before 'DOMContentLoaded' or wherever suitable
((JavascriptExecutor) webDriver).executeScript("document.getElementById('foo').value='hi there'");
In order to do that you will have to set the
pageLoadStrategyfrom defaultnormaltonone.This will pass the control to the next code line immediately after launching the page with
driver.get()method without waiting for the page content to be loaded.