0

So I am having problems with my code. Everytime i run it, I cannot click on the 'Accept Cookies'Button on the tiktok page I am trying to access. It simply just gives me a Timeoutexception error.

from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import chromedriver_autoinstaller
from selenium_stealth import stealth
chromedriver_autoinstaller.install() 
driver = webdriver.Chrome()

driver.get('https://www.tiktok.com/tag/reizen?lang=en')


WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "/html/body/tiktok-cookie-banner//div")))
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"/html/body/tiktok-cookie-banner//div/div[2]"))).click()
   

I am not sure where the problem is.

Many thanks!

Stephen
  • 15
  • 3
  • I can't see there element matching `/html/body/tiktok-cookie-banner//div` XPath. Are you sure it is there? – Prophet Aug 28 '22 at 14:14
  • Can you explain a little. Not sure I fully understand. Thanks! – Stephen Aug 28 '22 at 15:12
  • I have opened the URL you are using here in a new incognito browser and I see no elements matching the above locator. Also no any kind of accept cookies banners appeared. – Prophet Aug 28 '22 at 15:30
  • Interesting, I do. Even in incogenito. Is there some settings I can change to prevent the cookies coming up when I run in selenium? – Stephen Aug 29 '22 at 05:39
  • Since I can't reproduce that I don't know – Prophet Aug 29 '22 at 06:40

1 Answers1

0

What I noticed when using the Selenium head to manually debug is that the cookie element does not show up on my machine. If you have anything on your browser/machine that could be preventing the cookie popup, that may be a factor.

An additional problem is that the tiktok-cookie-banner element uses the shadow DOM, which introduces a whole separate element of complexity. To solve this problem, I recommend the shadow-automation-selenium package, which the documentation explains how to use to interact with the element you are interested in. More information about the shadow DOM can be found in this thread.

Aaron Meese
  • 1,670
  • 3
  • 22
  • 32