Friday, 24 January 2014

How to launch the Internet Explorer using Selenium in C#?

It is very simple to start the Internet Explorer browser and start performing the operations on it.
You can first read about how to set up the selenium web driver in visual studio.

You can download the IE driver at  https://code.google.com/p/selenium/downloads/list

Source code (Example)  to automate the Internet Explorer browser is given below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

        IWebDriver x = new InternetExplorerDriver(@"F:\selenium\Csharp");
        x.Url = "http://register.rediff.com/commonreg/index.php";
        x.Navigate();
        x.FindElement(By.Id("fullname")).SendKeys ("salunke");

        
        x.Quit();



        }
    }
}

You may run into problems while launching internet explorer like being unable to find elements or being unable to create the new driver instance due to security settings.

InvalidOperationException - due to different protected mode settings


You can follow below steps to fix this issue.

  1. Open Internet Explorer.
  2. Go to Security Tab.
  3. Check Enable Protected Mode for all zones like internet, local intranet, trusted sites, restricted sites.





This is how we can start the Internet Explorer Browser in C# using selenium API.

What do you think on above selenium topic. Please provide your inputs and comments. Thanks

How to launch the chrome browser using selenium in C#?

It is very simple to start the chrome browser and start performing the operations on it.
You can first read about how to set up the selenium web driver in visual studio.

Example -
Source code to automate the chrome browser is given below.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{


//Below line of code will create a new instance of the chrome browser
IWebDriver x = new ChromeDriver(@"C:\Users\sagar\Downloads");


//Below line sets the url property of the driver object.
x.Url = "http://register.rediff.com/commonreg/index.php";


//Here we are calling navigate method to open the website in browser
x.Navigate();


//Below line will find the element with id "fullname" and then enter the text "salunke saheb" into it.
x.FindElement(By.Id("fullname")).SendKeys ("Salunke Saheb");


x.Navigate();
x.FindElement(By.Id("fullname")).SendKeys ("salunke");



x.Quit();

}
}
}


This is how we can start the chrome browser in C# using selenium API.


What do you think on above selenium topic. Please provide your inputs and comments. Thanks

How to set up selenium web driver in C#?

Welcome Friends - In this article I am going to tell you how we can set up the selenium web driver in C sharp ( C#.Net)

What you need to set up Selenium Web driver in .Net?


  1. Microsoft Visual Studio.
  2. Selenium Web Driver API.
  3. Web-driver for Chrome 

You can download these things and then read below steps.
Please note that for testing applications on Internet Explorer, you will have to download the internet explorer driver separately.

Setting up Selenium Web driver in Visual Studio.Net 

We can set up selenium web driver in visual studio .Net easily with below steps.
  1. Create a new console based C# project.
  2. Go to the Project menu  and click on "add references".
  3. Choose all Web Driver API dll files.
  4. Finish.
Now you can access the selenium classes defined in the dll files to automate the web application.

Here is the sample code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//use of Selenium,Selenium.Chrome and Selenium.Support.UI namespaces is required
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

//Create a new Chrome webdriver instance 
//Please note that we have to give the path of the chromedriver.exe driver in below statement

        IWebDriver x = new ChromeDriver(@"C:\Users\sagar\Downloads");
//Set the URL to navigate to
        x.Url = "http://register.rediff.com/commonreg/index.php";
        x.Navigate();
// Find the element having id "fullname"
        x.FindElement(By.Id("fullname")).SendKeys ("sagar");    
//Quit the driver.
        x.Quit();
        }
    }
}

After you run above code chrome browser will be launched and it will navigate to the rediff registration page and enter the sagar in first name edit box.



What do you think on above selenium topic. Please provide your inputs and comments. Thanks

Buy Best Selenium Books

Contributors