如何使用Selenium和Python单击带有日历的特定日期

时间:2019-01-10 23:32:04

标签: python selenium selenium-webdriver xpath webdriverwait

我正在尝试使用Selenium从包含多个下拉菜单和表格的网站读取数据。 这是链接:“ https://markets.ft.com/data/funds/tearsheet/historical?s=NL0006294175:EUR”。

我需要更改日期范围,假设我想查看1月1日至1月5日这只股票的价格,因此,我单击了两个小日历。可以从左侧表格中选择并单击某个日期,这是可以的,我设法使用xpaths和css选择器做到了这一点。

但是,单击右侧表格中的日期确实很困难,我也不明白为什么。 Python总是显示如下错误:

selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable

这是我的代码:

driver.get(r'https://markets.ft.com/data/etfs/tearsheet/historical?s=O9P:SES:USD')
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 > h2 > span").click()
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()
driver.find_element_by_xpath('//*[@title="Next month"]').click()
driver.find_element_by_xpath("//*[@aria-label='1 Jan, %d']" %(y)).click()  #The click on the table on the left works
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--to > div.mod-ui-date-picker__input-container > i").click() 
time.sleep(2)
driver.find_element_by_xpath("//*[@aria-label='5 Jan, %d']" %(y)).click()   #this does not work, the element is not interactable

我也尝试使用ActionChains和WebDriverWait,但没有任何效果。 我怀疑问题在于这两个表确实很相似,并且硒试图访问第一个表,即使它不再可见了,但我真的不知道该如何解决。

您知道是否可以单击第二张表中的日期吗?

谢谢。

4 个答案:

答案 0 :(得分:1)

您可以尝试下面的日历事件代码。

driver.findElement(By.xpath("//div[contains(@class, 'date--from')]")).click();
driver.findElement(By.xpath("//div[contains(@class, 'date--from')]//div[contains(@class,'picker__nav--next')]")).click();
driver.findElement(By.xpath("//div[contains(@class, 'date--from')]//div[@aria-label='1 Jan, 2019']")).click();
driver.findElement(By.xpath("//div[contains(@class, 'date--to')]")).click();
driver.findElement(By.xpath("//div[contains(@class, 'date--to')]//div[contains(@class,'picker__nav--next')]")).click();
driver.findElement(By.xpath("//div[contains(@class, 'date--to')]//div[@aria-label='5 Jan, 2019']")).click();

如果它不起作用,则可以在单击日历后使用一些等待时间来打开日历。

答案 1 :(得分:0)

问题在于,有2个元素与您在该页面上提到的选择器匹配。最简单的检查方法是打开浏览器控制台并手动进行检查。

您需要一种区分两者的方法。为了获得清晰的xpath,您一次只能打开一个日期选择器。然后,您可以使用类似以下的内容进行匹配。

//div[contains(@class,'picker--opened')]//*[@aria-label='5 Jan, 2019']

答案 2 :(得分:0)

有2个问题,当您单击第一个日期时,会有一个重叠的元素阻止您单击第二个输入,您需要等到删除此元素后,才需要选择第二个输入日期,可以使用xpath { {1}}

(//the_xpath_selector)[2]

答案 3 :(得分:0)

要查看从1月1日到1月5日的股票价格,请点击以诱使 WebDriverWait 为所需的元素可点击,您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions")
    driver= webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get(r'https://markets.ft.com/data/etfs/tearsheet/historical?s=O9P:SES:USD')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='mod-ui-filter-overlay__filter-toggle']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='mod-ui-date-picker mod-filter-ui-historical-prices-overlay__date--from']//i[@class='mod-icon mod-icon--calendar']"))).click()
    driver.find_element_by_xpath("//div[@class='mod-ui-date-picker mod-filter-ui-historical-prices-overlay__date--from']//div[@title='Next month']").click()
    driver.find_element_by_xpath("//div[@class='mod-ui-date-picker mod-filter-ui-historical-prices-overlay__date--from']//div[@aria-label='1 Jan, 2019']").click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='mod-ui-date-picker mod-filter-ui-historical-prices-overlay__date--to']//i[@class='mod-icon mod-icon--calendar']"))).click()
    driver.find_element_by_xpath("//div[@class='mod-ui-date-picker mod-filter-ui-historical-prices-overlay__date--to']//div[@aria-label='5 Jan, 2019']").click()
    
  • 浏览器快照:

Calander_Clicks