Search code examples
c#wpfgeckogeckofx

Navigate to multiple urls with GeckoBrowser


I am trying to navigate through a list of url and get some content from the webpages via GeckoBrowser. The problem is that the OnDocumentCompleted is propably on a different thread and the for loop is not stopped so it keeps on going

I have tried to refactor with multiple browsers but it didnt work

   private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        browser.DocumentCompleted += OnDocumentCompleted;
        host.Child = browser;
        GridWeb.Children.Add(host);

        #region Collect All Offers
        foreach (var site in sites.OrderBy(x => x.Name))
        {
            _site = site;
             url = site.Url;
             browser.Navigate(site.Url);
        }
        #endregion

    }

So from the above code i would expect that the for loop would await the OnDocumentCompleted event before moving on.

Any ideas?


Solution

  • You're right, the DocumentCompleted event won't block your for loop. Rather than using a for loop, inside OnDocumentCompleted() method you wrote, grab the data you want from the site, and then Navigate() to the next url in the list.

    Also if you're just trying to download web data, the browser is overkill unless the site is really complicated and uses javascript to render things etc. If you're just trying to get simple html from a site, just use WebClient:

    string html = new WebClient().DownloadString("https://www.google.com");
    

    The WebClient.DownloadString() method does block, so you can use it in a for loop as you intended above.