Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Introduction Of Page Object Model In Selenium WebDriver: Advantages And disadvatages

Posted on 01/14/2025 By admin

Hello folks,

In this post, we will see below topics:

  1. Why we need Page Object Model?
  2. What is Page Object Model and its advantages?

Why we need Page Object Model?

When you write a code, it must have below properties:

  • Easy to understand.
  • Easy modifications.
  • Proper categorization.
  • Anybody should be able to locate desired lines of code easily in project folders.
  • Re-usability.
  • No duplicate codes.

Working with Selenium Webdriver has two major parts:

  • Locating web element/s.
  • Performing action on web element.

In previous posts, We have written locators and actions on web elements in same class. Consider below scenarios now:

  1. Can you reuse same code? Answer is No. If you need to use same web element in other place, you need to locate it again. It is developing same code again and again. Your code is not reusable. It produces duplicate code. Waste of effort and time.
  2. Can you modify locator easily? Answer is No. Suppose you have used same locator in multiple script files. After some deployment, there is change in locator. You will be fed up by updating locator in all script files. Imagine if there are multiple locators are changed. Using POM, you just need to modify in one class and it will be updated automatically in all scripts.
  3. Can you or anyone can navigate to your code easily? Answer is No. Suppose, you/any other colleague of yours need to use same code again in another class. You know that you have written it already and want to copy lines of code from that old class. But finding old class name will be difficult as there will be many classes. After finding class, you need to find out line number of code. It is very difficult. Traversing your code is not easy.
  4. Can you easily distinguish similar web elements on multiple web pages? Answer is No. 
  5. Can this code auto update reference of web elements when page is reloaded or refreshed. Answer is No. We can reuse already found locators until page is not refreshed./reloaded. For example:

Above code will give you staleElementException because reference of “ele” has changed. To know more about this refer this post.

All above scenarios can be handled easily by using Page object model in selenium webdriver.

What is Page Object Model and its advantages?

  • Page Object Model(POM) is an object design pattern in Selenium webdriver which says how to organize your object repository. Object means web elements here.
  • It is not a selenium framework.
  • POM is used to create object repository in such a way that web elements can be added, reused and modified easily. This object repository has web elements name and locators to find it at same place.
  • In POM, each web pages are represented as a separate class. For example, consider MakeSeleniumEasy website. It has many web pages like Home , Selenium Topics, Frequently Asked Java Programs etc. All these web pages contain different topics. I use different web pages to categories my topics. POM says same that we should have categorization of web elements according to web page in which they are present.
  • If any new web elements are added in to a web page, You can add that new web element in object repository by navigating to class which has same name as webpage. For example: I add a new web element in Home page. To add new web element in object repository, you will search from Home.java file and will add in to it.
  • If locator for any web element is changed, you just need to identify from which webpage it belongs. After that you need to navigate to particular webpage java file. find web element and modify its locators.
  • POM also says that create all user possible interaction on web elements in same page as a method. For example: You are on Home page and it has web elements like “User Name” and “Password” text boxes. You can create methods as below:

public void enterUserName(String userName)
public void enterPassword(String password)

It will be very useful as any developer just need to call methods directly in stead of  call locators first and then perform actions on it. It is called functional encapsulation.

  • Using POM, you can achieve coordination between pages. Suppose after login, you are moved to another web page named Dashboard. Using logic page method you can return an object of Dashboard page. We will see it later.
  • Object repository is made independent of any test scripts. Whatever objects you need to develop test scripts just call from that particular object repository.
  • POM helps in handling staleElementException without writing any extra code by developers. POM reinitialize objects whenever it is called. It page gets refreshed or reloaded, it takes care of new reference of web element. We will see this later in detail.
  • POM is lazy object locators. It will not find web elements until element is used. We will see it later.
  • POM enhances test maintenance, readability and reducing code duplication.
  • POM can be used with any type of selenium framework like Keyword, Data driver, hybrid etc.
  • Using POM, you can achieve abstraction and encapsulation.
  • Selenium webdriver provides a default implementation of Page Object Model using Page Factory which has more advantages than plain POM. We will see them later.

Disadvantages of Page Object Model:

  • You need to be little good in coding.
  • Developers may develop duplicate web elements if naming convention is not followed.
  • If a web page contains multiple div or iframe, POM class becomes bigger and difficult to manage. You can break pages in to multiple sub pages.
  • If webpage contains similar elements in different div/frame, you need to be careful for naming convention.
  • POM is specific to project. It is not generic.
  • You can get StaleElementException in case of findElements case. For example: Consider you are applying filter conditions and after checking each filter checkbox page gets refreshed. If you use findElements to store all checkbox and iterating it to select multiple filter conditions, it will give you StaleElementException for second iteration as page gets refreshed after first selection.

Is Page Object Model is a selenium framework?

No, its not a selenium framework. It is a design pattern. You can use POM in any type of framework.

We will see implementation of Page Object Model in upcoming posts. Stay tuned.

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: getOrDefault in java
Next Post: TestNG Tutorials 57: DataProvider in TestNG – Passing External Data File Name to DataProvider Method Using TestNG xml

Related Posts

REST Assured Tutorial 44 – Fetch Value From JSON Object Using JsonNode – Jackson – get() & path() Methods Uncategorized
Selenium Uncategorized
March 30, 2018 – Make Selenium Easy Uncategorized
Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
Advanced TestNG Tutorials 34: How To Pass a Group Name to be Run at Runtime in TestNG XML Using Beanshell Uncategorized
TestNG Tutorials 13: Can An Interface have TestNG Methods? If Yes, How To Execute Them? 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