Phillip Watts Ответов: 0

С# подождите загрузки веб-страницы до тех пор, пока элемент не будет присутствовать


Wait load webpage until element is present

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

Richard MacCutchan

Это неправильный способ сделать это. Вы должны добавить обработчики событий, которые будут получать уведомление о событии, когда загрузка страницы завершится.

0 Ответов