Java Selenium,在javascript激活后存储更新的页面源代码

Java Selenium, storing updated page source after javascript activation

本文关键字:更新 源代码 存储 Selenium javascript 激活 Java      更新时间:2023-09-26

我已经设法用我的链接打开了一个浏览器并激活了javascript,这使得页面可以显示更多结果。完成此操作后,我将尝试在控制台中打印新更新的页面源代码,但它显示的只是javascript激活之前的原始源代码。到目前为止,我的代码如下所示。

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!");
}

你必须获取 body 的属性 "innerHTML" 而不是 pageSource:

String bodyHtml = driver.findElement(By.tagName("body")).getAttribute("innerHTML");