Scenario: 1.Login to a website using script and navigate to the Home page. 2. On the home page, click a link and navigate to a new page. 3. Then navigate back to the home page using the top navigation bar Home link.

I am using the POM ( Page object model) where all my locators are defined in one python module.

The following is my directory structure 1. Properties.py ( This is where I specify all the login credentials, web driver and base url).

  1. Envsetup.py( This has setup and tear down methods)

  2. Locators.py ( This contains the locators for each page under a Locator class)

  3. Page objects( Under this directory I have HomePage.py for the home page that has methods under the Homepage class, each of these methods returns a specific web element locator using the Locator class)

  4. test scripts( test_homepage.py), this is where I am extending my Envsetup class to run the tests.

When I try to navigate back to my Home page, I get a stale elementrefernce exception error.


class Home(EnvSetup):
    def test_HomePage(self):
        browser = self.browser
        homepage = Home(browser)
        assert browser.title
        print('Running tests on ' + browser.title)
        if homepage.view_all_users().is_displayed(): # This method is defined in the Hompage module
            raise NoSuchElementException

        #browser.find_element_by_xpath('//*[@id="home"]').click() -----------> This works fine, takes me back to the Home page

    if homepage.nav_to_home().is_displayed(): ----------> I get a StaleElementException error

 In the following case, I get 

webDriverWait(browser, 20).until(EC.presence_of_element_located(By.XPATH, homepage.nav_to_home()))
TypeError: __init__() takes 2 positional arguments but 3 were given

            WebDriverWait(browser, 20).until(EC.presence_of_element_located(By.XPATH, homepage.nav_to_home()))
        except StaleElementReferenceException as e:
            raise e


if __name__ == '__main__':


from Tests.Pageobjects.Locators import Locator
from selenium.webdriver.common.by import By
class Home(object):

    def __init__(self, browser):
        self.browser = browser
        self.logo = browser.find_element(By.XPATH, Locator.logo)
        self.nav_Home = browser.find_element(By.XPATH, Locator.nav_Home)

    def get_logo(self):
        return self.logo

    def nav_to_home(self):
        return self.nav_Home
  • 1
    I need to create a try except block or a for loop to avoid this exception?
    – Dev Bin
    Oct 4, 2018 at 16:07

2 Answers 2


Fixed the Stale element exception with used WebDriverwait in my Page object. Added WebDriverwait(self.browser, 100).until(lambda browser: self.browser.find_element(By.XPATH, Locator.nav_Home) to my methods that return the locators in the page object.


from selenium.webdriver.common.by import By
from WFC_POM.locators.locators import locators

class signup_page():

    def __int__(self, driver):
        self.driver = driver
        self.signin_button = locators.signin_button
        self.click_signup_button_xpath = locators.click_signup_button_xpath
        self.input_fullname_id = locators.input_Fullname_id
        self.input_username_id = locators.input_username_id
        self.input_mobile_id = locators.input_mobile_id
        self.input_email_id = locators.input_email_id
        self.input_dob_xpath = locators.input_dob_xpath
        self.input_gender_id = locators.input_gender_id
        self.input_address_id = locators.input_address_id

    def enter_signin_button(self):
        self.driver.find_element(By.PARTIAL_LINK_TEXT, self.signin_button).click()

    def click_signup_button(self):
        self.driver.find_element(By.XPATH, self.click_signup_button_xpath).click()

    def input_fullname(self, fullname):
        self.driver.find_element(By.ID, self.input_fullname_id).send_keys(fullname)

    def input_username(self, username):
        self.driver.find_element(By.ID, self.input_username_id).send_keys(username)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.