Java Selenium,在javascript激活后存储更新的页面源 [英] Java Selenium, storing updated page source after javascript activation

查看:19
本文介绍了Java Selenium,在javascript激活后存储更新的页面源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

你必须得到 body 的属性 "innerHTML" 而不是 pageSource:

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

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

这篇关于Java Selenium,在javascript激活后存储更新的页面源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆