0

Im using geckofx browser for loading web page and search some data on webpage.

my code is

public void Navigate(string url){
    //.....some code
    browser.Navigate(url);

    var errorTost = webBrowser.Document.GetElementsByClassName("class-name");
  //....some more code
}

issue im facing is i can not check errorTost is exists or not because browser is not completely loaded.

then i have use webBrowser_DocumentCompleted and webBrowser_Navigating to find whether browser is completely loaded.

new modified code is as bellow

var isBrowserLoading = false; // class variable

public void Navigate(string url){
    //.....some code
    browser.Navigate(url);

    while(isBrowserLoading ){}

    var errorTost = webBrowser.Document.GetElementsByClassName("class-name");
  //....some more code
}
 private void webBrowser_DocumentCompleted(object sender, Gecko.Events.GeckoDocumentCompletedEventArgs e)
    {
        isBrowserLoading = false;
    }

    private void webBrowser_Navigating(object sender, Gecko.Events.GeckoNavigatingEventArgs e)
    {
        isBrowserLoading = true;
    }

but isBrowserLoading is never get updated. Can any one help me on this?!

Thanks.

Roshan
  • 3,236
  • 10
  • 41
  • 63
  • You got true and false backwards. isBrowserLoading should be initially set to TRUE and then when complete set to FALSE. – jdweng Oct 22 '19 at 09:53
  • Remove this: `while(isBrowserLoading ){}`. Move `var errorTost = webBrowser.Document.GetElementsByClassName("class-name");` in the `DocumentCompleted` event (that's when you know the `DomDocument` can be parsed). Btw, is this a WinForms Project? Another (event-driven) GUI Framework? – Jimi Oct 22 '19 at 10:05
  • @Jimi yes winforms project. when i move extraction part to event it will run when browser initially loads. i can use flag to identify it but is there any good way to do this? – Roshan Oct 22 '19 at 10:08
  • @jdweng no it freezing the ui browser is not loading! – Roshan Oct 22 '19 at 10:09
  • Do you mean, you want to parse a specific document, from a specific URI? Are you navigating to a different site when the Control is loaded? You can just add a flag (a `bool` field), setting it to `true` when you want to start parsing the `DOM`. `return` if set to `false`. Or check the current URI before parsing. – Jimi Oct 22 '19 at 10:11
  • @Jimi can i wait still browser loads? becouse i have 10 urls in a for loop, loop running fast than browser loads – Roshan Oct 22 '19 at 10:31
  • Ah, all right. You have the loopy-problem. Solved by not using a loop :) Add the URLs to a List, `Navigate` to the first. When you're done parsing the first URL, call `Navigate()` again from the `DocumentCompleted` event (or the parsing procedure activated by the `DocumentCompleted` event). It can always be tricky if a Document contains more than one Frame (each IFrames contains a Document, for example. Each IFrame can raise the `DocumentCompleted` event). – Jimi Oct 22 '19 at 10:44
  • This is something similar, using the standard WebBrowser control: [How to get an HtmlElement value inside Frames/IFrames?](https://stackoverflow.com/a/53218064/7444103). – Jimi Oct 22 '19 at 10:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201254/discussion-between-roshan-and-jimi). – Roshan Oct 22 '19 at 10:49

0 Answers0