【发布时间】:2021-09-17 09:53:12
【问题描述】:
我正在尝试从下拉菜单中提取表格,但我不断收到异常错误,即元素未在最后一个循环中附加到文档。我认为这与点击提交时页面刷新有关。
我的代码:
###Purpose: To retrieve gp election data
import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
## wesbsite details
driver = webdriver.Chrome(executable_path = '/usr/local/bin/chromedriver')
driver.get('http://wbsec.gov.in/results/panchayat_election_detailed_result?election_year=2013')
driver.maximize_window()
## dropdown menu
elements=driver.find_elements_by_tag_name("input")
for element in elements:
if element.get_attribute("value")=="PANCHAYAT SAMITY":
element.click()
districts = Select(driver.find_element_by_id('zilla_parishad'))
for district in districts.options:
district.click()
time.sleep(1)
blocks=Select(driver.find_element_by_id('panchayat_samity'))
for block in blocks.options:
block.click()
time.sleep(1)
poles=Select(driver.find_element_by_id('election_date'))
for pole in poles.options:
pole.click()
time.sleep(1)
test = driver.find_element_by_xpath("//input[@type='submit']").click()
time.sleep(5)
错误出现在这一行:
driver.find_element_by_xpath("//input[@type='submit']").click()
我认为问题在于页面没有足够的时间来加载。我尝试增加睡眠时间以及使用WebDriverWait,但每次都会遇到相同的错误。
【问题讨论】:
-
我已经问过你预期的输出是什么。你懒得回复。您是否需要
PANCHAYAT SAMITY之类的数据,即zilla_parishad?或者别的什么。 -
是的,我正在尝试明智地获取 PANCHAYAT SAMITY 的数据。
-
我添加了一个代码。检查一次。如果这不是您的要求,请告诉我。
-
澄清这一点与 zilla_parishad 不同。首先选择zilla_parishad(区),然后选择Panchayat Samity(街区)。
标签: python selenium staleelementreferenceexception