处理即填写表格C# [英] Handle IE To filling a form c#

查看:147
本文介绍了处理即填写表格C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要浏览一个网站,登录并填写通过我的申请表格没有看到用户的任何东西,最后它展示给用户提交。

I want to navigate a website, login and fill a form through my application without seeing users anything and finally show it to users for submitting.

以前我用网页浏览器控制。它通常但有时工作,在现场提出了用户之后,某些错误提交表单。但在IE这样的错误没有提出用相同的数据。

Previously I used webbrowser control. It works usually but sometimes, some error raised on site after user submit form. but in IE this errors not raised with same data.

有什么办法来浏览和IE浏览器中直接填写我的数据形式,然后展示给用户? (这个网站有这么多的客户端控件,我必须等待他们回应我的数据(后选择状态的例子负荷市)

Is there any way to navigate and fill forms with my data in IE directly and then show it to users? (this site has so many client side controls that I must wait to respond them to my data (for example load cities after select state)

推荐答案

您可以从您的C#应用​​程序首先自动化的Internet Explorer的一个实例,创建互操作程序集 SHDOCVW.DLL TLBIMP.EXE ieframe。 DLL 并将其添加到您的项目的引用,然后使用下面的代码来创建Internet Explorer的进程外的实例:

You can automate an instance of Internet Explorer from your C# app. First, create the interop assembly SHDocVw.dll with TlbImp.exe ieframe.dll and add it as a reference to your project. Then use the following code to create an out-of-process instance of Internet Explorer:

var ie = (SHDocVw.WebBrowser)Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.Application"));
ie.Visible = true;
ie.Navigate("http://www.example.com");

使用它以类似的方式您使用 web浏览器控制。

Use it in a similar way you used WebBrowser control.

这是说,我相信你仍然可以使用托管 web浏览器为你想实现什么,只是实现功能控制使其行为相同的方式IE浏览器(或尽可能接近)。

That said, I believe you still can use hosted WebBrowser for what you want to achieve, just implement feature control to make it behave the same way IE does (or as close as possible).

这个无辜的例子可能然而,有一个隐藏的陷阱。因为你在这里自动外进程内的COM对象,其事件(如果你处理任何),可以从你的主UI线程到达一个线程,不同的。一般情况下,你需要使用封送他们回你的主线程 Control.Invoke SynchronizationContext.Post / < A HREF =htt​​p://msdn.microsoft.com/en-us/library/system.threading.synchronizationcontext.send.aspx相对=nofollow>发送(取决于是否要处理它们异步或同步)。下面是处理的DocumentComplete 的一个例子,同时线程的护理:

This innocent example may however have a hidden catch. Because you automate an out-of-proc COM object here, its events (if you handle any) may arrive on a thread, different from your main UI thread. Generally, you'd need to marshal them back to your main thread using Control.Invoke or SynchronizationContext.Post/Send (depending on whether you want to handle them asynchronously or synchronously). Here's an example of handling DocumentComplete and taking care of threading:

using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;

namespace WinformsIE
{
    public partial class Form1 : Form
    {
         public Form1()
        {
            SetBrowserFeatureControl();
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs ev)
        {
            var ie = (SHDocVw.InternetExplorer)Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.Application"));
            ie.Visible = true;
            Debug.Print("Main thread: {0}", Thread.CurrentThread.ManagedThreadId);
            ie.DocumentComplete += (object browser, ref object URL) =>
            {
                string url = URL.ToString();
                Debug.Print("Event thread: {0}", Thread.CurrentThread.ManagedThreadId);
                this.Invoke(new Action(() =>
                {
                    Debug.Print("Action thread: {0}", Thread.CurrentThread.ManagedThreadId);
                    var message = String.Format("Page loaded: {0}", url);
                    MessageBox.Show(message);
                }));
            };
            ie.Navigate("http://www.example.com");
        }

    }
}

这篇关于处理即填写表格C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆