[英]How to change date in this calendar using Selenium with Python?
I'm trying to scrape through this website using Python and Selenium: https://markets.ft.com/data/etfs/tearsheet/historical?s=O9P:SES:USD . 我正在尝试使用Python和Selenium来浏览此网站: https : //markets.ft.com/data/etfs/tearsheet/historical? s = O9P:SES: USD 。
I need to change years on the left calendar, in order to see the 2018 prices. 我需要在左侧日历中更改年份,以便查看2018年的价格。 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).
即使我设法更改年份并在下拉菜单中选择2018,我也无法单击日期(例如,2018年1月1日)。 It is weird since I have no problem with clicking on a day in 2019.
这很奇怪,因为我在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()
Thank you very much for your help! 非常感谢您的帮助!
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. 在代码的最后一行:
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()
不会显示该元素。 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.