C# parse page content by web browser control

T.Zacks 3,986 Reputation points
2021-06-30T17:11:24.817+00:00

I am facing a issue regarding reading page content by web browser control. i have list<string> where two urls stored,

1) button click enable timer from where load one url into browser control. then DocumentCompleted fired and from there i could find h1 tag by webBrowser1.Document.GetElementsByTagName("h1");

but when next url load into browser then DocumentCompleted fired but save h1 tag could not find by GetElementsByTagName()

why browser control not able to find h1 tag from second page. when each url load into browser control then page content load by ajax.

here i am giving my sample code. please some one tell me what i mistake in code for which i am not able to find h1 tag from second url.

please guide me. full sample code can be found in .Net fiddle https://dotnetfiddle.net/alaqtD

Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,586 Reputation points
    2021-07-01T05:53:58.527+00:00

    I used your code for testing, but the code still cannot meet this judgment condition, so I removed it.

     if (webBrowser1.ReadyState == WebBrowserReadyState.Complete && IsRedirect)  
    

    Then I found that the two links in the code could not get the h1 element, because they did not have this tag in the source code, and then I tested it with other links, and successfully got the element after entering the webBrowser1_DocumentCompleted method every time.

            private void Form1_Load(object sender, EventArgs e)  
            {  
                urls.Add("https://bing.com");  
                urls.Add("https://csharp.net-tutorials.com/linq/linq-query-syntax-vs-method-syntax/");  
                DataCount = urls.Count;  
            }  
    

    Are you sure you used the site in the example when you got h1?


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.