簡體   English   中英

WebBrowser和DocumentCompleted事件

[英]WebBrowser and DocumentCompleted event

使用WebBrowser DocumentCompleted事件時,我遇到了一些問題。 我有此代碼:

int timesToRun = 20;
private void Time_Tick(object sender, EventArgs e)
{
    if (timesToRun >= siteList.SiteLists.Count)
    {
        timesToRun = 0;
        webBrowser1.DocumentCompleted -= this.webBrowser1_DocumentCompleted;
        Console.WriteLine("timer is done");
        timer.Stop();
    }
        timer.Enabled = false;
        Console.WriteLine("timer is now disabled");
        Console.WriteLine(timesToRun);
        string currentSite = siteList.GetSiteFromIndex(timesToRun);
        webBrowser1.Navigate(currentSite);
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser wb = (WebBrowser)sender;
    if (e.Url.AbsolutePath != wb.Url.AbsolutePath) //somtimes dosn't return true
    {
        Console.WriteLine(wb.Url);
        forumAction.FillOutFormIn(wb.Document);
        timesToRun++;
        Console.WriteLine("timer is now enabled");
        timer.Enabled = true;
    }  
}

我的問題是, 如果陳述僅適用於某段時間。 林不知道我還能檢查更多的東西,以確保可以使用瀏覽器。

我還能檢查什么? 還是我應該對這個問題采取不同的態度?

我從stackOverFlow中的一個不同問題中得到了這個陳述,因為我忽略了一個問題,即事件多次被觸發(因為iframe和東西)。

(對不起我的英語不好)

我認為問題在於瀏覽器的網址可能與嵌套IFrame的網址不同,因此當您調用

e.Url.AbsolutePath != wb.Url.AbsolutePath

如果DocumentCompleted事件是由一個IFrame觸發,則在URL e.Url是iframe的一個,而wb.Url是在網頁瀏覽器中包含頁面的URL。

如果您需要檢查瀏覽器是否可以使用,則可能需要等待很長時間。 您可以遍歷( WB1.Document.Window.Frames )的Frames屬性,以查看加載是否完成。

您可以使用像watin這樣的外部庫,它可以很好地檢查文檔的完成情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM