Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
                
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
178 views
Welcome To Ask or Share your Answers For Others

1 Answer

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()

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...