Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

c# - Program to automate form field entry and result checking of webpage

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...