Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Object Repository in Selenium Using Page Factory With Inner Class – Handling a Page Which Has Many Webelements

Posted on 02/19/2025 By admin

Hello Folks,

You must have seen a web page which contains many divisions or multiple web elements and similar web elements in multiple divisions. Creating a web element repository for such pages is difficult in some cases which are given below:

  • If you have many web elements in a page, Selecting desired web elements while using is difficult. As soon as you type connector (dot (.) ) operator , it will list all the web elements present in that page and you need to search from them. However this can be made simpler using Naming Convention approach.
  • If you have common web elements in multiple division of same web page, you need to provide proper differentiation in names so that anyone can easily identify desired one.

We can resolve above issues using inner class concept of Java. We can divide each section as a static inner class. It will also initialise only particular section web elements instead of all web elements of page.

Sample code:

package PageObjectModelWithInnerClass;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;



public class Page_Login {
        

    // Create an account section        
        public static class createAnAccount
        {
                // Initializing only createAnAccount members
                public  static createAnAccount getcreateAnAccount(WebDriver driver) {
                        return PageFactory.initElements(driver, createAnAccount.class);
                }
                
                @FindBy(id="email_create")
                public  WebElement txt_emailAddress;
                
                
                @FindBy(id="SubmitCreate")
                public  WebElement btn_createAccount;
                
                @FindBy(xpath="//h3[text()='Create an account']")
                public  WebElement lbl_createAnAccount;
                
                @FindBy(xpath="//p[text()='Please enter your email address to create an account.']")
                public  WebElement lbl_createAnAccountDesc;
                
                
        }
        
        // Already registered section
        public static class alreadyRegistered
        {
                
                // Initializing only createAnAccount members
                public  static alreadyRegistered getalreadyRegistered(WebDriver driver) {
                        return PageFactory.initElements(driver, alreadyRegistered.class);
                }
                
                
                @FindBy(id="email")
                public static WebElement txt_emailAddress;
                
                
                @FindBy(id="passwd")
                public static WebElement txt_password;
                
                
                @FindBy(id="SubmitLogin")
                public static WebElement btn_login;
                
                @FindBy(xpath="//h3[text()='Create an account']//a[text()='Forgot your password?']")
                public static WebElement lnk_forgotPassword;
                
                                
                
        }
}

In Test script class, you just need to access required inner class as below:

package PageObjectModelWithInnerClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class CreateAnAccount {
        
        public static void main(String[] args) {
                
                System.setProperty("webdriver.chrome.driver","./exefiles/chromedriver.exe");
                WebDriver driver= new ChromeDriver();
                driver.manage().window().maximize();
                driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                driver.get("http://automationpractice.com/index.php?controller=authentication&back=my-account");
                
                // Access inner class directly by its name as I have declared it as static 
                Page_Login.createAnAccount.getcreateAnAccount(driver).txt_emailAddress.sendKeys("[email protected]");
                
                
                
                
        }

}

Hope this will be helpful for you to create an object repository for a page which has multiple divisions and web elements.

If you have any doubt, feel free to comment below. If you like my posts, please like, comment, share and subscribe. #ThanksForReading

#HappySelenium

Uncategorized

Post navigation

Previous Post: XPath Method – string() – Usage in Locating Element- How it is Different From text()
Next Post: Learn REST Assured End to End

Related Posts

image – Make Selenium Easy Uncategorized
AfterBefore – Make Selenium Easy Uncategorized
Selenium Topics – Page 5 – Make Selenium Easy Uncategorized
Git Tutorial 24 – Ignore Files Using .gitignore Uncategorized
TestNG Tutorials 68 : Sharing Data Between Tests in a Suite Using ISuite & ITestContext Uncategorized
September 11, 2019 – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark