提问人:user18175591 提问时间:2/19/2022 更新时间:2/20/2022 访问量:178
使用 try/except 条件时 selenium python 崩溃
selenium python crash when using try/except condition
问:
我正在尝试使用 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>
答:
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()
评论
"stale element reference: element is not attached to the page document"
find_element
get(url)
click()
get(url)