I have my own application, in which I use the webbrowser to go to the site, where I want to, by clicking a button from the application, press 2 buttons on the site. Those. First, I press one button, the page is loaded, and there is the 2nd button I need.

The problem is that after I press the 1st button, the second one does not press, but if I press the button again from the application, then the 2nd button presses. Apparently the page does not have time to render before the 2nd button is pressed. How to make so that the 2nd button is pressed only when the page is fully loaded?

Here is the code:

private void button3_MouseClick(object sender, MouseEventArgs e) { HtmlWindow frames = webBrowser1.Document.Window.Frames[0]; var elements1 = frames.Document.GetElementsByTagName("input"); foreach (HtmlElement element in elements1) { string attrValue = element.GetAttribute("value"); if (attrValue == "Кнопка1") { element.InvokeMember("click"); break; } } //ищем 2ю кнопку и пробуем нажать frames = webBrowser1.Document.Window.Frames[0]; var elements = frames.Document.GetElementsByTagName("img"); foreach (HtmlElement element in elements) { string attrValue = element.GetAttribute("alt"); if (attrValue == "Кнопка2") { element.InvokeMember("click"); break; } } } 

I tried to set delays, between pressing the 1st and 2nd buttons, but then the application behaves strangely, first the delay works, and then the 1st button is pressed.

  • one
    In your case, you need to wait until the new page loads after pressing the first button. To do this, you need to handle the DocumentCompleted event. Roughly speaking, the code for pressing the second button should be in this event. - Alexander Petrov
  • Yes, it worked) - Pyrejkee
  • @AlexanderPetrov Post your comment as an answer, please. - andreycha

1 answer 1

In your case, you need to wait until the new page loads after pressing the first button. To do this, you need to handle the DocumentCompleted event. Roughly speaking, the code for pressing the second button should be in this event.