Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20



Python Selenium Page Object Model Tutorial

In Selenium Test Automation, Page Object Model is a framework in which all page elements are defined in a SiteModel class. In other modules of the test framework, the SiteModel class will be called to interact with elements. This makes the test maintenance easy because all the UI change will only affect the SiteModel class.

With Python language, we can define all site elements in an external xml file and read element information from this file and use in the main test code. When UI changes frequently, we just need to update the xml file with the updated UI elements and some find by logic.

Test Case: authorized users are able to login Mahara Demo site. Here are test steps.

1. Navigate to and verify that web page title "Home-Mahara" displays.

2. Enter "student1" in the Username filed. 

3. Enter "Testing1" in the Password field.

4. Click on the Login button

5. Assert that Logout link is present.

Note: if the above username and password do not work, try registering one and use your own user account for testing because the demo user password may have been changed by others. 

Test Implementation

Step 1: create a xml fie and save it as "SiteElement.xml" and add the following element information.

Step 2: launch Python IDE, create a new file , and save it as "" with the following code. 

import unittest
import xml.etree.ElementTree as ET
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

def getSearchValue(pagenameattribute,elementkeystring):
    for element in root.findall(xpathstring):
        return value
def getSearchBy(pagenameattribute,elementkeystring):
    for element in root.findall(xpathstring):
        return searchby
class DemoMaharaOrgLogin(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_login_in_demo_mahara_org(self):
        driver = self.driver
        self.assertIn("Home - Mahara", driver.title)
        username = driver.find_element_by_id(getSearchValue("LoginPage","LoginUserName"))
        self.assertTrue(driver.find_element_by_link_text(getSearchValue("DashboardPage","LogoutLink")),"Logout link")

    def tearDown(self):

if __name__ == "__main__":

Step 3: run the above code and see the result. Here is the code result.  Test passed. 

Ran 1 test in 20.094s