消息:陈旧元素引用:元素未附加到页面文档(与单击事件无关)

2024-05-29 10:58:29 发布

您现在位置:Python中文网/ 问答频道 /正文

这是回溯代码。

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…”在没有什么可获取的时候返回空数组[],但这里抛出异常。 谢谢你们帮了我这么多


Tags: nameinpyselfmessagebyseleniumline
1条回答
网友
1楼 · 发布于 2024-05-29 10:58:29

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

在未构建的情况下:

you need to add wait conditions

如果消失:

yours actions on element is wrong. add screenshot just before your failure code and recheck your code.

相关问题 更多 >

    热门问题