【发布时间】:2021-09-03 09:15:46
【问题描述】:
我正在尝试为this 网站制作网络爬虫。这个想法是代码通过选择机构的名称(首先是 3B-Wonen)来遍历所有机构,关闭弹出屏幕,单击下载按钮,然后对列表中的所有项目再次执行所有操作。
但是,在第一个循环之后,它在选择循环中选择第二个机构时抛出StaleElementReferenceException。从我读到的内容来看,这意味着第一个循环中定义的元素不再可访问。我已经阅读了多篇文章,但我不知道如何克服这种特殊情况。
谁能指出我正确的方向?顺便说一句,我正在使用 Python 的 selenium,而且我是编程的初学者,所以我还在学习。如果您能指出我的大致方向,那将对我有很大帮助!我的代码如下:
#importing and setting up parameters for geckodriver/firefox
...
# webpage
driver.get("https://opendata-dashboard.cijfersoverwonen.nl/dashboard/opendata-dashboard/beleidswaarde")
WebDriverWait(driver, 30)
# Get rid of cookie notification
# driver.find_element_by_class_name("cc-compliance").click()
# Store position of download button
element_to_select = driver.find_element_by_id("utilsmenu")
action = ActionChains(driver)
WebDriverWait(driver, 30)
# Drop down menu
driver.find_element_by_id("baseGeo").click()
# Add institutions to array
corporaties=[]
corporaties = driver.find_elements_by_xpath("//button[@role='option']")
# Iteration
for i in corporaties:
i.click() # select institution
driver.find_element_by_class_name("close-button").click() # close pop-up screen
action.move_to_element(element_to_select).perform() # select download button
driver.find_element_by_id("utilsmenu").click() # click download button
driver.find_element_by_id("utils-export-spreadsheet").click() # pick export to excel
driver.find_element_by_id("baseGeo").click() # select drop down menu for next iteration
【问题讨论】:
标签: python selenium staleelementreferenceexception