1

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.

test_homepage.py

class Home(EnvSetup):
    def test_HomePage(self):
        browser = self.browser
        browser.implicitly_wait(10)
        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
                homepage.view_all_users().click()
        else:
            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
                homepage.nav_to_home().click()

 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


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

        homepage.nav_to_home().click()




if __name__ == '__main__':
    unittest.main()

Homepage.py

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

0

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.

Thanks!

0
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 that you have read and understand our privacy policy and code of conduct.

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