Selenium with Python on TikTok

188 views Asked by At

Hello to everyone I'm trying to run selenium on tiktok to see how Selenium work and what I can do with. But I have a problem. I have this message:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/tiktok-cookie-banner//div/div[2]/button[2]"} (Session info: chrome=119.0.6045.105); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception

I understand that's because he cannot found the xpath that I gave to him but I look mor than one time and the XPATH is correct, do you have an idea for the solution?

my actual code:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC #permet d'attendre que certaines conditions se réalisent avant de poursuivre l'exécution du script
from selenium.webdriver.common.by import By #Permet d'accéder aux différents élements de la page web
import time
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging']) # Here
options.add_experimental_option("detach", True) #Pour garder la page ouverte meme après la fin du script
driver = webdriver.Chrome(options=options) #Pour déterminer quel driver on va utiliser chrome/firefox/edge


driver.get('https://www.tiktok.com/fr/') #aller sur le site que l'on souhaite sur le navigateur
driver.implicitly_wait(10)
time.sleep(5) #On met un temps d'attente pour que le chargement de la page soit complet

# Utilisez les sélecteurs CSS, les ID, les noms de classe, etc., pour localiser l'élément
# Cliquez sur le bouton "Accepter les cookies"
driver.find_element(By.XPATH, "/html/body/tiktok-cookie-banner//div/div[2]/button[2]").click()

time.sleep(5)

# Attendez que la page se charge (vous pouvez utiliser d'autres conditions si nécessaire)
# Par exemple, attendre que le titre de la page soit affiché
# WebDriverWait(driver, 10).until(EC.title_contains("Titre de la page"))
driver.find_element(By.XPATH, '/html/body/div[7]/div[3]/div/div/div[1]/div[1]/div/div/div[4]').click()
time.sleep(2)
#print(driver.title)
#element = driver.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[2]/div[1]/div[1]/div/div[1]/div[1]/a[2]')
#element.click()
#time.sleep(2)
# WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="uc-btn-accept-banner"]'))).click()


time.sleep(2)

I already tried to the wait function

0

There are 0 answers