Home > OS >  clicking on the element in selenium
clicking on the element in selenium

Time:03-27

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()

CodePudding user response:

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()
  • Related