Nidje Nidje - 3 years ago 446
HTML Question

Java Selenium, storing updated page source after javascript activation

I have managed to open a browser with my link and activate the javascript, which allows the page to display more results. Once this is done I am trying to print the new updated page source in the console but all it shows is the original source prior to the javascript activation. My code so far is shown below.

WebDriver driver = new FirefoxDriver();

driver.get("www.desiredLink.com");

if (driver instanceof JavascriptExecutor)
{
((JavascriptExecutor)driver).executeScript("javascriptFunction();");
System.out.println(driver.getPageSource());
}
else
{
throw new IllegalStateException("No support for JavaScript!");
}

Answer Source

You have to get attribute "innerHTML" of body instead of pageSource:

String bodyHtml = driver.findElement(By.tagName("body")).getAttribute("innerHTML");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download