WebBrowser.DocumentText
是异步的。您需要先处理好DocumentComplete
,然后才能访问 DOM,并不断泵送 Windows 消息。这是一个完整的网络抓取示例,async/await
用于保持方便的线性代码流。只需更改导航部分:
await NavigateAsync(ct, () => this.webBrowser.DocumentText = leMessage), timeout);
HtmlElementCollection elems = wb.Document.GetElementsByTagName("a");
这样你就可以循环进行。简而言之:
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinformsApp2
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
const string leMessage = "<a href='http://example.com'>Go there</a>";
private async void MainForm_Load(object sender, EventArgs e)
{
var wb = new WebBrowser();
TaskCompletionSource<bool> tcs = null;
WebBrowserDocumentCompletedEventHandler documentCompletedHandler = (sender2, e2) => tcs.TrySetResult(true);
for (int i = 0; i < 3; i++)
{
tcs = new TaskCompletionSource<bool>();
wb.DocumentCompleted += documentCompletedHandler;
try {
wb.DocumentText = leMessage;
await tcs.Task;
}
finally {
wb.DocumentCompleted -= documentCompletedHandler;
}
HtmlElementCollection elems = wb.Document.GetElementsByTagName("a");
foreach (HtmlElement elem in elems)
{
Debug.Print(elem.OuterHtml);
}
}
}
}
}