简体   繁体   中英

How to use selenium to handle and select elements from the date picker on the website?

I have a problem in selenium to scrap the data that i want by selecting the specific date from the date picker on the website. However, the code below I tried (eg i pick 11 April 2019) could only generate the data on the latest date (24 April 2019). Is there any way to make it work to access the date that i want?

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.action_chains import ActionChains

options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome('chromedriver.exe', options=options)
driver.set_window_size(1024, 768)
actions = ActionChains(driver)
ticker = '00001'

y = '2019'
m = '4'
d = '11'

year_list = {'2018':'//*[@id="date-picker"]/div[1]/b[1]/ul/li[1]/button', '2019':'//*[@id="date-picker"]/div[1]/b[1]/ul/li[2]/button'}
month_list = {'1':'//*[@id="date-picker"]/div[1]/b[2]/ul/li[1]/button', '2':'//*[@id="date-picker"]/div[1]/b[2]/ul/li[2]/button', '3':'//*[@id="date-picker"]/div[1]/b[2]/ul/li[3]/button', '4':'//*[@id="date-picker"]/div[1]/b[2]/ul/li[4]/button', '5':'//*[@id="date-picker"]/div[1]/b[2]/ul/li[5]/button', '6':'//*[@id="date-picker"]/div[1]/b[2]/ul/li[6]/button', '7':'//*[@id="date-picker"]/div[1]/b[2]/ul/li[7]/button', '8':'//*[@id="date-picker"]/div[1]/b[2]/ul/li[8]/button', '9':'//*[@id="date-picker"]/div[1]/b[2]/ul/li[9]/button', '10':'//*[@id="date-picker"]/div[1]/b[2]/ul/li[10]/button', '11':'//*[@id="date-picker"]/div[1]/b[2]/ul/li[11]/button', '12':'//*[@id="date-picker"]/div[1]/b[2]/ul/li[12]/button'}
day_list = {'1':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[1]/button','2':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[2]/button','3':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[3]/button','4':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[4]/button','5':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[5]/button','6':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[6]/button','7':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[7]/button','8':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[8]/button','9':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[9]/button','10':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[10]/button','11':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[11]/button','12':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[12]/button','13':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[13]/button','14':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[14]/button','15':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[15]/button','16':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[16]/button','17':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[17]/button','18':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[18]/button','19':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[19]/button','20':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[20]/button','21':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[21]/button','22':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[22]/button','23':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[23]/button','24':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[24]/button','25':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[25]/button','26':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[26]/button','27':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[27]/button','28':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[28]/button','29':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[29]/button','30':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[30]/button','31':' //*[@id="date-picker"]/div[1]/b[3]/ul/li[31]/button'}

year = year_list[y]
month = month_list[m]
day = day_list[d]

ccass = driver.get('http://www.hkexnews.hk/sdw/search/searchsdw_c.aspx')

popup_datepicker = driver.find_element_by_xpath('//*[@id="txtShareholdingDate"]').click()
actions.move_to_element(popup_datepicker).click()
ccass_year = driver.find_element_by_xpath(year)
actions.move_to_element(ccass_year)
actions.double_click(ccass_year)
time.sleep(0.5)

actions.move_to_element(popup_datepicker).click()
ccass_month = driver.find_element_by_xpath(month)
actions.move_to_element(ccass_month)
actions.double_click(ccass_month)
time.sleep(0.5)


actions.move_to_element(popup_datepicker).click()
ccass_day = driver.find_element_by_xpath(day)
actions.move_to_element(ccass_day)
actions.double_click(ccass_day)
time.sleep(0.5)

ccass_search = driver.find_element_by_xpath('//*[@id="txtStockCode"]').send_keys(ticker) #Keys.ENTER) 
ccass_search_click = driver.find_element_by_xpath('//*[@id="btnSearch"]').click()

You could set the value using javascript.

Try this:

driver.execute_script("document.getElementById('txtShareholdingDate').value='2019/4/11'")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM