使用 selenium (python) 从 html 表中获取数据:提交更改中断循环

Jan*_*ler 3 python selenium web-scraping selenium-webdriver

我想通过循环遍历这些组合,从 HTML 表中抓取不同下拉值组合的数据。选择组合后,需要提交更改。但是,这会导致错误,因为它会刷新页面。

这是我到目前为止所做的:

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time

browser.get('https://daten.ktbl.de/feldarbeit/entry.html')

# Selecting the constant values of some of the drop downs:
fertilizer = Select(browser.find_element_by_name("hgId"))
fertilizer.select_by_value("2") 
fertilizer = Select(browser.find_element_by_name("gId"))
fertilizer.select_by_value("193") 
fertilizer = Select(browser.find_element_by_name("avId"))
fertilizer.select_by_value("383")  
fertilizer = Select(browser.find_element_by_name("hofID"))
fertilizer.select_by_value("2") 

# Looping over different combinations of plot size and amount of fertilizer:
size = Select(browser.find_element_by_name("flaecheID"))
for size_values in size.options:
    size.select_by_value(size_values.get_attribute("value"))
    time.sleep(1)

    amount= Select(browser.find_element_by_name("mengeID"))
    for amount_values in amount.options:
        amount.select_by_value(amount_values.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)

Run Code Online (Sandbox Code Playgroud)

这导致错误:selenium.common.exceptions.StaleElementReferenceException: Message: The element reference of <option> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed。显然问题是我确实刷新了文档。

提交更改并且页面加载了结果后,我想用以下方法检索它们:

html_source = browser.page_source
df_list = pd.read_html(html_source, match = "Dieselbedarf")

Run Code Online (Sandbox Code Playgroud)

(向@bink1time 大喊,他在这里回答了我的这一部分问题)。

如何在不中断循环的情况下更新页面?

我非常感谢这里的一些帮助!

Sve*_*ohn 5

由于 DOM 中的元素 UUID 更改,经常在页面刷新时发生陈旧的元素引用异常

为了避免它,请始终尝试在交互之前搜索元素。在您的特定情况下,您搜索sizeamount,找到它们并将它们存储在变量中。但是,在刷新时,它们的 UUID 发生了变化,因此您存储的旧 UUID 不再附加到 DOM。在尝试与它们交互时,Selenium 在 DOM 中找不到它们并抛出此异常。

我修改了您的代码以在交互之前始终重新搜索大小和数量元素:

# 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)
Run Code Online (Sandbox Code Playgroud)

尝试这个?它对我有用。我希望它有帮助。