通过Javascript更新后获取更改后的HTML内容? (的HtmlUnit) [英] Get the changed HTML content after it's updated by Javascript? (htmlunit)

查看:123
本文介绍了通过Javascript更新后获取更改后的HTML内容? (的HtmlUnit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在查找 javascript更新后如何获取某些HTML 的内容时遇到了一些麻烦。

I'm having some trouble figuring out how to get the content of some HTML after javascript has updated it.

具体来说,我试图从美国获取当前时间海军天文台主时钟。它有一个 h1 元素, ID USNOclk in它显示当前时间。

Specifically, I'm trying to get the current time from US Naval Observatory Master Clock. It has an h1 element with the ID of USNOclk in which it displays the current time.

首次加载页面时,此元素设置为显示正在加载...,然后javascript启动并通过

When the page first loads, this element is set to display "Loading...", and then javascript kicks in and updates it to the current time via

function showTime()
    {
        document.getElementById('USNOclk').innerHTML="Loading...<br />";
        xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null){
            document.getElementById('USNOclk').innerHTML="Sorry, browser incapatible. <BR />";
            return;
        } 
        refresher = 0;
        startResponse = new Date().getTime();
        var url="http://tycho.usno.navy.mil/cgi-bin/time.pl?n="+ startResponse;
        xmlHttp.onreadystatechange=stateChanged;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
    }  

所以,问题是我不知道如何更新时间。当我检查元素时,我看到正在加载...作为 h1 元素的内容。

So, the problem is that I'm not sure how to get the updated time. When I check the element, I see the "Loading..." as the content of the h1 element.

我已经仔细检查过javascript是否已启用,我已经尝试在<上调用 waitForBackgroundJavaScript 函数code> webclient 以及希望它能给javascript时间开始更新内容。但是,到目前为止还没有成功。

I've double checked that javascript is enabled, and I've tried calling the waitForBackgroundJavaScript function on the webclient as well hoping that it would give the javascript time to start updating stuff. However, no success as of yet.

import com.gargoylesoftware.htmlunit._
import com.gargoylesoftware.htmlunit.html.HtmlPage

object AtomicTime {

  def main(args: Array[String]): Unit = {
    val url = "http://tycho.usno.navy.mil/what.html"
    val client = new WebClient(BrowserVersion.CHROME)

    println(client.isJavaScriptEnabled()) // returns true
    client.waitForBackgroundJavaScript(10000)
//    client.waitForBackgroundJavaScriptStartingBefore(10000) //tried this one too without success
    var response: HtmlPage = client.getPage(url)
    println(response.asText())
  }
}

如何触发javascript来更新HTML?

How do I trigger the javascript to update the HTML?

推荐答案

我想通了!

HtmlPage 对象有一个 executeJavaScript(String)哪个可以用来启动 showTime 脚本。然后,一旦脚本实际启动,那么当 waitForBackgroundJavaScript 变得相关时。

HtmlPage objects have an executeJavaScript(String) which can be used to kick off the showTime script. Then, once the script has actually started, that's when waitForBackgroundJavaScript becomes relevant.

我最终得到的代码:

import com.gargoylesoftware.htmlunit._
import com.gargoylesoftware.htmlunit.html.HtmlPage
import com.gargoylesoftware.htmlunit.html.DomElement

object AtomicTime {

  def main(args: Array[String]): Unit = {
    val url = "http://tycho.usno.navy.mil/what.html"
    val client = new WebClient(BrowserVersion.CHROME)

    var response: HtmlPage = client.getPage(url)
    response.executeJavaScript("showTime")

    printf("Current AtomicTime: %s", getUpdatedRespose(response, client))
  }

  def getUpdatedRespose(page: HtmlPage, client: WebClient): String = {
    while (page.getElementById("USNOclk").asText() == "Loading...") {
      client.waitForBackgroundJavaScript(200)
    }
    return page.getElementById("USNOclk").asText()
  }
}

这篇关于通过Javascript更新后获取更改后的HTML内容? (的HtmlUnit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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