如何使用硒来处理和选择网站上日期选择器中的元素?

时间:2019-04-25 03:44:07

标签: python selenium datepicker screen-scraping

我在硒中存在问题,可以通过从网站上的日期选择器中选择特定日期来抓取我想要的数据。但是,我尝试的以下代码(例如,我选择2019年4月11日)只能在最新日期(2019年4月24日)生成数据。有什么办法可以使其访问我想要的日期?

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()

1 个答案:

答案 0 :(得分:1)

您可以使用javascript设置值。

尝试一下:

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