Sunday, 16 March 2014

How to check the checkbox in selenium webdriver in C#.Net?

We can first see if the checkbox is selected using Selected property. 
Then using click method we can perform the operations such as selecting or deselecting the checkboxes as illustrated in the below example.

Complete example in C#.Net to select the checkbox using selenium Webdriver

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using OpenQA.Selenium;

using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
using System.Collections.ObjectModel;
namespace Abc
{
    class Program
    {
        static void Main(string[] args)
        {

           
IWebDriver driver=null;
try
{

driver = new ChromeDriver(@"F:\selenium\csharp");

driver.Url = "https://www.gmail.com";
              driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20));

driver.Navigate();

//find the checkbox element using xpath

IwebElement e1 = driver.FindElement(By.XPath ("//*[@id='PersistentCookie']"));

//click on the link sell


if (e1.Selected)
{

//deselect checkbox
e1.click();

}
else
{

//select checkbox
e1.click();

}

}

 catch(Exception e){
 Console.WriteLine("Exception ******"+e.ToString());
               
 }
           
            finally{
            Thread.Sleep(2000);
            driver.Quit();
            Console.ReadLine();
            }

           
        }
    }

}

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

1 comment:

Buy Best Selenium Books