'selenium python crash when using try/except condition
i'm trying make auto follow to tiktok with selenium, but i got a problem when i say if found Button: "Follow" Click it and go back, but if didn't find it go back..
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()
but for sorry i have a this problem
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>
Solution 1:[1]
You need to search comments element again using find element once you returned to previous page I'm assuming after driver.back() you want to click on comment1 then your code should looks like
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()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
