0

I'm trying to find the search box on tripadvisor but sometimes it is not able to find it. (I get timeout error). I have added WebDriverWait and time.sleep but to no avail. I am still in the midst of testing so there is a part of my code requiring human intervention below.

Edit: Changing to time.sleep(10) instead of 5 seconds helps. But it makes the code very slow. Hoping to use WebDriverWait instead...

searchbox = WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH, "//input[@placeholder='Where to?']")))

Below is my full code, where I have to find the searchbox multiple times for each country.

from selenium.webdriver.common.by import By
from selenium import webdriver 
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

driver = webdriver.Chrome(executable_path=r'C:\\Users\\user\\Downloads\\chromedriver.exe') 
driver.get("https://www.tripadvisor.com.sg/Hotels-g293951-Malaysia-Hotels.html")

Human intervention here: Click on a random check-in date, check-out date and then click "Update"

countries3 = ["France","Qatar","Brunei Darussalam","Hong Kong"]
countries4 = ["Germany","The Netherlands","Nigeria","South Africa","Russia"]
countries2 = ["New York","Saudi Arabia","Sri Lanka","Switzerland","Turkey"]
countries1 = ["United Arab Emirates","New Zealand","Thailand","India"]
countries5 = ["Vietnam","Australia","Malaysia","Cambodia","Indonesia","Laos","Myanmar","Philippines","Thailand","Vietnam","Australia",             "Canada","China","Japan","Taiwan","United Kingdom","United States","Bangladesh","Belgium","Egypt","Finland"]
countries = countries1+countries2+countries3+countries4+countries5

for country in countries: 
    driver.get(URL)
    driver.implicitly_wait(10)
    time.sleep(5)
    searchbox = WebDriverWait(driver,20).until(EC.presence_of_element_located((By.XPATH, "//input[@placeholder='Where to?']")))
    searchbox.send_keys(country) #select your destination here 
    time.sleep(2)
    possible = driver.find_elements_by_class_name("_3a_rGDNB")
    for i in possible:
        if i.text == country:
            i.click()
            break
            
    time.sleep(5)

    hotels = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@title="Hotels"]')))
    driver.execute_script("arguments[0].click();", hotels)
    time.sleep(3)

ping
  • 13
  • 3

2 Answers2

1

To entered value in search input box

Use WebDriverWait() and wait for element_to_be_clickable() and following xpath.

searchbox = WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH, "//input[@aria-label='Search']")))
KunduK
  • 26,790
  • 2
  • 10
  • 32
0

To find the search box on Tripadvisor instead of presence_of_element_located() you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    searchbox = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='q'][title='Search']")))
    
  • Using XPATH:

    searchbox = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='q' and @title='Search']")))
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217