Ivan Climov

Ivan Climov

  • 1k
  • 692
  • 22.6k

Element “webBrowser”. How to wait for the page to load?

Dec 23 2018 3:07 PM

Document structure
"Form0" - Form
- - "panel2" - Panel
- - - "Frm5UC" - Custom item
- - - - "webBrowser1" - Browser

Application logic:
- go to the page in "webBrowser1";
- enter login;
- enter the password;
- click the "Login" button.

If I execute the logic through the code (this is the "Method_0 ()" method), the form does not have time to load in the "Authorization ()" method. I get "webBrowser1.Document = null", error "Object link does not indicate an object instance."

If I do everything through the interface, then everything works.

How to make the logic run programmatically?

 
  1. private void Frm5UC_Load(object sender, EventArgs e)  
  2.         {  
  3.             webBrowser1.Visible = true;  
  4.   
  5.             // *** TESTS ***  
  6.             Method_0();  
  7.         }  
  8.  
  9.  
  10.  
  11.         #region *** TESTS ***  
  12.         public void Method_0()  
  13.         {  
  14.             Method_1();  
  15.             Method_2();  
  16.         }  
  17.   
  18.         public void  Method_1()  
  19.         {  
  20.             textBox2.Text = "_domain_com";  
  21.             textBox1.Text = @"domain_com/login/";  
  22.   
  23.             button1.PerformClick();  
  24.         }  
  25.   
  26.         public void Method_2() // ???????????  
  27.         {  
  28.             Authorization();  
  29.         }  
  30.         #endregion *** TESTS ***  
  31.   
  32.   
  33.         private void button1_Click(object sender, EventArgs e)  
  34.         {  
  35.             webBrowser1.Navigate(textBox1.Text);              
  36.         }         
  37.   
  38.         private void button1_Click(object sender, EventArgs e)  
  39.         {  
  40.             Authorization();  
  41.         }         
  42.   
  43.         public void Authorization() // ???????????  
  44.         {              
  45.                 foreach (HtmlElement he in webBrowser1.Document.GetElementsByTagName("input"))  
  46.                 {  
  47.                     if (he.GetAttribute("name") == "login[login]")  
  48.                     {  
  49.                         he.SetAttribute("value""login798");  
  50.                     }  
  51.                 }  
  52.   
  53.             // Code "enter password"  
  54.             // Code "Press the button"  
  55.         }  
 
 
 

Answers (1)