Saturday 15 March 2014

How to navigate to particular URL using Selenium Webdriver in C#.Net?

There are 2 ways we can navigate to particular URL with selenium webdriver in C#.Net.

  1. Navigate Method
  2. GoToUrl Method
using Navigate method

  driver.Url = "http://www.google.com";
  driver.Navigate();

using GoToUrl method
  driver.Navigate().GoToUrl("http://www.google.com")

Below example shows how we can navigate to the particular URL in Selenium Webdriver in C#.Net


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 x = new InternetExplorerDriver(@"F:\selenium\csharp");
//IWebDriver x = new FirefoxDriver();
 IWebDriver driver=null;
            try
            {
                //here you have to give the path of the chrome driver exe file
                driver = new ChromeDriver(@"F:\selenium\csharp");
               
                //set the url of the website you want to navigate to.
                driver.Url = "http://www.google.co.in";

                driver.Manage().Window.Maximize();
                 
                //use navigate method to go to particular URL.
                driver.Navigate();
              }
            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

No comments:

Post a Comment

Buy Best Selenium Books

Contributors