程序自动表单字段项,导致网页的检查 [英] Program to automate form field entry and result checking of webpage

查看:139
本文介绍了程序自动表单字段项,导致网页的检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建在C#程序(也许使用的WinForms),它会进入一个许可证号到特定的网站和确认车牌号码是否是当前有效的许可证。



我我不能确定从哪里开始,因为我甚至无法找到该网站的源代码中的表单字段id和我不能确定该网站使用的是什么技术。



此外,这一计划的目的是要输入许可号码列表,并返回每个许可证的名称和验证状态。 ,数据源作为网站



这是如何去这方面的消息将不胜感激,我是一个中间的C#开发者 - 已经在ASP.Net主要工作,但感觉的WinForms可能更适合这个项目。



亲切的问候


解决方案

您可以使用 web浏览器 控制:




  1. 您可以通过加载页面 WebBrowser1.Navigate时(网站的网址)

  2. 找到使用页面元素 webBrowser1.Document.GetElementById(buttonid)你也可以遍历的HtmlElement webBrowser1.Document.Body.All 和检查,例如 element.GetAttribute(值)==一些vaule找到它。

  3. 使用元素设定值 element.InnerText =一些价值 element.SetAttribute(价值,某些价值)

  4. 通过调用提交表单或使用 element.InvokeMember(办法)

  5. 的提交按钮点击

    示例



    例如,如果你浏览谷歌和看页面的源代码,你会看到搜索文本框中的名称是q和包含搜索框形式的名称为F,这样你就可以写代码来自动搜索。




    1. 创建名称的形式 BrowserSample

    2. 从工具箱中,将 web浏览器和形式下降。

    3. Hanfdle 加载形式的事件并导航到谷歌。

    4. 拉手 DocumentCompleted webBrowser1 并找到˚F并找到并设置的InnerText 并调用提交的˚F。导航和文档加载完成后,此事件。

    5. 在实际的应用中添加所需的空检查。



    代码:

     私人无效BrowserSample_Load(对象发件人,EventArgs五)
    {
    this.webBrowser1.Navigate(https://www.google.com/);
    }

    无效webBrowser1_DocumentCompleted(对象发件人,WebBrowserDocumentCompletedEventArgs E)
    {
    //因为提交˚F导致导航
    //到pervent一个循环,我们检查导航
    的URL //如果它是从谷歌的网址,返回
    如果(e.Url.AbsoluteUri!=htt​​ps://www.google.com/)
    不同返回;
    变种F = this.webBrowser1.Document.Body.All.GetElementsByName(F)
    .Cast&所述;的HtmlElement>()
    .FirstOrDefault();

    变种Q = f.All.GetElementsByName(Q)
    .Cast<&的HtmlElement GT;()
    .FirstOrDefault();

    q.InnerText =C#WebBrowser控件
    f.InvokeMember(提交);
    }

    如果你执行程序时,它首先导航到谷歌,然后显示搜索结果





    在你的特殊情况



    由于该网站用ajax加载的内容,那么你应该在 DocumentCompleted 延迟:

     异步无效webBrowser1_DocumentCompleted(对象发件人,WebBrowserDocumentCompletedEventArgs E)
    {
    如果(e.Url.AbsoluteUri!= https://www.onegov.nsw.gov.au/PublicRegister/#/publicregister/search/Security)
    的回报;

    等待Task.Delay(5000);
    变种F = this.webBrowser1.Document.Body.All.GetElementsByName(searchForm)
    .Cast<&的HtmlElement GT;()
    .FirstOrDefault();

    变种Q = f.All.GetElementsByName(SEARCHTEXT)
    .Cast<&的HtmlElement GT;()
    .FirstOrDefault();

    q.InnerText =123456789;
    f.InvokeMember(提交);
    }



    不要忘了添加使用的System.Threading .Tasks; 或者如果你使用.NET 4.0,你可以简单使用 System.Threading.Thread.Sleep(5000),然后取出异步/游览车。


    I am trying to create a program in C# (maybe using WinForms) which will enter a licence number into a form field of a specific website and validate whether or not the licence number is a currently valid licence.

    I am unsure as to where to start, as I can't even find the form field id in the source code of the website, and am unsure what technologies the website uses.

    Additionally, the purpose of this program will be to enter a list of license numbers and return the names and validation status of each license. Datasource being the website.

    Any information on how to go about this would be much appreciated, I am an intermediate C# developer - having mostly worked in ASP.Net, though feel Winforms may be better suited for this project.

    Kind Regards

    解决方案

    You can use a WebBrowser control:

    1. You can load the page using webBrowser1.Navigate("url of site")
    2. Find elements in page using webBrowser1.Document.GetElementById("buttonid") also you can iterate over HtmlElement of webBrowser1.Document.Body.All and check for example element.GetAttribute("value") == "some vaule" to find it.
    3. Set value for element using element.InnerText ="some value" or element.SetAttribute("value", "some value")
    4. Submit your form by invoking the submit of form or click of its submit button using element.InvokeMember("method")

    Example

    For example, if you browse google and look at page source, you will see name of search text box is "q" and name of the form that contains the search box is "f", so you can write this codes to automate search.

    1. Create a form with name BrowserSample.
    2. From toolbox, drag a WebBrowser and drop on form.
    3. Hanfdle Load event of form and navigate to google.
    4. Handle DocumentCompleted event of webBrowser1 and find f and find q and set InnerText of q and invoke submit of f. This event fires after the navigation and document load completed.
    5. In a real application add required null checking.

    Code:

    private void BrowserSample_Load(object sender, EventArgs e)
    {
        this.webBrowser1.Navigate("https://www.google.com/");
    }
    
    void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        //Because submitting f causes navigation
        //to pervent a loop, we check the url of navigation 
        //and if it's different from google url, return
        if (e.Url.AbsoluteUri != "https://www.google.com/")
            return;
        var f = this.webBrowser1.Document.Body.All.GetElementsByName("f")
                    .Cast<HtmlElement>()
                    .FirstOrDefault();
    
        var q = f.All.GetElementsByName("q")
                    .Cast<HtmlElement>()
                    .FirstOrDefault();
    
        q.InnerText = "C# Webbrowser Control";
        f.InvokeMember("submit");
    }
    

    If you execute the program, it first navigate to google and then shows search result:

    In your special case

    Since the site loads content using ajax, then you should make a delay in DocumentCompleted:

    async void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (e.Url.AbsoluteUri != "https://www.onegov.nsw.gov.au/PublicRegister/#/publicregister/search/Security")
            return;
    
        await Task.Delay(5000);
        var f = this.webBrowser1.Document.Body.All.GetElementsByName("searchForm")
                    .Cast<HtmlElement>()
                    .FirstOrDefault();
    
        var q = f.All.GetElementsByName("searchText")
                    .Cast<HtmlElement>()
                    .FirstOrDefault();
    
        q.InnerText = "123456789";
        f.InvokeMember("submit");
    }
    

    Don't forget to add using System.Threading.Tasks; or if you use .Net 4.0 you simple can use System.Threading.Thread.Sleep(5000) and remove async/await.

    这篇关于程序自动表单字段项,导致网页的检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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