使用 try/except 条件时 selenium python 崩溃

selenium python crash when using try/except condition

提问人:user18175591 提问时间:2/19/2022 更新时间:2/20/2022 访问量:178

问:

我正在尝试使用 Selenium 自动关注 TikTok,但是当我说如果找到按钮时遇到了问题:“关注”单击它并返回,但如果没有找到它,请返回。.

comment1 = driver.find_element(By.CSS_SELECTOR, '#app > div.tiktok-19fglm-DivBodyContainer.etsvyce0 > a')
comment1.click()
time.sleep(7)
try:
    follow = driver.find_element(By.CSS_SELECTOR, '#app > div.tiktok-ywuvyb-DivBodyContainer.etsvyce0 > div.tiktok-1h3j14u-DivFollowButtonWrapper.e143oaad4')
    follow.click()
    driver.back()
except:
    driver.back()
comments.click()

但对不起,我有这个问题

Traceback (most recent call last):
  File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
    exec(code, self.locals)
  File "/home/eslammustafa/Desktop/PY Projects/0undetectable tiktok login.py", line 142, in <module>
    comments.click()
  File "/home/eslammustafa/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 81, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/home/eslammustafa/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 710, in _execute
    return self._parent.execute(command, params)
  File "/home/eslammustafa/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "/home/eslammustafa/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, 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=98.0.4758.102)
Stacktrace:
#0 0x557988df4b33 <unknown>
#1 0x5579888bd6d8 <unknown>
#2 0x5579888c055c <unknown>
#3 0x5579888c0356 <unknown>
#4 0x5579888c060c <unknown>
#5 0x5579888f526f <unknown>
#6 0x5579888e85d6 <unknown>
#7 0x557988911062 <unknown>
#8 0x5579888e7fa3 <unknown>
#9 0x55798891116e <unknown>
#10 0x5579889242fb <unknown>
#11 0x557988910f53 <unknown>
#12 0x5579888e6a0a <unknown>
#13 0x5579888e7ad5 <unknown>
#14 0x557988e262fd <unknown>
#15 0x557988e3f4bb <unknown>
#16 0x557988e280d5 <unknown>
#17 0x557988e40145 <unknown>
#18 0x557988e1baaf <unknown>
#19 0x557988e5cba8 <unknown>
#20 0x557988e5cd28 <unknown>
#21 0x557988e7748d <unknown>
#22 0x7fd141ca8609 <unknown>
python-3.x python-2.7 selenium selenium-web驱动程序

评论

1赞 Misc08 2/19/2022
您发布的异常发生在您调用 comments.click() 时。但是在代码片段中,您没有显示注释是如何定义的。请准确说明您正在做什么以及您想要实现的目标。
0赞 furas 2/19/2022
Selenium提供了对浏览器内存中元素的引用 - 当您加载新页面时,它会从内存中删除元素,当您返回上一页时,这些元素可以在内存中的不同位置,并且引用是无用的 - 这给了你的消息。您必须再次使用,或者首先您应该将所有 URL 获取为文本,然后更改页面并返回,然后使用 而不是 .但是当你有URL时,你就不必返回,而是直接使用"stale element reference: element is not attached to the page document"find_elementget(url)click()get(url)

答:

0赞 vineet singh 2/19/2022 #1

返回上一页后,您需要使用 find 元素再次搜索注释元素 我假设在 driver.back() 之后您要单击 comment1,那么您的代码应该如下所示

comment1 = driver.find_element(By.CSS_SELECTOR, '#app > div.tiktok-19fglm-DivBodyContainer.etsvyce0 > a')
comment1.click()
time.sleep(7)
try:
    follow = driver.find_element(By.CSS_SELECTOR, '#app > div.tiktok-ywuvyb-DivBodyContainer.etsvyce0 > div.tiktok-1h3j14u-DivFollowButtonWrapper.e143oaad4')
    follow.click()
    driver.back()
except:
    driver.back()
comment1 = driver.find_element(By.CSS_SELECTOR, '#app > div.tiktok-19fglm-DivBodyContainer.etsvyce0 > a')
comment1.click()

评论

0赞 xitas 2/19/2022
我认为这不是答案,请将其添加为评论。
1赞 Community 2/19/2022
正如目前所写的那样,你的答案尚不清楚。请编辑以添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。您可以在帮助中心找到有关如何写出好答案的更多信息。