StaleElementReferenceException on Python Selenium

后端 未结 9 2120
孤独总比滥情好
孤独总比滥情好 2020-11-27 13:28

I am getting the following error while using Selenium in python:

selenium.common.exceptions.StaleElementReferenceException: Message: u\'stale element referen         


        
9条回答
  •  Happy的楠姐
    2020-11-27 13:48

    This is the python solution for this problem:

    def clickAndCatchStaleRefException(locator):
    
    
    
        driver = sel2._current_browser()
        result = False
        attempts = 0
    
        locator = locator[6:]
        # This line is optional because sometimes you pass a xpath from a varibles file
        # that starts with 'xpath='. This should be omitted otherwise the find_element_by_xpath 
        # function will throw an error.
        # But if you pass an xpath directly you don't need this
    
    
        while attempts < 2:
            try:
                driver.find_element_by_xpath(locator).click()
                result = True
                break
            except EC as e:
                raise e
            finally:
                attempts += 1
        return result
    

提交回复
热议问题