DEN
DEN

Reputation: 1893

Web automation using .NET

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. Automatically Input some value and Click on "Run" button.
  2. Check In the ComboBox and Click on "Download" button.

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?

Upvotes: 17

Views: 49257

Answers (7)

Mladen B.
Mladen B.

Reputation: 2996

You could use Selenium WebDriver.

A quick code sample below:

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

// Requires reference to WebDriver.Support.dll
using OpenQA.Selenium.Support.UI;

class GoogleSuggest
{
    static void Main(string[] args)
    {
        // Create a new instance of the Firefox driver.
        // Note that it is wrapped in a using clause so that the browser is closed 
        // and the webdriver is disposed (even in the face of exceptions).

        // Also note that the remainder of the code relies on the interface, 
        // not the implementation.

        // Further note that other drivers (InternetExplorerDriver,
        // ChromeDriver, etc.) will require further configuration 
        // before this example will work. See the wiki pages for the
        // individual drivers at http://code.google.com/p/selenium/wiki
        // for further information.
        using (IWebDriver driver = new FirefoxDriver())
        {
            //Notice navigation is slightly different than the Java version
            //This is because 'get' is a keyword in C#
            driver.Navigate().GoToUrl("http://www.google.com/");

            // Find the text input element by its name
            IWebElement query = driver.FindElement(By.Name("q"));

            // Enter something to search for
            query.SendKeys("Cheese");

            // Now submit the form. WebDriver will find the form for us from the element
            query.Submit();

            // Google's search is rendered dynamically with JavaScript.
            // Wait for the page to load, timeout after 10 seconds
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
            wait.Until(d => d.Title.StartsWith("cheese", StringComparison.OrdinalIgnoreCase));

            // Should see: "Cheese - Google Search" (for an English locale)
            Console.WriteLine("Page title is: " + driver.Title);
        }
    }
}

The great thing (among others) about this approach is that you can easily switch the underlying browser implementations, just by specifying a different IWebDriver, like FirefoxDriver, InternetExplorerDriver, ChromeDriver, etc. This also means you can write 1 test and run it on multiple IWebDriver implementations, thus testing how the page works when viewed in Firefox, Chrome, IE, etc. People working in QA sector often use Selenium to write automated web page tests.

Upvotes: 2

Fun Mun Pieng
Fun Mun Pieng

Reputation: 6891

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.

example:

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

for the HTML:

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

the code:

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

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, 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.

Upvotes: 20

Garu Jwon
Garu Jwon

Reputation: 65

I'm using ObjectForScripting to automate WebBrowser, A Javascript callback to C# function and then function in c# extract data or automate many-thing.

I have clearly explained in the following link

Web Automation using Web Browser and C#

Upvotes: 1

Nathan Ridley
Nathan Ridley

Reputation: 34396

Check out SimpleBrowser, which is a fairly mature, lightweight browser automation library.

https://github.com/axefrog/SimpleBrowser

From the page:

SimpleBrowser is a lightweight, yet highly capable browser automation engine designed for automation and testing scenarios. It provides an intuitive API that makes it simple to quickly extract specific elements of a page using a variety of matching techniques, and then interact with those elements with methods such as Click(), SubmitForm() and many more. SimpleBrowser does not support JavaScript, but allows for manual manipulation of the user agent, referrer, request headers, form values and other values before submission or navigation.

Upvotes: 5

Dmytrii Nagirniak
Dmytrii Nagirniak

Reputation: 24088

If you want to simulate a real browser then WatiN will be a good fit for you. (Selenium is another alternative, but I do not recommend it for you).

If you want to work on the HTTP level, then use WebRequest and related classes.

Upvotes: 4

mellamokb
mellamokb

Reputation: 56769

You cannot easily automate client-side activity, like filling out forms or clicking on buttons from C#. However, if you look into JavaScript, you may be able to better automate some of those things. To really automate, you would need to reverse engineer the call made by clicking the button, and connect to the url directly, using the classes @John mentions.

Upvotes: 0

John Saunders
John Saunders

Reputation: 161773

.NET does not have any built-in functionality for this. It does have the WebClient and HttpRequest/HttpResponse classes, but they are only building blocks.

Upvotes: 0

Related Questions