i would like to change / select the date to "12/03/2022" on this website:
https://www.cyberarena.live/schedule-efootball
with the following code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
if __name__ == '__main__':
options = Options()
options.add_experimental_option ('excludeSwitches', ['enable-logging'])
options.add_argument("start-maximized")
options.add_argument('window-size=1920x1080')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
srv=Service(ChromeDriverManager().install())
driver = webdriver.Chrome (service=srv, options=options)
waitWD = WebDriverWait (driver, 10)
link = f"https://www.cyberarena.live/schedule-efootball"
driver.get (link)
tdayString = "12/03/2022"
driver.execute_script("arguments[0].setAttribute('value',arguments[1])", waitWD.until(EC.element_to_be_clickable((By.XPATH, "//input[contains(@id,'input_comp')]"))), tdayString)
input("Press!")
But nothing changed on the opened site - it is still the actual date selected with this code. How can i change the date in the input / select box?
CodePudding user response:
Javascript doesn't work, and if we try to clear the input box
date_element = driver.find_element(By.XPATH, "//input[contains(@id,'input_comp')]")
date_element.clear()
we get error
InvalidElementStateException: Element is not currently interactable and may not be manipulated
So the only solution I see is to manually select the date from the date picker menu (notice that target_date
and current_date
have different date format! If you want to change this you have to edit the string '%d/%m/%Y'
in the definition of target_date
)
# open the date picker
driver.find_element(By.CSS_SELECTOR, '.EYZRr3').click()
# date to be set
from datetime import datetime
target_date_str = '1/1/2022' # day / month / year
target_date = datetime.strptime(target_date_str, '%d/%m/%Y')
# current date
date_element = driver.find_element(By.XPATH, "//input[contains(@id,'input_comp')]")
current_date = datetime.strptime(date_element.get_attribute('value'), '%m/%d/%Y') # month / day / year
currentMonthYear = driver.find_element(By.CSS_SELECTOR, 'button[data-testid=currentMonthYear]')
# select the month
while currentMonthYear.text != target_date.strftime('%B %Y'): # compare month name and year, example 'January 2023' != 'March 2022'
# compare year and month as integers, example 202205 > 202301
if int(target_date.strftime('%Y%m')) > int(current_date.strftime('%Y%m')):
driver.find_element(By.CSS_SELECTOR, 'button[data-testid=nextMonth]').click()
else:
driver.find_element(By.CSS_SELECTOR, 'button[data-testid=prevMonth]').click()
currentMonthYear = driver.find_element(By.CSS_SELECTOR, 'button[data-testid=currentMonthYear]')
# select the day
driver.find_element(By.XPATH, f"//table[@role='table']//span[text()='{target_date.day}']").click()