简体   繁体   中英

C# Webbrowser document , invokemember(“click”), is it possible to wait until that click action is resolved?

What I have going on is a Invokemember("Click"), the problem is I want to be able to grab the resulting innerhtml. The problem is i'm unsure of how/if it's possible to wait until the resulting action of the invokemember("click") is resolved. Meaning, in a javascript when you perform this click it will take you ot the next 20 items listed. However, i'm unsure of how to tell when that javascript will be fully loaded. Below is what I'm using.

private void button1_Click(object sender, EventArgs e)
{
    HtmlElement button = webBrowser1.Document.GetElementById("ctl08_ctl00_InventoryListDisplayFieldRepeater2_ctl00_BlockViewPaging_Next");
    button.InvokeMember("click");
    HtmlElement document = webBrowser1.Document.GetElementsByTagName("html")[0];


}

One possible solution is to modify your "click" event handler in javascript so it changes the value of some hidden input field right before exiting the method (after all work is done). You can attach to the event of changing field from C# code and act when it's fired.

// Your init method - you can call it after `InitializeComponent`
// in the constructor of your form
Init() {
    webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
    webBrowser1.Document.GetElementsByTagName("statusField")[0].AttachEventHandler("onchange", WorkDone);
}

void WorkDone(object sender, EventArgs e) {
    HtmlElement document = webBrowser1.Document.GetElementsByTagName("html")[0];
}

That's the raw solution, I haven't yet checked whether "onchange" is a correct DOM event.

Also, you can't attach to DOM events before the document is completely loaded, that's why I put the attaching logic in the handler of DocumentCompleted event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM