Handle IE To filling a form c#(处理 IE 填写表单 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 的实例.首先,使用 TlbImp.exe ieframe.dll
创建互操作程序集 SHDocVw.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");
以与使用 WebBrowser
控件类似的方式使用它.
Use it in a similar way you used WebBrowser
control.
也就是说,我相信您仍然可以使用托管的 WebBrowser
来实现您想要实现的目标,只需实现 功能控制 使其行为与 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/发送(取决于你是否想要异步或同步处理它们).这是处理 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");
}
}
}
这篇关于处理 IE 填写表单 c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:处理 IE 填写表单 c#
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01