Getting Stale Element error in selenium python while navigating back on the page

41 views Asked by At

I'm trying to build my own web scrapper in python selenium for this website : https://wyszukiwarka-krs.ms.gov.pl/,

But when my code comes back from clicking an element,it gives this error : Message: stale element reference: stale element not found Here this "for a_tag in a_tags:" loop is only executed 1 time and then it breaks. PS: If I don't click any element and just do "print(a_tag.text)", then the loop runs till the end and prints all the content

Please help me with this, how to run it after navigating back

    for tr_element in tr_elements:
            td_element = tr_element.find_elements_by_tag_name('td')
           
            if len(td_element):
                
                for td_content in td_element:
                    a_tags = td_content.find_elements_by_tag_name('a')
                    
                    if len(a_tags):
                        for a_tag in a_tags:
                           
                            try:
                              
                                #driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
                                # Store the current URL to navigate back to it after going to the next page
                                current_url = driver.current_url
                                location = a_tag.location
                               
                                
                                driver.execute_script("arguments[0].click();", a_tag)
                                

                                time.sleep(5)

                                # Find the elements on the new page
                                div_elements = driver.find_elements_by_tag_name('div')
                                div_element = driver.find_element_by_xpath("//div[contains(text(), 'Nazwa')]")
                                name_div_element = div_element.find_element_by_xpath("following-sibling::div")

                                register_div_element = driver.find_element_by_xpath("//div[contains(text(), 'Rejestr')]")
                                name_register_div_element = register_div_element.find_element_by_xpath("following-sibling::div")

                                number_krs_div_element = driver.find_element_by_xpath("//div[contains(text(), 'Numer KRS')]")
                                name_krsr_div_element = number_krs_div_element.find_element_by_xpath("following-sibling::div")

                                date_of_entry_div_element = driver.find_element_by_xpath("//div[contains(text(), 'Data wpisu do Rejestru Przedsiębiorców')]")
                                name_date_of_entry_element = date_of_entry_div_element.find_element_by_xpath("following-sibling::div")
                                translated_text = translator.translate(name_register_div_element.text, src='pl', dest='en')
                                # Navigate back to the previous page
                                driver.back()
                              
                                time.sleep(5)
                                td_content = driver.find_element_by_tag_name('td')
                               
                                time.sleep(3)
                                a_tags = td_content.find_elements_by_tag_name('a')
                               
                                
                                

                               
                                time.sleep(5)
                                
                            except ElementClickInterceptedException:
                                print("Element is not clickable")
                            except StaleElementReferenceException:
                                print("Stale element reference: retrying...")
                                a_tag = td_content.find_element_by_tag_name('a')
                                driver.execute_script("arguments[0].click();", a_tag)
                                time.sleep(5)
                               
                                driver.get(current_url)
                        time.sleep(2)
1

There are 1 answers

1
Wonka On

I solve doing this

a_tags = get_elements(....)
for i_a in range(len(a_tags)):

    a_tags[i_a].click() #or whatever you want to do

    #Do what you need to do
    ...

    #Go back
    a_tags = get_elements(....)