我有44個html地址,我希望每次都導航到一個html地址。我如何使用webbrowser每次瀏覽一個html頁面?
for (int i = 1; i < 45; i++)
{
adrBarTextBox.Text = sourceUrl + i;
getCurrentBrowser().Navigate(adrBarTextBox.Text);
}
在web瀏覽器文件完成的事件,我需要,只有當它完成導航和加載所有包括JavaScript和一切才把導航到下一個HTML頁的地址。
private void Form1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// If page loaded completly then do something
}
的問題是,該環路將嘗試每次導航加載的最後一個前一個新的HTML頁面和我做了它在完成事件之前的東西。
編輯
按鈕單擊事件:
private void toolStripButton3_Click(object sender, EventArgs e)
{
GetHtmls();
CheckQueue();
}
然後GetHtmls方法:
private Queue<Uri> myUrls = new Queue<Uri>();
private bool isBusy = false;
private void GetHtmls()
{
for (int i = 1; i < 45; i++)
{
adrBarTextBox.Text = sourceUrl + i;
targetHtmls = (combinedHtmlsDir + "\\Html" + i + ".txt");
Uri targetUri = new Uri(sourceUrl + i);
myUrls.Enqueue(targetUri);
}
}
然後checkQueue方法:
private void CheckQueue()
{
if (isBusy)
return; // We're downloading some page right now, don't disturb
isBusy = true; // OK, let's get started
if (myUrls.Count == 0) // No more pages to download, we're done
{
isBusy = false;
return;
}
Uri uri = myUrls.Dequeue(); // Get one URL from queue
getCurrentBrowser().Navigate(uri);
}
和最後完成事件:
private void Form1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
myUrls.Dequeue();
// If page loaded completly then do something
isBusy = false; // We're done
CheckQueue(); // Check next page in queue
}
Lukasz即時獲取右側的這一行上的錯誤:Uri uri = myUrls.Dequeue();錯誤不能將類型'object'隱式轉換爲'System.Uri'。明確的轉換存在(你是否缺少演員?) – 2015-02-24 15:17:41
我想你在隊列聲明中忘記'' –
2015-02-24 15:18:59
Lukasz我試過了,我看到的是當它的導航沒有看到頁面完全加載時,它的去完成的事件再次執行checkQueue()並且下一個uri是3我的意思是第一個uri是1然後是3然後是7然後是9.另一件事是我在哪裏添加/獲得源代碼的一部分,如view-source來自每個uri的內容並將其寫入文本文件? – 2015-02-24 15:38:16