如何解决过时的元素参考 - Selenium(Python)

问题描述 投票:3回答:2

我知道这个问题已被多次询问,但我似乎无法将任何解决方案应用于我自己的情况。

我正在抓取某个网站的特定值,但是,这些值存在于网站上的不同配置文件中。因此,我登录,检索值,注销,在新配置文件下重新登录,检索值,注销等。

问题出在其中一个悬停菜单项上,它们似乎生成了陈旧的元素引用。我认为这是由于我退出并再次返回?这是可以修复还是我应该只是启动一个新的WebDriver实例?

这是我到目前为止的代码,在我看来我是Python的新手,所以请原谅任何愚蠢的错误或假设:

from selenium import webdriver
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains
from selenium.common.exceptions import StaleElementReferenceException

options = Options()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:/Users/SChogle/Downloads/chromedriver.exe')
actions = ActionChains(driver)

driver.get("xxxxx")

iframe = ui.WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.TAG_NAME, "iframe")))

driver.switch_to.frame(iframe)

driver.find_element_by_id("Username").send_keys("xxxx")
driver.find_element_by_id("Password").send_keys("xxxx")
driver.find_element_by_id("submit_button").click()

driver.switch_to.default_content()

Investment = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.menu.menuTopCenter > ul > li:nth-child(3) > a")))
actions.move_to_element(Investment).perform()

Investment_Summary = (WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"li:nth-child(3) > div > div:nth-child(1) > a")))).click()

Imp_Prov = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#product-UT td.portfolioProductContractFundHeaderValueRight"))).get_attribute('innerHTML').strip()
print(Imp_Prov)

#log-out
log_out = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a#btnLogoff"))).click()

#log back in

iframe = ui.WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.TAG_NAME, "iframe")))

driver.switch_to.frame(iframe)

driver.find_element_by_id("Username").send_keys("xxxx")
driver.find_element_by_id("Password").send_keys("xxxx")
driver.find_element_by_id("submit_button").click()

driver.switch_to.default_content()

tries = 0
while tries < 3:
    try:
        Investment = WebDriverWait(driver, 10,).until(EC.element_to_be_clickable((By.CSS_SELECTOR,  "div.menu.menuTopCenter > ul > li:nth-child(3) > a")))
        actions.move_to_element(Investment).perform()
        tries = 3

    except StaleElementReferenceException:
        tries += 1

Investment_Summary1 = (WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"li:nth-child(3) > div > div:nth-child(1) > a")))).click()

Imp_Pen = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#product-UT td.portfolioProductContractFundHeaderValueRight"))).get_attribute('innerHTML').strip()
print(Imp_Pen)

请参阅下面的stacktrace:

174,256,175.68 ZAR
Traceback (most recent call last):
  File "C:/Users/SChogle/PycharmProjects/test1/venv/Web Scraping - BCI.py", line 60, in <module>
    Investment_Summary1 = (WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"li:nth-child(3) > div > div:nth-child(1) > a")))).click()
  File "C:\Users\SChogle\PycharmProjects\test1\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 


Process finished with exit code 1
python css selenium selenium-webdriver
2个回答
1
投票

如果你看看source code

class element_to_be_clickable(object):
    """ An Expectation for checking an element is visible and enabled such that you can click it."""
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        element = visibility_of_element_located(self.locator)(driver)
        if element and element.is_enabled():
            return element
        else:
            return False

该元素可能在if element and element.is_enabled():位于前一行(visibility_of_element_located处理StaleElementReferenceException)之前变得陈旧。您可以将ignored_exceptions=[StaleElementReferenceException]添加到WebDriverWait的减速度来解决此问题

Investment1 = WebDriverWait(driver, 10, ignored_exceptions=[StaleElementReferenceException]).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.menu.menuTopCenter > ul > li:nth-child(3) > a")))

还有一点:

  1. 有预期条件frame_to_be_available_and_switch_to_it处理框架
  2. Python变量应该都是小写的
  3. 你有代码重复,你可以使用函数 options = Options() options.add_argument("start-maximized") driver = webdriver.Chrome(options=options, executable_path=r'C:/Users/SChogle/Downloads/chromedriver.exe') actions = ActionChains(driver) driver.get("xxxxxxx") def do_login(): WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME, "iframe"))) driver.find_element_by_id("Username").send_keys("xxxxx") driver.find_element_by_id("Password").send_keys("xxxxx") driver.find_element_by_id("submit_button").click() driver.switch_to.default_content() def print_content(): investment = WebDriverWait(driver, 10, ignored_exceptions=[StaleElementReferenceException]).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.menu.menuTopCenter > ul > li:nth-child(3) > a"))) actions.move_to_element(investment).perform() investment_summary = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"li:nth-child(3) > div > div:nth-child(1) > a"))).click() imp_prov = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#product-UT td.portfolioProductContractFundHeaderValueRight"))).get_attribute('innerHTML').strip() print(imp_prov) do_login() print_content() driver.find_element_by_css_selector("a#btnLogoff").click() do_login() print_content()

编辑:

根据堆栈跟踪,您添加的异常实际上是在actions.move_to_element(Investment1).perform()上。它可以通过简单的循环和重试来解决

tries = 0
while tries < 3:
    try:
        investment = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.menu.menuTopCenter > ul > li:nth-child(3) > a")))
        actions.move_to_element(investment).perform()
        tries = 3
    except StaleElementReferenceException:
        tries += 1

0
投票

尝试使用 -

  1. Check元素出现在该位置。
  2. 如果是,那么您可以使用javascript点击
  3. 如果没有,则再次找到元素并单击。
© www.soinside.com 2019 - 2024. All rights reserved.