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

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.
sunny-jAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technical NinjaCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sunny-jAuthor 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.
0
Chinmay PatelChief Technical NinjaCommented:
That depends on which elements you want to access? What data you want to capture?
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

sunny-jAuthor Commented:
Thank you for your help. I divided the program into steps and that worked. Kind regards.
0
sunny-jAuthor Commented:
Thank you again for looking at issue.
0
Chinmay PatelChief Technical NinjaCommented:
Glad I could help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.