'Python - Logical error in Selenium library

I'm trying to develop a bot that automate some boring staff in a Web Application using Python/Selenium lib. The bot sometime comes to a point where it raise an "Stall Element" exception error. This happens when it tries to select from a drop down menus that are disabled and are getting enabled ONLY once the drop menu before it is selected. When I try to re-run the bot again it can select from the drop down menu without any issue.

The bot is working just fine but I don't know what's the reason of this error and why it's happening.

Here's my code as advised:

select_location = Select(
        self.driver.find_element(
            By.ID,
            'CPB_txtIntLocIDN'
        )
    )
    select_location.select_by_visible_text(location)
    self.wait_until('CPB_txtIntFloorIDN')

    select_floor = Select(
            self.driver.find_element(
                By.ID,
                'CPB_txtIntFloorIDN'
            )
        )

    select_floor.select_by_visible_text(floor)
    self.wait_until('CPB_txtIntSecIDN')

    select_section = Select(
        self.driver.find_element(
            By.ID,
            'CPB_txtIntSecIDN'
        )
    )
    select_section.select_by_visible_text(section)
    self.wait_until('CPB_txtIntSubIDN')

    select_subsection = Select(
        self.driver.find_element(
            By.ID,
            'CPB_txtIntSubIDN'
        )
    )
    select_subsection.select_by_visible_text(subsection)
    self.wait_until('CPB_txtIntStatusIDN')

    select_status = Select(
        self.driver.find_element(
            By.ID,
            'CPB_txtIntStatusIDN'
        )
    )
    select_status.select_by_visible_text(status)

and here's the wait until function code:

def wait_until(self, ID):
    WebDriverWait(self.driver, TIMEOUT).until(
        EC.element_to_be_clickable(
            (By.ID, ID)
        )
    )

ExceptionError:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

Thanks in advance and appreciate your help.

I tried to change the connection to the Web App to decrease the time taken to load the web objects. I tried to increase the timeout parameter of Selenium to give the bot more time to wait until the objects get settled.

I expected that the issue should be resolved after doing the above workarounds but it still exist.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source