owce
owce

Reputation: 67

How to change date in the calendar with Selenium and Python?

I'm trying to change the date with selenium, on the calendar that is here: https://www.wtatennis.com/rankings/singles

Do you have any idea how to do that? For now; my code is pretty basic, as I don't know how to proceed:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver import ActionChains
from tqdm import tqdm
import time

options = Options()
options.add_argument("--headless")

browser = webdriver.Chrome(ChromeDriverManager().install(),options=options)
browser.get('https://www.wtatennis.com/rankings/singles')
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[@data-text="Accept Cookies"]'))).click()
value = "start"
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[@data-text = "Show More"]'))).click()

while(browser.find_element_by_xpath("(//tr[@class='rankings__row'])[last()]").text != value):
    elem = browser.find_element_by_xpath('(//*[contains(text(),"Loading")])[2]')
    value = browser.find_element_by_xpath("(//tr[@class='rankings__row'])[last()]").text
    browser.execute_script("arguments[0].scrollIntoView()", elem)
    WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.XPATH,"//tr[@class='rankings__row']")))
    try:
       WebDriverWait(browser, 10).until_not(EC.text_to_be_present_in_element((By.XPATH,"(//tr[@class='rankings__row'])[last()]"), value))
    except:
        None

Upvotes: 0

Views: 1478

Answers (2)

YasserKhalil
YasserKhalil

Reputation: 9538

@PDHide Thank you very much for your great solution. I took your code and played a little with it (Please give me your notes as I am seeking to learn python skills)

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import datetime

# Date Format >> dd/mm/yyyy
thedate='11/04/2016'

def myClick(by, desc):
    wait = WebDriverWait(driver, 10)
    by = by.upper()
    if by == 'XPATH':
        wait.until(EC.element_to_be_clickable((By.XPATH, desc))).click()
    if by == 'ID':
        wait.until(EC.element_to_be_clickable((By.ID, desc))).click()

day, month, year = thedate.split('/')     
month_name = datetime.date(int(year), int(month), int(day)) 
month = month_name.strftime("%B")

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.wtatennis.com/rankings/singles')
driver.maximize_window()

myClick('xpath', '//*[@data-text="Accept Cookies"]')
myClick('xpath', '//span[contains(text(),"Filter by Date")]/..')
driver.execute_script("window.scrollBy(0,300)")
myClick('xpath', '(//div[@class="date-picker__month-year"]/span[2])[1]')
myClick('xpath', f'//span[@class = "date-picker__button-label" and contains(text(),"{year}")]')
myClick('xpath', '(//div[@class="date-picker__month-year"]/span[1])[1]')
myClick('xpath', f'//span[@class = "date-picker__button-label" and contains(text(), "{month}")]')
myClick('xpath', f'(//span[@class = "date-picker__cell-data" and contains(text(), "{day}")]/..)[1]')

Upvotes: 2

PDHide
PDHide

Reputation: 19929

browser = webdriver.Chrome(options=options)
browser.get('https://www.wtatennis.com/rankings/singles')
browser.maximize_window()

WebDriverWait(browser, 10).until(EC.element_to_be_clickable(
    (By.XPATH, '//*[@data-text="Accept Cookies"]'))).click()




WebDriverWait(browser, 10).until(EC.element_to_be_clickable(
    (By.XPATH, '//span[contains(text(),"Filter by Date")]/..'))).click()


browser.execute_script("window.scrollBy(0,300)")

WebDriverWait(browser, 10).until(EC.element_to_be_clickable(
    (By.XPATH, '(//div[@class="date-picker__month-year"]/span[2])[1]'))).click()


WebDriverWait(browser, 10).until(EC.element_to_be_clickable(
    (By.XPATH,  '//span[@class = "date-picker__button-label" and contains(text(),"1990")]'))).click()

WebDriverWait(browser, 10).until(EC.element_to_be_clickable(
    (By.XPATH, '(//div[@class="date-picker__month-year"]/span[1])[1]'))).click()


WebDriverWait(browser, 10).until(EC.element_to_be_clickable(
    (By.XPATH,  '//span[@class = "date-picker__button-label" and contains(text(), "March")]'))).click()

WebDriverWait(browser, 10).until(EC.element_to_be_clickable(
    (By.XPATH,  '(//span[@class = "date-picker__cell-data" and contains(text(), "12")]/..)[1]'))).click()


input()

you have to use the date picker , change the date month and year as you want

Upvotes: 2

Related Questions