簡體   English   中英

如何使用Selenium和Python更改此日歷中的日期?

[英]How to change date in this calendar using Selenium with Python?

我正在嘗試使用Python和Selenium來瀏覽此網站: https : //markets.ft.com/data/etfs/tearsheet/historical? s = O9P:SES: USD

我需要在左側日歷中更改年份,以便查看2018年的價格。 即使我設法更改年份並在下拉菜單中選擇2018,我也無法單擊日期(例如,2018年1月1日)。 這很奇怪,因為我在2019年點擊一天沒有問題。

我真的不知道為什么 這是無效的代碼。

 driver.find_element_by_css_selector("body > div.o-grid-container.mod-container > div:nth-child(2) > section.mod-main-content > div:nth-child(1) > div > div > div.mod-ui-filter-overlay.clearfix.mod-filter-ui-historical-prices-overlay > div.mod-ui-overlay.mod-ui-filter-overlay__form > div > form > fieldset > span > div.mod-ui-date-picker.mod-filter-ui-historical-prices-overlay__date--from > div.mod-ui-date-picker__input-container > i").click()
    select_element_from = driver.find_element_by_xpath("//*[@class='mod-ui-date-picker mod-filter-ui-historical-prices-overlay__date--from']//select[option[@value = '%d']]" %lastyear)
    select_from = Select(select_element_from)
    select_from.select_by_visible_text(lastyearstr)
    time.sleep(2)
    driver.find_element_by_xpath("//*[@class='mod-ui-date-picker mod-filter-ui-historical-prices-overlay__date--from']//*[@aria-label='1 Jan, %d']" %(y-1)).click()

非常感謝您的幫助!

在代碼的最后一行: driver.find_element_by_xpath("//*[@class='mod-ui-date-picker mod-filter-ui-historical-prices-overlay__date--from']//*[@aria-label='1 Jan, %d']" %(y-1)).click()不會顯示該元素。 由於該元素在瀏覽器中不可見,因此無法單擊它。

我采用了分步方法來模擬用戶單擊網頁時的操作。 這是有效的代碼:

# Find the element that expands the date picker
filter_arg = '//span[@class="o-labels" and @aria-hidden="false"]'
filter_icon = driver.find_element_by_xpath(filter_arg)
filter_icon.click()

# Find the calendar icon
cal_arg = '//i[@class="mod-icon mod-icon--calendar"]'
cal_icon = driver.find_element_by_xpath(cal_arg)
cal_icon.click()

# Find the dropdown selector that selects the year
year_arg = '//select[@class="picker__select--year"]'
year_selector = driver.find_element_by_xpath(year_arg)
year_selector.click()

# Find '2018'
last_year = driver.find_element_by_xpath('//option[@value="2018"]')
last_year.click()

# Find all the days that are available, and click on the one with the relevant `.text` attribute
which_day = driver.find_elements_by_xpath('//div[@class="picker__day picker__day--infocus"]')
which_day[0].text
'1'

# If i want to click on the 4th day of the month...
which_day[3].click()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM