Site Loader
Auckland, New Zealand
In last post we discussed about Page Object Model in Selenium with C# and we also created a simple page object for the EA Page. In this post we will start working with Page Navigational concept of Page Object Model and try to work with two different pages like Login page and EA page and try to demonstrate the concept. We have already discussed about page navigation concept in Selenium with Java, and in this post we will talk the same concept using Selenium with C#. Here is the complete video of above discussion Here is the complete code from the above video Code for Login and EA Page
class EAPageObject

        public EAPageObject()
            PageFactory.InitElements(PropertiesCollection.driver, this);

        [FindsBy(How = How.Id, Using = "TitleId")]
        public IWebElement ddlTitleID { get; set; }

        [FindsBy(How = How.Name, Using = "Initial")]
        public IWebElement txtInitial { get; set; }

        [FindsBy(How = How.Name, Using = "FirstName")]
        public IWebElement txtFirstName { get; set; }

        [FindsBy(How = How.Name, Using = "MiddleName")]
        public IWebElement txtMiddleName { get; set; }

        [FindsBy(How = How.Name, Using = "Save")]
        public IWebElement btnSave { get; set; }

        public void FillUserForm(string intial, string middleName, string firstName)


    //Login Page Object Class
    class LoginPageObject

        public LoginPageObject()
            PageFactory.InitElements(PropertiesCollection.driver, this);

        [FindsBy(How = How.Name, Using = "UserName")]
        public IWebElement txtUserName { get; set; }

        [FindsBy(How = How.Name, Using = "Password")]
        public IWebElement txtPassword { get; set; }

        [FindsBy(How = How.Name, Using = "Login")]
        public IWebElement btnLogin { get; set; }

        public EAPageObject Login(string userName, string password)
            //Click button

            //Return the page object
            return new EAPageObject();


Code for Test Script
            //Login to Application
            LoginPageObject pageLogin = new LoginPageObject();
            EAPageObject pageEA =  pageLogin.Login("execute", "automation");
            //Fill User Details
            pageEA.FillUserForm("KK", "Karthik", "Automation");
Thanks for watching the video and reading the post!! Please leave your comments and let me know if there is anything need to be improved in the post!!! Thanks, Karthik KK

Post Author: Karthik kk

9 Replies to “Page Navigation in Selenium C#”

  1. How to handle controls in modal popup, through page object model?
    By following your instructions, when i click on a link which opens up in pop, i get an error “Null refernce exception was handled”

  2. Hi Karthik,
    Why can we create just one class that has objects and methods for all the pages in the website. Advantages:
    1) We will have less number of .cs files
    2)we would have some less lines of code
    3) As we will have all the page objects in one file, maintenance will be easier.
    Any thoughts?

    1. Hi Vivek,

      What you have said is opposite to what I have demonstrated, but thats not going to help anyways for bigger projects.


  3. Hello Kartik !
    I really liked your lessons. POM – Great. But you nothing tell about hidden elements and hover actions. Could you give me a simple way to use hover actions (hidden elements) in selenium C#, using custom methods and POM. Thanks a lot !

    1. You cannot hover on an hidden element, since it wont get rendered in DOM though, may I know whats your usecase to hover in hidden elements ?

      1. Here is my code: (I copied from your lesson in Java)
        public void Property()
        IWebElement menu = driver.FindElement(By.Id("Automation Tools"));
        IWebElement selenium = driver.FindElement(By.Id("Selenium"));
        IWebElement seleniumRC = driver.FindElement(By.Id("Selenium RC"));
        hover(driver, menu);
        hoverAndClick(driver, selenium, seleniumRC);
        public static void hover(IWebDriver driver, IWebElement element)
        Actions action = new Actions(driver);
        public static void hoverAndClick(IWebDriver driver, IWebElement elementToHover, IWebElement elementToClick)
        Actions action = new Actions(driver);

        As you can see, this code found a Selenium RC element and click on it.
        I would like to know, can I write this code a shortly and simply.

  4. Hi Karthik,

    I am building selenium framework (C#) for my project, I am in the initial phase. So I am exploring to keep most of the best practices across. While using PageFactory which you have used in the above code FillUserForm function , i have a scenario where if save button does not exist in the page I need to do some operation for ex: click on new button or some thing. With the code btnSave IWebElment object does not exist it will throw exception. How do you want me to handle this type of scenario.

    Please let me know may be am I thinking in wrong way or not, instead of using properties if I use something like By object (public static By saveBtn = By.Id(“save”) so that I can check if Save By objects exists or not before clicking. I am confused whether do I need to use these
    [FindsBy(How = How.Id, Using = “”)] properties or static By Objects in my page class file. Please help me on this.

Leave a Reply

Your email address will not be published. Required fields are marked *