简体   繁体   English

WebBrowser和DocumentCompleted事件

[英]WebBrowser and DocumentCompleted event

i got a bit of a problem using the WebBrowser DocumentCompleted event. 使用WebBrowser DocumentCompleted事件时,我遇到了一些问题。 i have this code: 我有此代码:

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

my problem that the if statment is true only somtimes. 我的问题是, 如果陈述仅适用于某段时间。 im not sure what more can i check to be sure the browswer is ready to use. 林不知道我还能检查更多的东西,以确保可以使用浏览器。

what else can i check? 我还能检查什么? or should i take a diffrent approch to this problem? 还是我应该对这个问题采取不同的态度?

i got this if statment from a diffrent question here in stackOverFlow becuse i hed a problem that the event was fired more the onces (becuse iframe's and stuff). 我从stackOverFlow中的一个不同问题中得到了这个陈述,因为我忽略了一个问题,即事件多次被触发(因为iframe和东西)。

(sorry for my english) (对不起我的英语不好)

I think the problem is that the url of the browser can differ from the url of a nested IFrame, so when you call 我认为问题在于浏览器的网址可能与嵌套IFrame的网址不同,因此当您调用

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

if the DocumentCompleted event is triggered by an IFrame, then the url in e.Url is the one of the IFrame while wb.Url is the url of the containing page in the webbrowser. 如果DocumentCompleted事件是由一个IFrame触发,则在URL e.Url是iframe的一个,而wb.Url是在网页浏览器中包含页面的URL。

If you need to check if the browser is ready to use you might have to wait a long time. 如果您需要检查浏览器是否可以使用,则可能需要等待很长时间。 You can loop through Frames property of ( WB1.Document.Window.Frames ) to see if the loading is completed. 您可以遍历( WB1.Document.Window.Frames )的Frames属性,以查看加载是否完成。

You could use external libraries like watin , it has a good check on document completion. 您可以使用像watin这样的外部库,它可以很好地检查文档的完成情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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