我尝试实现这个答案https://stackoverflow.com/a/6473442/4085725来处理新winform中的弹出窗口。 但我不知道如何在完成我的工作后关闭弹出窗口以及如何在浏览器点击元素时设置延迟(我试图设置异步,但我收到错误“异步方法不能有ref参数)。 所以我的问题是如何在完成工作后关闭新表格以及如何设置延迟。 这是部分代码:
void nativeBrowser_NewWindow2(ref object ppDisp, ref bool Cancel)
{
var popup = new Form2();
popup.Browser.ScriptErrorsSuppressed = true;
popup.Show(this);
ppDisp = popup.Browser.ActiveXInstance;
//Here I need a delay
popup.Browser.Document.GetElementById("pass").SetAttribute("value", "password");
// Here I need other delay
// here I want to close the popup form
}
这是完整的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using SHDocVw;
namespace alta_solutie
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SHDocVw.WebBrowser nativeBrowser;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
nativeBrowser = (SHDocVw.WebBrowser)webBrowser.ActiveXInstance;
nativeBrowser.NewWindow2 += nativeBrowser_NewWindow2;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
nativeBrowser.NewWindow2 -= nativeBrowser_NewWindow2;
base.OnFormClosing(e);
}
void nativeBrowser_NewWindow2(ref object ppDisp, ref bool Cancel)
{
var popup = new Form2();
popup.Browser.ScriptErrorsSuppressed = true;
popup.Show(this);
ppDisp = popup.Browser.ActiveXInstance;
//Here i need a delay
popup.Browser.Document.GetElementById("pass").SetAttribute("value", "password");
// Here i need other delay
// here i want to close the popup form
}
/// Login on the site
private async void button1_Click(object sender, EventArgs e)
{
webBrowser.Navigate("http://hitserr.com/");
webBrowser.ScriptErrorsSuppressed = true;
while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
await Task.Delay(1000);
SetParamethers.SendTextNoId(webBrowser, "input", "name", "login", "Username");
SetParamethers.SendTextNoId(webBrowser, "input", "name", "pass", "password");
SetParamethers.ClickNoId(webBrowser, "input", "name", "connect");
await Task.Delay(1000);
foreach (System.Windows.Forms.HtmlElement html in webBrowser.Document.GetElementsByTagName("a"))
{
if (html.InnerText == "Facebook Likes")
{
html.InvokeMember("Click");
break;
}
}
}
// Click on the button who create the popup window
private void button2_Click(object sender, EventArgs e)
{
foreach (System.Windows.Forms.HtmlElement html in webBrowser.Document.GetElementsByTagName("a"))
{
if (html.InnerText == "Like")
{
// when html invoke Click, the window appears
OnLoad(e);
html.InvokeMember("Click");
break;
}
}
}
}
}
谢谢你,我将非常感谢你的回答。