【问题标题】:stale element reference element is not attached to the page document when trying to scrape information from different links尝试从不同链接抓取信息时,旧元素参考元素未附加到页面文档
【发布时间】:2020-08-30 16:11:02
【问题描述】:

我在 Python 上使用 Selenium 并尝试移动光标并单击特定元素。这适用于第一个链接,并且 HTML 的结构对于下一个链接是相同的,但是当通过相同的 web 驱动程序访问第二个链接时,我得到了第二个链接的 StaleElementReferenceException。为什么会发生这种情况,我该如何解决?下面是我正在运行的代码。非常感谢!

def getZest(url):
    zestlist = []
    yearlist = []
    driver.get(url)
    time.sleep(5)
    
    
    result = False;
    attempts = 0;
    while(attempts < 5):
        try:
            Home_Value = wait.until(EC.presence_of_element_located((By.XPATH, "//a[text()='Home value']")))
            action.move_to_element(Home_Value).click().perform()
    
            zestimate = driver.find_element_by_xpath('//*[@id="ds-home-values"]/div/div[3]/button')
            action.move_to_element(zestimate).perform()
            result = True
            break
        except exceptions.StaleElementReferenceException as e:
            print(e)
        attempts = attempts + 1
fivenums = ["https://www.zillow.com/homedetails/212-Haddrell-St-Mount-Pleasant-SC-29464/10922911_zpid/", "https://www.zillow.com/homedetails/20-Grove-St-Hicksville-NY-11801/31127407_zpid/"]
for num in fivenums:
    getZest(num)

【问题讨论】:

  • @DebanjanB 我知道您对 Selenium 有很多经验,如果您能提供帮助,那就太好了!
  • 这听起来像X-Y problem。与其寻求解决问题的帮助,不如编辑您的问题并询问实际问题。你想做什么?
  • @DebanjanB 我正在尝试单击网站上的某个元素以获取同一 Web 驱动程序中的多个链接。但是,当 webdriver 移动到第二个链接时,我得到一个 StaleElementReferenceException。无论我试图访问什么链接,情况都是如此。我想摆脱这个异常并能够单击网站上的元素以获取列表中的所有链接。感谢您的帮助,我将更改问题。

标签: python selenium web-scraping staleelementreferenceexception


【解决方案1】:

我能够使用以下代码获取有关第一个和第二个链接的信息,没有任何错误:

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC
from selenium.webdriver.common.by import By
import time
from selenium.common import exceptions

u = 'https://www.oddsportal.com/moving-margins/'
driver = webdriver.Chrome(executable_path=r"chromedriver.exe")
driver.maximize_window()

def getZest(url):
    zestlist = []
    yearlist = []
    driver.get(url)
    time.sleep(5)
    
    
    result = False
    attempts = 0
    action = webdriver.ActionChains(driver)
    wait = WebDriverWait(driver, 300)
    while(attempts < 5):
        try:
            Home_Value = wait.until(EC.presence_of_element_located((By.XPATH, "//a[text()='Home value']")))
            action.move_to_element(Home_Value).click().perform()
    
            zestimate = driver.find_element_by_xpath('//*[@id="ds-home-values"]/div/div[3]/button')
            action.move_to_element(zestimate).perform()
            result = True
            break
        except exceptions.StaleElementReferenceException as e:
            print(e)
        attempts = attempts + 1
fivenums = ["https://www.zillow.com/homedetails/212-Haddrell-St-Mount-Pleasant-SC-29464/10922911_zpid/", "https://www.zillow.com/homedetails/20-Grove-St-Hicksville-NY-11801/31127407_zpid/"]
for num in fivenums:
    getZest(num)

在您的代码中,没有显示某些变量的实例化位置,因此,也许这就是问题所在。

但是当打开第一个链接时,网站向我显示了谷歌验证码保护,所以,我想你有某种授权,可以在所有者的许可下抓取信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 2017-12-13
    • 2022-12-01
    • 2019-01-08
    相关资源
    最近更新 更多