Skip to content Skip to sidebar Skip to footer

Getting Data From Html Table With Selenium (python): Submitting Changes Breaks Loop

I want to scrape data from an HTML table for different combinations of drop down values via looping over those combinations. After a combination is chosen, the changes need to be s

Solution 1:

Stale Element Reference Exception often occurs upon page refresh because of an element UUID change in the DOM.

In order to avoid it, always try to search for an element before an interaction. In your particular case, you searched for size and amount, found them and stored them in variables. But then, upon refresh, their UUID changed, so old ones that you have stored are no longer attached to the DOM. When trying to interact with them, Selenium cannot find them in the DOM and throws this exception.

I modified your code to always re-search size and amount elements before the interaction:

# Looping over different combinations of plot size and amount of fertilizer:
size = Select(browser.find_element_by_name("flaecheID"))
for i in range(len(size.options)):
    # Search and save new select element
    size = Select(browser.find_element_by_name("flaecheID"))
    size.select_by_value(size.options[i].get_attribute("value"))
    time.sleep(1)

    amount = Select(browser.find_element_by_name("mengeID"))
    for j in range(len(amount.options)):
        # Search and save new select element
        amount = Select(browser.find_element_by_name("mengeID"))
        amount.select_by_value(amount.options[j].get_attribute("value"))
        time.sleep(1)

        #Refreshing the page after the two variable values are chosen:
        button = browser.find_element_by_xpath("//*[@type='submit']")
        button.click()
        time.sleep(5)

Try this? It worked for me. I hope it helps.

Post a Comment for "Getting Data From Html Table With Selenium (python): Submitting Changes Breaks Loop"