[Django]-Django: Selenium- stale element reference on browse

2👍

Does wait_for_page_load have to be a generator? I think it would return without waiting! That’s why your tests are flaky. Sometimes the element would have loaded by the time you call find_element_by_link_text sometimes not.

I have used this approach with success:

from selenium.webdriver.support import expected_conditions as EC

def wait_for_element(self, elm, by = 'id', timeout=10) :
    wait = WebDriverWait(self.driver, timeout)
    if by == 'id' :
        element = wait.until(EC.element_to_be_clickable((By.ID,elm)))
        return self.driver.find_element_by_id(elm)
    elif by == 'link':
        wait.until(EC.element_to_be_clickable( (By.LINK_TEXT,elm)))
        return self.driver.find_element_by_link_text(elm)
    # by tag, by css etc etc goes here.

I call this method with a prominent id of a dom element that should show up before the page can be interacted with. The returned element can be interacted with directly.

👤e4c5

Leave a comment