I'm trying to execute a js function after js script gets loaded on an HTML page. But the issue is js script takes some time to load on the HTML page. It works fine if I add a sleep for few seconds in my java code and then execute the js function.
index.html
<!DOCTYPE HTML>
<html lang="en">
<head>
</head>
<body>
</body>
<script>
var loadJS = function(url) {
var script = document.createElement("script");
script.src = url;
document.body.appendChild(script);
}
</script>
</html>
Java Code
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("www.test-url.com");
js.executeScript("loadData(123)");
The above code works if I add Thread.sleep(5000); between the above js.executeScript code lines but not when I remove it. How do I replace the
Thread.sleep(5000); and still make it work