Web 浏览器控件在调用表单提交方法时在 IE 中打开下一页



我正在尝试创建自动表单提交应用程序。我需要在不同的网站上订阅。它涉及提交两份表格。我正在使用 .Net Web 浏览器控件。当我填写第一个表单并调用表单的提交方法或按钮的单击方法时,两者都会在IE中打开下一页。(在窗口浏览器中,第一个表单在点击提交按钮后在新选项卡中打开下一个表单)

HtmlElementCollection inputs = form.GetElementsByTagName("input");
foreach (HtmlElement input in inputs)
{
       if (input.GetAttribute("type") == "text")
       {
            inputname = input.GetAttribute("name");
            input.SetAttribute("value", email);
       }
        else if (input.GetAttribute("type") == "submit")
        {
             //input.InvokeMember("click");
             form.InvokeMember("submit");
             break;
         }
 }

所以问题是它不会在控件中打开下一个窗体,而是在 IE 中打开它。我怎样才能让它在控件中打开下一个窗体。

任何帮助将不胜感激。谢谢

以下内容对我有用。按原样尝试,找出与您的代码不同的内容。

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinformsWb
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        const string HTML_DATA =
            "<form target='_blank' action='http://requestb.in/yc89ojyc' method='post' enctype='multipart/form-data'>" +
            "<input type='hidden' name='arg' value='val'>" +
            "<button type='submit' name='go'  value='submit'>Submit</button>" +
            "</form>";
        // this code depends on SHDocVw.dll COM interop assembly,
        // generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
        // and add as a reference to the project
        private async void MainForm_Load(object sender, EventArgs e)
        {
            // make sure the ActiveX has been created
            if ((this.webBrowser.Document == null && this.webBrowser.ActiveXInstance == null))
                throw new ApplicationException("webBrowser");
            // handle NewWindow
            var activex = (SHDocVw.WebBrowser_V1)this.webBrowser.ActiveXInstance;
            activex.NewWindow += delegate(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
            {
                Processed = true;
                object flags = Flags;
                object targetFrame = Type.Missing;
                object postData = PostData != null ? PostData : Type.Missing;
                object headers = !String.IsNullOrEmpty(Headers) ? Headers.ToString() : Type.Missing;
                SynchronizationContext.Current.Post(delegate
                {
                    activex.Navigate(URL, ref flags, ref targetFrame, ref postData, ref headers);
                }, null);
            };
            // navigate to a stream
            using (var stream = new MemoryStream())
            using (var writer = new StreamWriter(stream))
            {
                writer.Write(HTML_DATA);
                writer.Flush();
                stream.Position = 0;
                // naviage and await DocumentCompleted
                var tcs = new TaskCompletionSource<bool>();
                WebBrowserDocumentCompletedEventHandler handler = (s, arg) =>
                    tcs.TrySetResult(true);
                this.webBrowser.DocumentCompleted += handler;
                this.webBrowser.DocumentStream = stream;
                await tcs.Task;
                this.webBrowser.DocumentCompleted -= handler;
            }
            // click the button
            var button = this.webBrowser.Document.GetElementById("go");
            button.InvokeMember("click");
        }
    }
}

最新更新