I'm attempting to replace the JavaScript functionality of a web page using Selenium (in Java with Firefox Geckodriver, if that makes a difference).
Consider this webpage:
<HTML><HEAD></HEAD>
<BODY>
<DIV id="time">Time</DIV>
</BODY>
<SCRIPT>
!function(){
setInterval(function(){
document.getElementById('time').innerHTML = new Date();
}, 500);
}();
</SCRIPT>
</HTML>
Now, after loading it with Selenium, I am using JavascriptExecutor
to remove the <SCRIPT>
section.
((JavascriptExecutor) driver).executeScript(
"var r = document.getElementsByTagName('script');" +
"for(var i = (r.length - 1); i >=0; i--){" +
" r[i].parentNode.removeChild(r[i]);" +
"}");
Then, I wait for 2 seconds and add a new <SCRIPT>
element.
Thread.sleep(2000);
((JavascriptExecutor) driver).executeScript(
"var newScript = document.createElement(\"SCRIPT\");" +
"newScript.innerHTML = \"document.getElementById('time').innerHTML = 'NEW SCRIPT IS RUNNING';\";" +
"document.body.appendChild(newScript);");
It appears to be working, but the old script is still active and updating the <DIV>
tag with the current time. So, I am looking for a way to stop the active JavaScript threads or instruct Selenium to 'soft' reload the page with the altered DOM tree.