如何解决陈旧元素参考-Selenium(Python)

时间:2019-03-20 08:15:55

标签: python css selenium selenium-webdriver

我知道这个问题已被问过多次,但我似乎无法将任何决议适用于我自己的情况。

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

问题出在一个悬停菜单项上,该菜单项似乎正在生成一个过时的元素引用。我认为这是由于我注销并再次登录?这可能可以解决,还是我应该只是启动一个新的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

2 个答案:

答案 0 :(得分: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()
    

编辑:

根据您添加的stacktrace,异常实际上在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

答案 1 :(得分:0)

尝试使用-

  1. 该位置存在检查元素。
  2. 如果是,那么您可以使用javascript单击
  3. 如果否,则再次查找元素并单击。