使用.NET的Web自动化 [英] Web automation using .NET

查看:239
本文介绍了使用.NET的Web自动化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个非常新手程序员。是否有人对你知道该怎么做网络自动化与 C#? 基本上,我只是想实现自动在网络上的一些简单的动​​作。 待我打开了网站链接,我只是想执行的操作自动下方。

I am a very newbie programmer. Does anyone of you know how to do Web automation with C#? Basically, I just want auto implement some simple action on the web. After I have opened up the web link, i just want to perform the actions below automatically.

  1. 自动输入一些值,然后单击运行按钮。
  2. 检查在组合框,然后点击下载按钮。

我如何能做到这一点与 C#?我的朋友把我介绍给使用PowerShell,但我想净就提供了这样的库了。任何建议或链接,我参考?

How can I do it with C#? My friend introduce me to use Powershell but I guess .Net do provide this kind of library too. Any suggestion or link for me to refer?

推荐答案

您可以使用 System.Windows.Forms.WebBrowser 控制(的 MSDN文档)。为了进行测试,它可以让你这样做可以在浏览器中做的事情。它很容易执行JavaScript的,没有任何额外的努力。如果出事了,你就可以直观地看出国家,该网站是

You can use the System.Windows.Forms.WebBrowser control (MSDN Documentation). For testing, it allows your to do the things that could be done in a browser. It easily executes JavaScript without any additional effort. If something went wrong, you will be able to visually see the state that the site is in.

例如:

private void buttonStart_Click(object sender, EventArgs e)
{
    webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
    webBrowser1.Navigate("http://www.wikipedia.org/");            
}

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    HtmlElement search = webBrowser1.Document.GetElementById("searchInput");
    if(search != null)
    {
        search.SetAttribute("value", "Superman");
        foreach(HtmlElement ele in search.Parent.Children)
        {
            if (ele.TagName.ToLower() == "input" && ele.Name.ToLower() == "go")
            {
                ele.InvokeMember("click");
                break;
            }
        }
    }
}


要回答你的问题:如何检查一个复选框


To answer your question: how to check a checkbox

对于HTML:

<input type="checkbox" id="testCheck"></input>

在code:

the code:

search = webBrowser1.Document.GetElementById("testCheck");
if (search != null)
    search.SetAttribute("checked", "true");

实际上,具体的如何在很大程度上取决于什么是真正的HTML。

actually, the specific "how to" depends greatly on what is the actual HTML.

有关处理你的多线程问题:

For handling your multi-threaded problem:

private delegate void StartTestHandler(string url);
private void StartTest(string url)
{
    if (InvokeRequired)
        Invoke(new StartTestHandler(StartTest), url);
    else
    {
        webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        webBrowser1.Navigate(url);
    }
}

InvokeRequired ,检查当前线程是否是UI线程(实际上,该表单创建的线程)。如果不是,那么它会尝试在所要求的线程中运行StartTest

InvokeRequired, checks whether the current thread is the UI thread (actually, the thread that the form was created in). If it is not, then it will try to run StartTest in the required thread.

这篇关于使用.NET的Web自动化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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