在webbrowser导航后执行命令 [英] Executing commands after webbrowser navigation

查看:87
本文介绍了在webbrowser导航后执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,

当我正在使用webbrowser时,我想让我的应用程序导航到url,例如,在我之后点击一个按钮,我应该怎么做?问题是我的应用程序总是执行所有命令太快而且最终我的应用程序无法通过id找到元素,因为网页仍未加载。好吧,我试图使用计时器,它似乎工作,但当我正在尝试做我需要在更多网页上导航的应用程序,然后单击按钮我只是得到太多的计时器和它的混乱。我很确定必须有某种方法可以让某种延迟等到页面加载,然后执行命令。已经尝试过基于webbrowser的url的DocumentCompleted事件,但它似乎创建了无限循环。我正在考虑Sleep(),但它会让你的应用程序冻结一段时间,因此'可能不是我正在寻找的东西。我不知道如何在不创建那么多计时器的情况下解决这个问题。任何人都可以帮忙吗?

解决方案

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



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

< a href =http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted.aspx> 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


这篇关于在webbrowser导航后执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆