HtmlElement question on how to navigate to new page using the code below

sunny-j
sunny-j used Ask the Experts™
on
Looking at a piece of code and seeking to alter it as below. Any help appreciated.

      private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var doc = webBrowser1.Document;
            var results = doc.GetElementsByTagName("OL")[0];
          foreach (HtmlElement result in results.Children)
            {
                if (result.TagName != "LI")

                {
                    continue;
                }
 
                var publishedInfo = new PublishedInfo();

                publishedInfo.Title =
                    result.GetElementsByTagName("H2")[0].InnerText;
             
          var Link =
                result.GetElementsByTagName("H2")[0].GetElementsByTagName("A")[0].GetAttribute("href");

I now want to navigate to this link and extract information from this link page. How do I do this?

Do i need to set up a new browser page? Any example of how to do this so i am in a position to extract new html elements from the link page will be great. Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi Sunny-j,

I am writing the following code without IDE, please bear with me if you encounter any syntax errors.

This can be bit tricky. So first thing you should do is
webBrowser1.DocumentCompleted -=  webBrowser1_DocumentCompleted;

Open in new window

This way we ensure that the code you have posted does not execute one more time.

Now create another method with the same signature as
private void webBrowser1_SecondDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// Code to process the second page
}

Open in new window


Third step would be
...
...
...
          var Link =
                result.GetElementsByTagName("H2")[0].GetElementsByTagName("A")[0].GetAttribute("href");
// Perform error checking, if requred
webBrowser1.Navigate(Link);

Open in new window


The idea is, you detach the event handle from WebBrowser1 if you want to reuse it - and I think that's how it should be done.
If you don't want to do that - which I strongly recommend against- then get another instance of WebBrowser and attach webBrowser1_SecondDocumentCompleted to it.

Let me know if you have any questions/concerns.

Regards,
Chinmay.

Author

Commented:
Appreciate your answer and taking the time to explain. What I really want is how to access the elements in the link page.

  //navigate to link page and extract the full body of description here.
             
                   webBrowser1.Navigate(new Uri(Link));
                    I want to do something like this here... to access the html elements of this new page
doc2 = webBrowser1.Navigate(new Uri(Link));

                   

Any help on this particular issue most welcome. Thanks again.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
That depends on which elements you want to access? What data you want to capture?
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Author

Commented:
Thank you for your help. I divided the program into steps and that worked. Kind regards.

Author

Commented:
Thank you again for looking at issue.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Glad I could help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial