gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:58:13 27 4
gpt4 key购买 nike

我在 Selenium 中遇到问题,无法通过从网站上的日期选择器中选择特定日期来抓取我想要的数据。然而,我尝试的下面的代码(例如我选择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()

最佳答案

您可以使用 JavaScript 设置该值。

试试这个:

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

关于python - 如何使用 Selenium 处理和选择网站上日期选择器中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55841503/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com