windows 使用 C# 与浏览器交互

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1160474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:50:54  来源:igfitidea点击:

interacting with the browser using C#

c#windows

提问by Anirudh Goel

i wish to interact with my browser window may be IE great if it works on Firefox too, using C#.

我希望与我的浏览器窗口进行交互,如果它也可以在 Firefox 上使用 C#,那么 IE 可能很棒。

I want to make a software which can fill the entries in a webform automatically. In old times there was gator now is roboform, where it can fill in the values automatically.

我想制作一个可以自动填写网络表单中条目的软件。过去有 gator 现在是 roboform,它可以自动填充值。

I actually have users who are comfortable working on an old windows forms application, so i want to make a solution where they can still enter the data in their windows application and it actually fills in the entries at the web form and acts as if the request had generated from the browser itself.

我实际上有一些用户可以轻松地使用旧的 Windows 窗体应用程序,所以我想制定一个解决方案,让他们仍然可以在他们的 Windows 应用程序中输入数据,并且它实际上填写了 Web 表单中的条目,并且就像请求一样是从浏览器本身生成的。

I know i can merge both the databases, since it is a legacy application re writing the database for windows app is a trouble..

我知道我可以合并这两个数据库,因为它是一个遗留应用程序,为 Windows 应用程序重新编写数据库是一个麻烦..

Any suggestion?

有什么建议吗?

回答by AakashM

WatiNis designed to make testing web applications easy from .NET, and it sounds like it could be handy for what you want to do:

WatiN旨在使从 .NET 测试 Web 应用程序变得容易,听起来它对您想要做的事情很方便:

Following is the Hello world example of web test automation; searching Google.

[Test] public void
SearchForWatiNOnGoogle() {  using (IE
ie = new IE("http://www.google.com")) 
{  
ie.TextField(Find.ByName("q")).TypeText("WatiN");
ie.Button(Find.ByName("btnG")).Click();
     Assert.IsTrue(ie.ContainsText("WatiN"));
} }

WatiN Feature List

  • Automates all major HTML elements
  • Find elements by multiple attributes
  • Supports AJAX website testing
  • Supports frames (cross domain) and iframes
  • Supports popup dialogs like alert, confirm, login etc..
  • Supports HTML dialogs (modal and modeless)
  • Works with Internet Explorer 6, 7, 8 and FireFox 2 and 3

以下是 Web 测试自动化的 Hello world 示例;搜索谷歌。

[Test] public void
SearchForWatiNOnGoogle() {  using (IE
ie = new IE("http://www.google.com")) 
{  
ie.TextField(Find.ByName("q")).TypeText("WatiN");
ie.Button(Find.ByName("btnG")).Click();
     Assert.IsTrue(ie.ContainsText("WatiN"));
} }

WatiN 功能列表

  • 自动化所有主要的 HTML 元素
  • 按多个属性查找元素
  • 支持AJAX网站测试
  • 支持框架(跨域)和 iframe
  • 支持弹出对话框,如警报、确认、登录等。
  • 支持 HTML 对话框(模态和非模态)
  • 适用于 Internet Explorer 6、7、8 和 FireFox 2 和 3

回答by s_hewitt

It's billed as a testing application, but Selenium RCcan be used to fill in forms and is fairly easy to setup. You could also check out WatiN. Don't know anything about what security issues you might see though.

它被称为测试应用程序,但Selenium RC可用于填写表格并且相当容易设置。您也可以查看WatiN。不知道您可能会看到哪些安全问题。

回答by jamesaharvey

You might also want to check out Seleniumwhich is a web application testing framework that you can programmitically interact the web UI.

您可能还想查看Selenium,它是一个 Web 应用程序测试框架,您可以通过编程方式与 Web UI 交互。

回答by Daniel Earwicker

If you use fiddleryou may be able to see what the browser sends back to the server, and so you could write C# code to generate the same kind of HTTP request.

如果您使用fiddler,您可能能够看到浏览器发送回服务器的内容,因此您可以编写 C# 代码来生成相同类型的HTTP 请求

If the interaction is very complex (it often is with modern webapps), you could instead automate the browser, as you suggested.

如果交互非常复杂(通常是现代 web 应用程序),您可以按照您的建议自动化浏览器。

I've had some success automating IE by using the InternetExplorer.Applicationobject. It basically launches a copy of IE and lets you control it from code. I wrote a script this way a few years ago to search for cheap train ticket reservations for me on the Virgin Trains website.

我通过使用InternetExplorer.Application对象在自动化 IE 方面取得了一些成功。它基本上启动了 IE 的副本,并让您从代码中控制它。几年前,我以这种方式编写了一个脚本,以便在 Virgin Trains 网站上为我搜索便宜的火车票预订。

The problem was that with some IE installs, it would sometimes stop to give security warnings that I couldn't skip automatically. There didn't seem to be a pattern to this.

问题是在某些 IE 安装中,它有时会停止发出我无法自动跳过的安全警告。这似乎没有规律可循。

回答by IRBMe

If your users are simply using the application via a WindForms application, then is there any particular reason why you have to manipulate the user interface of an existing web browser, such as Internet Explorer, rather than just making the necessary HTTP requests yourself in your WinForms application? You can use the WebRequestclass by setting the Methodproperty to "POST"and writing the field data to the Stream, which you can get using the httpRequest.GetRequestStream() method.

如果您的用户只是通过 WindForms 应用程序使用该应用程序,那么您是否有任何特殊原因必须操作现有 Web 浏览器(例如 Internet Explorer)的用户界面,而不仅仅是自己在 WinForms 中发出必要的 HTTP 请求应用?您可以通过将Method属性设置为“POST”并将字段数据写入流来使用WebRequest类,您可以使用 httpRequest.GetRequestStream() 方法获取该流。