返回页面时,在 selenium python 中出现 Stale Element 错误

问题描述 投票:0回答:1

我正在尝试用 python selenium 为这个网站构建我自己的网络抓取器:https://wyszukiwarka-krs.ms.gov.pl/,

但是当我的代码从单击一个元素返回时,它给出了此错误:消息:过时的元素引用:找不到过时的元素 这里这个“for a_tag in a_tags:”循环只执行了1次,然后就中断了。 PS:如果我不单击任何元素而只执行“print(a_tag.text)”,那么循环将运行到最后并打印所有内容

请帮我解决这个问题,如何在返回后运行它

    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)
python selenium-webdriver web-scraping selenium-chromedriver staleelementreferenceexception
1个回答
0
投票

我解决了这样做

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(....)
© www.soinside.com 2019 - 2024. All rights reserved.