Home > Back-end >  unable to locate element by XPATH on Tripadvisor
unable to locate element by XPATH on Tripadvisor

Time:10-14

I don't know how to click on this image, I tried many things but it doesn't work. That says the XPATH is wrong. what I want to click : enter image description here

enter image description here

my code :

# TripAdvisor
driver.get("https://www.tripadvisor.fr/")
time.sleep(0.5)
driver.implicitly_wait(3)
driver.find_element(By.XPATH, '//*[@id="onetrust-reject-all-handler"]').click()
time.sleep(1.5)
driver.implicitly_wait(3)

if choices == "r":
    driver.find_element(By.CSS_SELECTOR, '#lithium-root > main > div.cBOoN > div.rGQXC._T.ivvQp > div > div > div:nth-child(4) > a').click()
    driver.implicitly_wait(1)
    driver.find_element(By.XPATH, '/html/body/div[3]/div/form/input[1]').send_keys(city)
    time.sleep(0.75)
    driver.implicitly_wait(2)
    driver.find_element(By.XPATH, '//*[@id="typeahead_results"]/a[1]').click()
    driver.implicitly_wait(2)
    try:
        driver.find_element(By.XPATH, '//*[@id="onetrust-reject-all-handler"]').click()
    except:
        pass


    time.sleep(2)
    driver.find_element(By.XPATH, '//*[@id="component_48"]/div/div[2]/div[2]/div[3]/div/label').click()
    time.sleep(1)
    driver.implicitly_wait(2)
    driver.find_element(By.XPATH, '//*[@id="component_36"]/div[1]/div[1]/div/span[2]/span').click()
    time.sleep(1)
    driver.implicitly_wait(2)
    driver.find_element(By.XPATH, '//*[@id="component_48"]/div/div[2]/div[2]/div[1]/div/label').click()
    driver.find_element(By.XPATH, '//*[@id="component_48"]/div/div[2]/div[2]/div[2]/div/label').click()
    time.sleep(2)
    driver.implicitly_wait(2)
    driver.find_element(By.XPATH, '//*[@id="component_2"]/div/div[1]/div/span/div[1]/div[1]/span[2]/a').click()

I also tried with coordinates but that don't work. Thank you for anyone who helps me.

CodePudding user response:

instead of digging through divs, you could try looping through elements that match a more specific xpath, like this

//a[contains(@href,"html")]//descendant::img[contains(@src,"https://media-cdn")]

CodePudding user response:

You need to improve your locators and use WebDriverWait expected_conditions explicit waits instead of driver.implicitly_wait.
the following code works:

import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)

url = "https://www.tripadvisor.fr/"

driver.get(url)
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@data-test-target='nav-links']//a[contains(@href,'Restaurants')]"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[data-test-attribute*='RESTAURANTS'] input[type='search']"))).send_keys("Paris")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#typeahead_results a[href*='Restaurants']"))).click()
time.sleep(1)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='checkbox_5']"))).click()
time.sleep(4)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(),'Supprimer tous les filtres')]"))).click()
time.sleep(2)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='checkbox_3']"))).click()
time.sleep(2)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='checkbox_4']"))).click()
time.sleep(2)
wait.until(EC.presence_of_element_located((By.XPATH, "//a[contains(@href,'Review-')]//div[contains(@class,'carousel')]//span[@class]"))).click()
  • Related