如何在c# WPF应用中加载网页后获取文本

时间:2023-01-18 17:59:32

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.

我有一个WPF应用程序,它应该在加载后读取网页中的文本(使用角)。是否有办法等待网页完全载入,然后显示文字?我寻找答案已经有一段时间了,但似乎没有一个对我有用。

Thanks in advance!

提前谢谢!

1 个解决方案

#1


0  

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

这是一个Winforms应用程序,我需要在VB中使用类似的东西,但是如果您使用telerik转换器,可以很容易地转换为c#。如果你能分享你的代码,我也许能帮你完善我的答案

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

这是c#转换。

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);
}
}

#1


0  

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

这是一个Winforms应用程序,我需要在VB中使用类似的东西,但是如果您使用telerik转换器,可以很容易地转换为c#。如果你能分享你的代码,我也许能帮你完善我的答案

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

这是c#转换。

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);
}
}