I am trying to extract table from a website using python selenium. In the webpage, i need to select the desired date. For day and month there are two dropdown box. For these two i am using css selector and able to select the date and month. But the problem is with year. It is a text box with a default value '2020' the current year. So first I simply tried sending "2019" as input. But that wouldn't work and return the default page data, i.e, the current date data.
Then I tried clearing the textbox and then send the input as "2019". This method returns a table with NANs. How can I send 2019 as input for year?
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from datetime import datetime, timedelta
from math import ceil
import pandas as pd
import numpy as np
from time import time, sleep
import re
CHROMEDRIVER_PATH = '/path /to/chromedriver'
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(CHROMEDRIVER_PATH, chrome_options=options)
driver.get("URL")
# date = datetime.now() - timedelta(days=1)
date = datetime(2019,11,4)
year = date.strftime("%Y")
month = date.strftime("%m")
day = date.strftime("%d")
date = date.strftime("%Y-%m-%d")
driver.find_element_by_css_selector("#ContentPlaceHolder2_txtyear").clear() #Clear the default value from the box
sleep(1)
driver.find_element_by_css_selector("#ContentPlaceHolder2_txtyear").send_keys(year) #Send the year as a string
sleep(1)
driver.find_element_by_css_selector("#ContentPlaceHolder2_txtyear").send_keys(Keys.ENTER) #Hit enter after sending the key
sleep(1)
driver.find_element_by_css_selector("#ContentPlaceHolder2_ddmonth").send_keys(month) #Send input for month
driver.find_element_by_css_selector("#ContentPlaceHolder2_ddday").send_keys(day) # send input for day
html = driver.page_source
pd.read_html(html)[0]
Edit: The year textbox element from source code is as follows:
<input name="ctl00$ContentPlaceHolder2$txtyear"
type="text"
value="2020"
onchange="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder2$txtyear\',\'\')', 0)"
onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;"
id="ContentPlaceHolder2_txtyear"
style="height:16px;width:46px;margin-left: 0px">