3

Hello all I am trying to practice selenium with python on the following website booking.com in that I want to click an element where in can select on the next month. I have tried to write the xpath for it which is even valid when checked on chrome console. however it does not click on the next arrow below is the snapshot wherein i want to click on the next arrow and console pic for your reference. Can you please tell me where i am going wrong on thisenter image description here

enter image description here

from selenium import webdriver import time from selenium.webdriver.common.by import By driver = webdriver.Chrome(executable_path='C:\chromedriver') driver.get("https://www.booking.com/") time.sleep(4) driver.maximize_window() time.sleep(2) #place to add the destination location driver.find_element(By.XPATH, "//input[@placeholder='Where are you going?']").send_keys("Jaisalmer") time.sleep(5) #location name select driver.find_element(By.XPATH, ".//*[contains(@data-label,'Suryagarh ')]").click() time.sleep(3) driver.find_element(By.CLASS_NAME, "sb-date-field__icon sb-date-field__icon-btn bk-svg-wrapper calendar-restructure-sb").click() time.sleep(6) driver.execute_script("window.scrollTo(24,document.body.scrollHeight);") nextbutton = driver.find_element(By.XPATH, "//div[@class='bui-calendar__control bui-calendar__control--next']").click() #for i in range(3): #nextbutton.click() time.sleep(4) driver.quit() 

1 Answer 1

4

Your code is almost correct. You just need to add a short delay directly before clicking on the next month button.
Also you should not use all these hardcoded pauses like time.sleep(4) Expected Conditions explicit waits should be used instead.
I understand you want to click the next month button 3 times?
This code will do what you are looking for:

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time from selenium.webdriver.common.by import By driver = webdriver.Chrome(executable_path='C:\chromedriver') wait = WebDriverWait(driver, 20) driver.get("https://www.booking.com/") driver.maximize_window() wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@placeholder='Where are you going?']"))).send_keys("Jaisalmer") wait.until(EC.visibility_of_element_located((By.XPATH, "//*[contains(@data-label,'Suryagarh ')]"))).click() for i in range(3): time.sleep(0.5) wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@data-bui-ref='calendar-next']"))).click() 
Sign up to request clarification or add additional context in comments.

4 Comments

Thankyou @Prophet can i know why have you not added time in your explicit wait code??
before clicking the next month button? I tried and I saw it's not working, this web page needs some more delay there.
in general like for eg in this wait.until(EC.visibility_of_element_located((By.XPATH, "//input[@placeholder='Where are you going?']"))).send_keys("Jaisalmer") there is not time mentioned
The timeout for the Expected Conditions object is defined with wait = WebDriverWait(driver, 20) it is 20 seconds here. But it is not a delay, it is a timeout while Selenium will continue polling until the condition is met, here - visibility of element.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.