StaleElementException with Python



我试图从下拉菜单中提取表,但我一直收到一个异常错误,即元素在最后一个循环中没有附加到文档。我认为这是因为点击提交时页面会刷新。

我的代码:

###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,但每次都会出现同样的错误。

问题不在于它没有时间加载,而是元素不再是新的和可访问的

您可能希望在收到此错误时尝试合并一种刷新页面的方法,以便再次拥有此元素。

另一种方法是保存每次搜索时用来查找元素的文本,而不是保存物理元素来搜索每个循环。

即:

for i in range(len(driver.find_elements_by_tag_name("input"))):
if driver.find_elements_by_tag_name("input")[i].get_attribute("value")=="PANCHAYAT SAMITY":
driver.find_elements_by_tag_name("input")[i].click()

现在的情况是,您正试图从下拉列表中选择选项,其中for district in districts.options也有--Select--作为选项。当你点击Get Results时,页面会把我们带到其他地方。我们需要忽略--Select--选项,所以for loop应该像这个for i in range(1,len(districts)):

点击Get Results后,Selenium将无法识别以前的选项,因此我们需要再次在for loop中找到这些选项。否则抛出Element not attached to the page异常。

最后,一些选项的Polling Date可能什么都没有,所以抛出List of index out of range。所以把它放在试块里。

import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome(executable_path = 'path to chromedriver.exe')
driver.get('http://wbsec.gov.in/results/panchayat_election_detailed_result?election_year=2013')
driver.maximize_window()
driver.implicitly_wait(30)
driver.find_element_by_id("search_type_ps").click()
option1 = Select(driver.find_element_by_id("zilla_parishad")).options
for i in range(1,len(option1)):
try:
option1 = Select(driver.find_element_by_id("zilla_parishad")).options
print("Options 1 :{}".format(option1[i].text))
option1[i].click()
time.sleep(2)
options2 = Select(driver.find_element_by_id("panchayat_samity")).options
opt2list = []
for j in range(1,len(options2)):
options2 = Select(driver.find_element_by_id("panchayat_samity")).options
print("Options 2 : {}".format(options2[j].text))
# opt2list.append(opt2.text)
options2[j].click()
time.sleep(2)
options3 = Select(driver.find_element_by_id("election_date")).options
try:
for k in range(1,len(options3)):
options3 = Select(driver.find_element_by_id("election_date")).options
print("Options 3 : {}".format(options3[k].text))
ele_dates=options3[k].text
options3[k].click()
driver.find_element_by_xpath("//input[@name='submit']").click()
time.sleep(1)
except:
pass
except:
driver.get('http://wbsec.gov.in/results/panchayat_election_detailed_result?election_year=2013')
driver.find_element_by_id("search_type_ps").click()
driver.quit()

最新更新