如何在不刷新页面的情况下修复陈旧元素错误 [英] How to fix stale element error without refreshing the page

查看:45
本文介绍了如何在不刷新页面的情况下修复陈旧元素错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图在此页面上获取轮胎的详细信息.https://eurawheels.com/fr/catalogue/INFINY-INDIVIDUAL.每个轮胎都有不同的FINITIONS.每个 FINITIONS 的价格和其他细节都不同.我想点击每个FINITION类型.问题是,在单击 FINITION 类型时,链接变得陈旧,并且您无法刷新页面,如果您这样做,它将带您回到起始页面.那么,如何在不刷新页面的情况下避免陈旧的元素错误?

Trying to get details of Tyres on this page. https://eurawheels.com/fr/catalogue/INFINY-INDIVIDUAL . Each tyre has different FINITIONS. The price and other details are different for each FINITIONS. I would like to click on each FINITION type. The problem is that on clicking the FINITION type the links go stale, and You cannot refresh the page, if you do it will take you back to the starting page. So, How can I avoid stale element error without refreshing the page?

     count_added = False
     buttons_div = driver.find_elements_by_xpath('//div[@class="btn-group"]')
     fin_buttons = buttons_div[2].find_elements_by_xpath('.//button')
     fin_count = len(fin_buttons) 
     if fin_count > 2:
            for z in range(fin_count):
                if not count_added:
                    z = z + 2 #Avoid clicking the Title
                    count_added = True
                fin_buttons[z].click()
                finition = fin_buttons[z].text
                time.sleep(2)
                driver.refresh() #Cannot do this. Will take to a different page
                

推荐答案

澄清:陈旧的元素被抛出,因为该元素不再附加到 DOM.在你的情况下是这样的:buttons_div = driver.find_elements_by_xpath('//div[@class=btn-group"]') 它被用作 fin_buttons[z].click() 中的父级

To clarify: the stale element is thrown because the element is no longer attached to the DOM. In your case is this: buttons_div = driver.find_elements_by_xpath('//div[@class="btn-group"]') that its being used as parent in the fin_buttons[z].click()

要解决此问题,您必须刷新"DOM 更改后的元素.你可以这样做:

To solve this you'll have to "refresh" the element once the DOM changes. You can do that like this:

from selenium import webdriver
from time import sleep



driver = webdriver.Chrome(executable_path="D:/chromedriver.exe")

driver.get("https://eurawheels.com/fr/catalogue/INFINY-INDIVIDUAL")

driver.maximize_window()

driver.find_elements_by_xpath("//div[@class='card-body text-center']/a")[1].click()


def click_fin_buttons(index):
    driver.find_elements_by_xpath('//div[@class="btn-group"]')[2].find_elements_by_xpath('.//button')[index].click()

def text_fin_buttons(index):
    return driver.find_elements_by_xpath('//div[@class="btn-group"]')[2].find_elements_by_xpath('.//button')[index].text

sleep(2)
count_added = False
buttons_div = driver.find_elements_by_xpath('//div[@class="btn-group"]')
fin_buttons = buttons_div[2].find_elements_by_xpath('.//button')
fin_count = len(fin_buttons) 
if fin_count > 2:
    for z in range(fin_count):
        if not count_added:
            z = z + 2 #Avoid clicking the Title
            count_added = True
        click_fin_buttons(z)
        finition = text_fin_buttons(z)
        sleep(2)
        print(finition)
        #driver.refresh() #Cannot do this. Will take to a different page

这篇关于如何在不刷新页面的情况下修复陈旧元素错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆