Problem with StaleElementReferenceException error pops up occasionally

后端 未结 3 1097
一整个雨季
一整个雨季 2021-01-29 03:12

Hi I have this code which is generating me:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not

3条回答
  •  北海茫月
    2021-01-29 03:47

    It's easily cause this problem if you use find_elements_by_xpath to get a set of elements first.

    I suggest you find element again in for loop like this:

    offer_type_list = context.driver.find_elements_by_xpath(Locators.all_elements_buttons_offer_type) 
    offer_type_text = []
    for i in range(1, len(offer_type_list)+1):
        # xpath example: (//div[@class='abc'])[1]
        element_offer_type = context.driver.find_element_by_xpath("("+Locators.all_elements_buttons_offer_type+")["+str(i)+"]")
        compare_announcement_text(context, option_name, element_offer_type)
        offer_type_text.append(element_offer_type.text)
    

提交回复
热议问题