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?
private void Frm5UC_Load(object sender, EventArgs e)
{
webBrowser1.Visible = true;
// *** TESTS ***
Method_0();
}
#region *** TESTS ***
public void Method_0()
{
Method_1();
// Method_2();
}
public void Method_1()
{
textBox2.Text = "_domain_com";
textBox1.Text = @"domain_com/login/";
button1.PerformClick();
}
public void Method_2() // Авторизация
{
Authorization();
}
#endregion *** TESTS ***
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
Authorization();
}
public void Authorization() // Авторизация
{
foreach (HtmlElement he in webBrowser1.Document.GetElementsByTagName("input"))
{
if (he.GetAttribute("name") == "login[login]")
{
he.SetAttribute("value", "login798");
}
}
// Code "enter password"
// Code "Press the button"
}
Update.
I try to use the event "DocumentCompleted".
Added the variable "bool statusAuthorization;".
As a result: - a page with fields for entering login / password opens; and nothing else happens. The code does not enter the login / password.
I try to do debugging.
Steps through the entire code.
No errors. Everything works, but the form with the browser does not open.
If I log in through the interface, then everything works.
bool statusAuthorization;
private void Frm5UC_Load(object sender, EventArgs e)
{
webBrowser1.Visible = true;
statusAuthorization = true; // !!! CHANGES
// *** ТЕСТ ***
Method_0();
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.ReadyState != WebBrowserReadyState.Complete) return;
if (statusAuthorization == true)
{
Authorization();
}
}
#region *** TESTS ***
public void Method_0()
{
Method_1();
// Method_2();
}
public void Method_1()
{
textBox2.Text = "_domain_com";
textBox1.Text = @"domain_com/login/";
button1.PerformClick();
}
public void Method_2() // Авторизация
{
Authorization();
}
#endregion *** TESTS ***
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
Authorization();
}
public void Authorization() // Авторизация
{
foreach (HtmlElement he in webBrowser1.Document.GetElementsByTagName("input"))
{
if (he.GetAttribute("name") == "login[login]")
{
he.SetAttribute("value", "login798");
}
}
// Code "enter password"
// Code "Press the button"
statusAuthorization = false; // !!! CHANGES
}
Update
Result: the form loads again and again.
private void Frm5UC_Load(object sender, EventArgs e)
{
string s = "stop";
webBrowser1.Visible = true;
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompletedHandler);
// *** ТЕСТ ***
Method_1();
}
private void DocumentCompletedHandler(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//Done!
Authorization();
}