如何使用 Selenium 和 Python 更改此日历中的日期? [英] How to change date in this calendar using Selenium with Python?

查看:31
本文介绍了如何使用 Selenium 和 Python 更改此日历中的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 和 Selenium 浏览此网站:https://markets.ft.com/data/etfs/tearsheet/historical?s=O9P:SES:USD.

I'm trying to scrape through this website using Python and Selenium: https://markets.ft.com/data/etfs/tearsheet/historical?s=O9P:SES:USD.

我需要更改左侧日历上的年份,以便查看 2018 年的价格.即使我设法更改年份并在下拉菜单中选择 2018,我也无法单击这一天(例如,2018 年 1 月 1 日).这很奇怪,因为我在 2019 年点击一天没有问题.

I need to change years on the left calendar, in order to see the 2018 prices. Even though I manage to change year and select 2018 in the dropdown menu, I can't click on the day (for example, 1 Jan 2018). It is weird since I have no problem with clicking on a day in 2019.

我真的不知道为什么.这是不起作用的代码.

I really don't know why. Here's the code which does not work.

 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() 元素是不显示.由于该元素在浏览器中不可见,因此无法点击.

In the last line of your code: 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() the element is not displayed. Since the element is not visible on the browser, it cannot be clicked.

我采用了分步方法来模拟用户在点击网页时会执行的操作.这是有效的代码:

I've adopted a step-by-step approach simulating what a user would do as he clicks through the webpage. Here is the code which works:

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

这篇关于如何使用 Selenium 和 Python 更改此日历中的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆