且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在webbrowser导航后执行命令

更新时间:2023-11-11 21:29:28

IF当然,任何类型的延迟都是完全毫无意义的,而不仅仅是缓慢的。



看起来你需要处理事件 System.Windows.Forms.WebBrowser.DocumentCompleted

http://msdn.microsoft.com/en-us/library/system .windows.forms.webbrowser.documentcompleted.aspx [ ^ ]。



或者,要获得相同的效果,请覆盖虚拟方法 System.Windows.Forms.WebBrowser.OnDocumentCompleted

http://msdn.microsoft.com/en-us/library/system.windows.f orms.webbrowser.ondocumentcompleted.aspx [ ^ ]。



导航后可能不是你想要的(但当然,有适当的事件)你可以处理),因为它是在文件开始加载时调用的。



-SA

Hey guys,
When I''m working with webbrowser and I want to let my application navigate to url and for example click a button right after that how should I do that? The problem is that my apps always perform all the commands too quickly and it mostly ends up with my app not being able to find the element by id because web page is still not loaded. Well I tried to use timers and it seems to work but when I''m trying to do apps which I need to navigate on more webpages, then clicking the buttons I just get too many timers and its confusing. I''m pretty sure there must be some way to make some kind of delay to wait until the page loads, then execute the commands. Already tried DocumentCompleted event based on webbrowser''s url but it seems to create infinite loop. I was thinking about Sleep() but it makes your app freeze for certain time so that''s propably not what I''m looking for. I have no idea how to solve this issue without creating that many timers. Can anybody please help?

IF course, any kinds of delay for a fixed amount of times are completely pointless, not just slow.

It looks like you need to handle the event System.Windows.Forms.WebBrowser.DocumentCompleted:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted.aspx[^].

Alternatively, to get the same effect, override the virtual method System.Windows.Forms.WebBrowser.OnDocumentCompleted:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.ondocumentcompleted.aspx[^].

"After navigation" may be not what you want (but of course, there is an appropriate event you can handle), because it is invoked at the moment of time when a document only starts to load.

—SA