0

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>
2
  • 1
    The exception you posted occurs when you call comments.click(). But in your code snippet you don't show how comments is defined. Please specify exactly what you are doing and what you want to achieve.
    – Misc08
    Commented Feb 19, 2022 at 1:24
  • Selenium gives references to elements in browser memory - when you load new page then it removes elements from memory and when you go back to previous page then these elements can be in different place in memory and references are useless - and this gives your message "stale element reference: element is not attached to the page document". You have to use find_element again, or first you should get all URLS as text, next change pages and go back, and later use get(url) instead of click(). But when you have URLS then you don't have to go back but use directly get(url)
    – furas
    Commented Feb 19, 2022 at 4:19

1 Answer 1

0

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()
2
  • I think this is not the answer, please add this as a comment.
    – xitas
    Commented Feb 19, 2022 at 11:49
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Feb 19, 2022 at 11:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.