Try   HackMD

Selenium - Notes - 3

Issues

Element is not attached to the page document

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

Solution 1

https://www.crifan.com/selenium_by_id_got_element_click_error_element_is_not_attached_to_the_page_document/
Refresh the current page before locating and interacting with the element.

self.driver.refresh()

target = (By.XPATH, "//a[text()='"+targetStr+"']")
targetElement = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(targetUnit))
targetElement.click()

Solution 2

Add wait to wait until the element is displayed or exists.

Element cannot be located after switching to another page

Solution

When switching from a page to another page, can assign the webdriver of the current page to the next page object. (Refresh the page to ensure element loading)

previousPage = PreviousPage()
previousPage.driver = currentPage.driver
currentPage.driver.refresh()
tags: selenium test automation software test