简体   繁体   中英

How to get text after webpage is loaded in C# WPF Application

I have a WPF Application which is supposed to read text from a webpage (which is using angular) once it has loaded. Is there any way to wait for the webpage to fully load and then get the text displayed? I have searched for answers for a while and none of them seemed to work for me.

Thanks in advance!

This is from a Winforms application that I needed to use a similar thing for and is in VB, but can be easily converted to C# if you use telerik convertor. If you can share your code I may be able to refine my answer for you

Private Property pageready As Boolean = False

Private Sub WaitForPageLoad()
    AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
        Dim blocker = WebBrowser1.Document.Window.Frames(0).Frames(3).Frames(4).Document.GetElementsByTagName("head")
    End While
    pageready = False

End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub

In this example as you can probably see I was working with multiple frames, but this waits for the page to be ready before doing what I needed it to do

this is the C# conversion

bool pageready = False;

private void WaitForPageLoad()
{
WebBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(PageWaiter);
while (!pageready) {
    Application.DoEvents();
    dynamic blocker = WebBrowser1.Document.Window.Frames(0).Frames(3).Frames(4).Document.GetElementsByTagName("head");
}
pageready = false;

}

private void PageWaiter(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (WebBrowser1.ReadyState == WebBrowserReadyState.Complete) {
    pageready = true;
    WebBrowser1.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(PageWaiter);
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM