【问题标题】:Message: stale element reference: element is not attached to the page document (Not relevant to click event)消息:过时的元素引用:元素未附加到页面文档(与点击事件无关)
【发布时间】:2023-03-04 11:08:01
【问题描述】:

这是回溯代码。

2021-10-04 18:21:53.294724 : ERROR : Message: stale element reference: element is not attached to the page document
(Session info: chrome=93.0.4577.63)
(Driver info: chromedriver=71.0.3578.80,platform=Linux 4.9.230-76 aarch64)

2021-10-04 18:21:53.319382 : ERROR : Traceback (most recent call last):
File "/home/odroid/Documents/python/crawling-worker/linkedin/crawler.py", line 143, in main_loop
self.main_process()
File "/home/odroid/Documents/python/crawling-worker/linkedin/crawler.py", line 73, in main_process
if self.message_updated():
File "/home/odroid/Documents/python/crawling-worker/linkedin/crawler.py", line 54, in message_updated
now = get_last_messages(self.driver, self.db)
File "/home/odroid/Documents/python/crawling-worker/linkedin/message/parse/conversation.py", line 310, in get_last_messages
i.find_elements_by_class_name("msg-conversation-card__title-row")[0]) for i in members if
File "/home/odroid/Documents/python/crawling-worker/linkedin/message/parse/conversation.py", line 311, in <listcomp>
len(i.find_elements_by_class_name("msg-conversation-card__message-snippet-body"))]
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 413, in find_elements_by_class_name
return self.find_elements(by=By.CLASS_NAME, value=name)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 685, in find_elements
{"using": by, "value": value})['value']
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=93.0.4577.63)
(Driver info: chromedriver=71.0.3578.80,platform=Linux 4.9.230-76 aarch64)


2021-10-04 18:21:53.356802 : INFO : Device shutdown.

这是出错的块代码

def get_last_messages(driver: WebDriver, db: Db):
    safe_open(driver, "https://www.linkedin.com/messaging", db)
    members = driver.find_elements_by_class_name("msg-conversation-listitem")
    temp = [(i.find_elements_by_class_name("msg-conversation-card__message-snippet-body")[0],
             i.find_elements_by_class_name("msg-conversation-card__title-row")[0]) for i in members if
            len(i.find_elements_by_class_name("msg-conversation-card__message-snippet-body"))]
    cards = [(i[0].text, none_or_value(i[1].find_elements_by_tag_name("time"))) for i in temp]
    return [(i[0], i[1].text) for i in cards if i[1]]

你们知道这里有什么问题吗,因为我预计这个函数“find_elements_by...”会在没有什么可以获取的时候返回空数组[],但是这里抛出异常。 谢谢你帮我这么多!

【问题讨论】:

    标签: python selenium web-scraping web-crawler linkedin


    【解决方案1】:

    只有当您的元素在您的网站中不存在(消失或未构建)时才会发生 StaleElementReferenceException。请添加等待条件以检查元素的可见性。

    By optionXpath = By.xpath("your xpath fp goes here !");
    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.elementToBeClickable(optionXpath));
    wait.until(ExpectedConditions.visibilityOfElementLocated(optionXpath));
    driver.findElement(optionXpath).click();
    

    未构造的情况:

    需要添加等待条件

    万一消失:

    您对元素的操作是错误的。在您之前添加屏幕截图 失败代码并重新检查您的代码。

    【讨论】:

    • 谢谢您的回答,先生。但我只是想知道我没有在这个元素中执行任何操作(单击或其他)。我只是用“find_elements”函数获取元素=>如果不存在,我应该返回[]。但是为什么我会得到这个异常?如果它消失了,它应该返回 [] 而不是 Exception,对吗?对不起,如果我对你的话有任何误解/
    • 但是您在方法中提到了其他索引,find_elements_by_class_name("msg-conversation-card__message-sn-p-body")[0],
    • 所以,如果数组返回 [],但我访问第 0 个元素 => 它应该返回“列表超出范围”异常,不是吗?
    • 在这种情况下,元素不是空的 [] 但未附加到主 html 页面。最好添加等待时间(我希望这会解决)并截图进行调试。
    • 对不起,我真的不明白你的意思。如果它不是空的,它应该返回一些东西而不是这个异常?我认为如果它没有附加到主 html 页面 => 它应该返回 []。你能详细解释一下吗?很抱歉给您带来不便。抱歉,我可以调试这个,因为它有时会出现,我无法重现它。
    猜你喜欢
    • 2018-10-23
    • 2021-12-19
    • 2019-09-30
    • 2022-08-17
    • 1970-01-01
    • 2019-02-03
    • 2022-01-21
    • 2021-05-10
    • 2020-11-13
    相关资源
    最近更新 更多