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

查看:32
本文介绍了在 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 元素,其 IDUSNOclk,用于显示当前时间.

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 是否已启用,并且我已经尝试在 webclient 上调用 waitForBackgroundJavaScript 函数以及希望它能给 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天全站免登陆