等待加载网页,直到元素存在 [英] Wait load webpage until element is present

查看:82
本文介绍了等待加载网页,直到元素存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c#及其概念的新手,所以如果这个问题有点愚蠢,我很抱歉。


我尝试使用winforms webbrowser控件进行一些自动化。



我正在寻找一种加载方法网页,直到找到一个网页。

等待网页加载,我使用行"等待(30)""
"等待(30) )"这是一个明显的期望,这不是最佳的。



我只需要一个同步下载:

如果网页项是在页面上丢失然后等待1秒,如果找到网页项目完成下载网页。

I am new to c# and its concepts, so i am sorry if this question is kind of dumb.
I try to do some automatation using the winforms webbrowser control.

I'm looking for a method that loads a web page until a web item is found.
to wait for the web page to load, I use line "Wait(30)"
"Wait(30)" this is an obvious expectation and this is not optimal.

I just need a synchronous download:
Find web item if the web item is missing on the page then wait 1 second, if the web item is found finish downloading a web page.

private void Wait(int number)

{

    DateTime time = DateTime.Now;

   做¥
    {

        Application.DoEvents();

    }
    while(time.AddSeconds(number)> DateTime.Now);

}



private void CheckFindElement1()

{

   试试
    {

       等待(30);

        var mlm = web_Browser.Document.GetElementById(" id1");

    }
    catch(例外)

    {

       等待(1);

        CheckFindElement1();

    }¥b $ b}    



    

    

    

希望你能帮助我,抱歉我的英语不好。   

private void Wait(int number)
{
    DateTime time = DateTime.Now;
    do
    {
        Application.DoEvents();
    }
    while (time.AddSeconds(number) > DateTime.Now);
}

private void CheckFindElement1()
{
    try
    {
        Wait(30);
        var mlm = web_Browser.Document.GetElementById("id1");
    }
    catch (Exception)
    {
        Wait(1);
        CheckFindElement1();
    }
}    

    
    
    
Hope u can help me with that, sorry for my bad english.   

推荐答案

嗨Aleks Roth,

Hi Aleks Roth,

感谢您在此发帖。

对于您的问题,当我们想要获取元素ID时,需要完全加载网页。我们不确定网页是否会在30秒内加载。

For your question, when we want to get the element ID, it need the web page load completely. And we are not sure the web page will be loaded in 30 seconds.

请尝试在webBrowser1_DocumentCompleted事件中获取元素ID。

Please try to get the element ID in webBrowser1_DocumentCompleted event.

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var mlm = webBrowser1.Document.GetElementById("id");
        }

最好的问候,

Wendy


这篇关于等待加载网页,直到元素存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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