Home > other >  Why does the pop-up calendar not click into the next month (python, selenium)?
Why does the pop-up calendar not click into the next month (python, selenium)?

Time:10-13

I wrote a simple code below to open the reservation calendar and click to the following month, but the calendar doesn't seem to be switching to the next month. Is there something glaring that I am missing?

driver.get("https://www.tablecheck.com/shops/peterluger/reserve")
driver.maximize_window() 

driver.find_element_by_id("reservation_start_date").click()
driver.find_element(By.XPATH, "//div[@aria-label='Next Month']").click()

CodePudding user response:

The outer HTML

<input class="form-control mobidate i-txt refresh-menu-items mbsc-comp" readonly="" placeholder="-- Select Date --" type="text" name="reservation[start_date]" id="reservation_start_date" value="2021-10-13">

shows that it has an value attribute in form of 2021-10-13.

We do not need to click on this input field and then select a value from the calendar.

Instead we can directly pass the value attribute using execute script in Python-Selenium bindings.

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
wait = WebDriverWait(driver, 30)

driver.get("https://www.tablecheck.com/shops/peterluger/reserve")
date = wait.until(EC.visibility_of_element_located((By.ID, "reservation_start_date")))
driver.execute_script("arguments[0].scrollIntoView(true);", date)
driver.execute_script("arguments[0].setAttribute('value', '2021-10-13')", date)

Imports :

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

CodePudding user response:

Before click the element, try to move first to the target.

Learn about ActionChains:

driver.get("https://www.tablecheck.com/shops/peterluger/reserve")
driver.maximize_window() 

driver.find_element_by_id("reservation_start_date").click()

element = driver.find_element(By.XPATH, "//div[@aria-label='Next Month']")
action = ActionChains(driver)
action.move_to_element(element).click(element).perform()

Following import:

from selenium.webdriver import ActionChains
  • Related