Saturday, 14 February 2015

Selenium C# : Click on button control in website with selenium C# webdirver

Click Here to Download ClickOnButtonUsingSeleniumWebDriverTool.zip

Note : If any runtime error occurred by running our downloaded project meaning of that your browser version and Selenium API DLL version is not matching. To resolve this issue you need to download latest Selenium C# API DLLs from this link and paste downloaded DLLs into 'Bin' folder of downloaded project. Or you can remove references of existing DLLs references and add new DLLs references. You can learn this thing from this link.


Using selenium C# webdriver API we are able to do click on button automatically in web browser. To click on button we need button locater string. To get locater string you need to install FireFox Element Locater Tool in Firefox. This article is very useful for automate website testing.

Here is example for this:

In this example first we open website 'www.google.com'. After that, we locate search textbox and search button ID locater string using FireFox Element Locater Add-on.

Now in C# code we create object of FireFoxDriver Class. You can also create object of other browsers. Now using 'FindElement' method of  'FireFoxDriver' Class object, we are finding search textbox and search button.

After finding search textbox we enter some text using 'SendKeys' method. After that, programmatically we are doing click on search button using 'Click' method.

Selenium C# Code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OpenQA;
using Selenium;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnOpenBrowser_Click(object sender, EventArgs e)
    {
        string STR_IE_DRIVER = Server.MapPath("EXEFiles");

        OpenQA.Selenium.IWebDriver objFF = new OpenQA.Selenium.Firefox.FirefoxDriver();
        objFF.Navigate();
        objFF.Url = "http://www.google.com";

        //Insert Text in search text box
        objFF.FindElement(OpenQA.Selenium.By.XPath("//input[contains(@id,'gbqfq')]")).SendKeys("ASP.NET");

        //Click on Search Button
        objFF.FindElement(OpenQA.Selenium.By.XPath("//button[@id='gbqfb']")).Click();

    }
}
Output :

(To view original size image , click on image)
Prerequisite to learn this article :

You need to learn these previous articles before learning this article.

Selenium Tool : Configure selenium tool for asp.net C# website or project in Visual Studio.
Selenium Tool : Download and Install WebDriver Element Locator Add-on.
Selenium C# : Insert text in website textbox with selenium C# webdirver

Full Video : 



17 comments:

  1. Very good write-up. I definitely appreciate this website. Continue the good work!
    Devops Online Training

    ReplyDelete
  2. Nice list, though Maarten Balliauw ‘s blog just serves ASP.NET error pages to me :-(

    I’m in the process of writing my blog in ASP.NET MVC (currently Blogger, but soon to by MVC!) so it’s nice to have some more great sources of info!

    dotnet training in chennai

    ReplyDelete
  3. Hello!
    Tutorial is amaizing! Thank you!
    What is this line for:
    string STR_IE_DRIVER = Server.MapPath("EXEFiles");

    ReplyDelete
  4. this is really too useful and have more ideas from yours. keep sharing many techniques. eagerly waiting for your new blog and useful information. keep doing more.
    GMAT Coaching Chennai
    best GMAT coaching classes in chennai

    ReplyDelete
  5. Hello author,

    I found your blog while searching for the updates in Selenium Click,I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about selenium tutorials for beginners with reference of your blog.

    ReplyDelete
  6. you are posting a good information for people and keep maintain and give more update too.
    seo company india

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. SQream Technologies provides you with a state of the art software which combines modern GPU technology (Graphic Processing Units) with the best practices in today’s Big Data platforms, providing up to 100x faster insights from data.
    Bigdata Training in Chennai OMR

    ReplyDelete
  9. Nice blog..! I really loved reading through this article. Thanks for sharing such a amazing post with us and keep blogging... best angularjs training institute in chennai | angularjs training in omr | angularjs training in chennai | angularjs training in velachery |

    ReplyDelete
  10. Hello! Someone in my Facebook group shared this site with us so I came to take a look. I'm definitely loving the information. I'm bookmarking and will be tweeting this to my followers! Superb blog and terrific design.

    ReplyDelete
  11. Come up with a great learning experience of Data Science training in Chennai, from Infycle Technologies, the best software training institute in Chennai. Get up with other technical courses like Data Science, Selenium Automation Testing, Mobile App Development, Cyber Security, Big Data, Full Stack Development with a great learning experience and outstanding placements in top IT firms. For best offers with learning, reach us on +91-7504633633, +91-7502633633.

    ReplyDelete
  12. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites
    ibm full form in india |
    ssb ka full form |
    what is the full form of dp |
    full form of brics |
    gnm nursing full form |
    full form of bce |
    full form of php |
    bhim full form |
    nota full form in india |
    apec full form

    ReplyDelete