java - Selenium 没有提取与网站上相同的 HTML

标签 java html selenium webdriver

我正在开发一个项目,该项目连接到成绩查看器并从网站中提取 html。然而,当它这样做时,它似乎失去了一些东西。我正在连接到该页面并使用 Selenium WebDriver 打印 Web 源代码,但它生成的 html 与我在页面上看到的 html 略有不同。只是到处都缺少一些小块。这是我的代码:

    //get into frame
    driver.switchTo().frame(driver.findElement(By.id("sg-legacy-iframe")));//get inside iFrame grades are held in   
    WebElement full = driver.findElement(By.id("btnView"));
    full.submit();//click "show full view button"

    //Print out source
    PrintWriter pw = new PrintWriter(new FileWriter(new File("grades.txt")));
    pw.println(driver.getCurrentUrl());//confirms the driver is on the correct page
    pw.println((driver.getPageSource()));//prints out html
    pw.close();

我怀疑这可能是在页面和 iFrame 之间切换时出现的某种 cookie 问题,但我真的不知道。我还有一份应该获取的正确 HTML 代码的副本,它是实际输出,但它们很大,无法放入正文中。这些是指向 HTML 预期和输出的链接,其中任何 secret 信息均已更改。主要问题是找不到“AssignmentClass”div。

Desired HTML Output(HTML of the site)

HTML being output by my program

如果有人能解释为什么会发生这种情况或如何解决,我会永远爱你。

最佳答案

在我过去的一个项目中,我使用 getAttribute() 来获取 html 的源代码。那么沿着这些思路,你已经尝试过了吗?

driver.switchTo().frame(driver.findElement(By.id("sg-legacy-iframe")));
WebElement full = driver.findElement(By.id("btnView"));
full.submit();//click "show full view button"
WebElement body = driver.findElement(By.tagName("body"));

//Print out source
PrintWriter pw = new PrintWriter(new FileWriter(new File("grades.txt")));
pw.println(driver.getCurrentUrl());//confirms the driver is on the correct page
pw.println(body.getAttribute("innerHTML"));//prints out html
pw.close();

关于java - Selenium 没有提取与网站上相同的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28801174/

相关文章:

javascript - 如何使用 selenium 驱动程序选择最后一个 'li',该值在 'Ul' 中每次运行时都会动态变化

java - 将 Android ListView 行高设置为最大行

iphone - 通过 iPhone Web App 访问相机

html - "Source"和 "Generated Source"有什么区别?

php - 如何在div中回显php变量

python - browserstack.com API 有什么意义?

java帮助小程序 Action 监听器

java - 在 .jj 文件中检测到左递归

com.sun.mail.imap.MessageCache.getMessage(MessageCache.java :123) 处出现 java.lang.ArrayIndexOutOfBoundsException

python - 防止在 Python 驱动的 PhantomJS/Selenium 中下载 CSS/其他资源