0

在通过 AJAX 函数更新后,我需要获取 webBrowser 的更新 html

例如,我导航到“page1.asp”,但“page1.asp”包含一个从另一个页面加载内容的 iframe。它还有一个 jQoery 函数,可以在 xx 秒后显示一个提交按钮。

我的问题是如何从 webBrowser 获取最新的 html?

我尝试更新但没有运气,

webBrowser.Update();
4

2 回答 2

1

好吧,这是一个有点棘手的问题,您需要正确遵循以下准则才能实现目标。

  1. 您需要在表单上创建一个计时器。用 tick 绑定它并给出大约 500 ms 的间隔。
  2. 在 timer_tick 事件中写下这段代码:

    if (browser.ReadyState == WebBrowserReadyState.Complete)

  3. 一旦您发现就绪状态已完成,您就会寻找您认为在完成 ajax 请求后将更新的 html 元素。

您可以通过这种方式检查任何元素:

        HtmlElementCollection forms = browser.Document.GetElementsByTagName("form");
        HtmlElement form = null;
        foreach (HtmlElement el in forms)
        {
            string name = el.GetAttribute("name");
            if (name == "DATA")
            {
                form = el;
                break;
            }
        }

4)一旦你有了你的元素,你就可以继续你的工作。

更新:这是您可以根据需要扩展的计时器滴答编码确保您已设置Timer1.Inverval = 100(100 毫秒)

    private void Timer1_Tick(sender, args) 
    {
        Application.DoEvents();

// make sure your are pulling right element id
        HtmlElement cTag = webBrowser.Document.GetElementById("myelement");         

        if(cTag != null) // if elemnt is found than its fine. 
        { 
            cTag.SetAttribute("value", "Eugene");
            Timer1.Enabled = false;
        } 
        else 
        {
// dont worry, the ajax request is still in progress... just wait on it and move on for the next tick. 
        }
        Application.DoEvents(); // you can call it at the end too.
    }
于 2013-03-08T20:51:46.007 回答
0

使用WebBrowser.DocumentText().

来自 MSDN

此属性包含当前文档的文本,即使已请求另一个文档。如果设置此属性的值,然后立即再次检索它,如果 WebBrowser 控件没有时间加载新内容,则检索到的值可能与设置的值不同。您可以在 DocumentCompleted 事件处理程序中检索新值。或者,您可以通过在循环中调用 Thread.Sleep 方法来阻止线程直到加载文档,直到 DocumentText 属性返回您最初设置的值。

尝试在 DocumentCompleted() 事件处理程序中运行调用 DocumentText()。如果因为 iFrame 不引发此事件而无法执行此操作,则只需设置一个计时器并每隔几秒检索一次文本。

于 2013-03-08T20:36:08.083 回答