【问题标题】:StaleElementException with PythonPython 的 StaleElementException
【发布时间】: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


【解决方案1】:

问题不是没有时间加载而是元素是no longer fresh and accessible

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

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

即:

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()

【讨论】:

  • 我试过这个,但我得到了同样的错误。
  • 尝试合并刷新或解决此问题,这不是明确的解决方案,但更旨在帮助指导您,每个网站的导航方式不同,但如果您收到 StaleElementException,这将表明引用的参考资料已过时且不再可用
【解决方案2】:

发生的情况是您正尝试使用此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。所以把它放在 try 块中。

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()

【讨论】:

  • 非常感谢您的代码。迭代工作了一段时间,我可以看到页面加载但我收到以下错误:NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“css selector”,“selector”:“[id = "panchayat_samity"]"}
  • @ak7880 - 这是因为很少有选项有No results found。把整个东西放在一个 try 块中。如果发生异常,请从头开始做所有事情。我已经更新了相同的代码。
  • 我的意思是,从头开始做所有事情,但有其他选择。
  • 代码完美运行!在下一步中,我将尝试提取所有生成的表,所以我假设我只是将其添加到 try 块中?
  • @ak7880 - 是的,那是正确的。提取表格的代码将出现在 for 循环中的 driver.find_element_by_xpath("//input[@name='submit']").click() 之后。
猜你喜欢
  • 2020-07-10
  • 2017-11-07
  • 1970-01-01
  • 2015-09-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多