【发布时间】:2018-12-23 18:54:39
【问题描述】:
文档结构
"Form0" - 表格
- - "panel2" - 面板
- - - "Frm5UC" - 自定义项目
- - - - "webBrowser1" - 浏览器
应用逻辑:
- 转到“webBrowser1”中的页面;
- 输入登录名;
- 输入密码;
- 点击“登录”按钮。
如果我通过代码执行逻辑(这是“Method_0()”方法),表单没有时间在“Authorization()”方法中加载。 我得到“webBrowser1.Document = null”, 错误“对象链接不指示对象实例。”
如果我通过界面执行所有操作,那么一切正常。
如何使逻辑以编程方式运行?
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"
}
更新。
我尝试使用事件“DocumentCompleted”。
添加变量“bool statusAuthorization;”。
结果: - 一个用于输入登录名/密码字段的页面打开; 没有其他事情发生。 验证码不输入登录名/密码。
我尝试进行调试。
逐步执行整个代码。
没有错误。一切正常,但浏览器的表单无法打开。
如果我通过界面登录,那么一切正常。
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
}
更新
结果:表单一次又一次地加载。
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();
}
【问题讨论】:
-
您应该使用 WebBrowser 的 DocumentCompleted 事件。
-
@LarsTech 我尝试使用它。不工作。你能告诉我如何正确使用它吗?
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) -
发布您为
DocumentCompleted事件编写的代码。 -
@Jimi 我不明白如何组织代码。用什么方法放在那里?
Test2 ();? -
你可能想要something like this。