Selenium calendar picker: I can click manually but Selenium cant click

余生长醉 提交于 2020-12-13 03:25:44

问题


I am attempting to choose date on a calendar on this website. On the first calendar (date from) I can choose the desired date using Selenium, however, I get the following error while clicking on the desired month even though the exact element is found.

ElementNotInteractableException:element not interactable

To me, it seems weird because I can click on the month manually. Here is what I have tried so far

from selenium import webdriver
import time

year = 2019
month = 'JAN'
driver_path = 'pathtochromedriver\chromedriver.exe'
url = 'https://app.cpcbccr.com/ccr/#/caaqm-dashboard-all/caaqm-landing/data'
driver = webdriver.Chrome(driver_path)
driver.get(url)
time.sleep(8)

# find desired calendar
to_date = driver.find_element_by_xpath('//*[@id="date2"]/angular2-date-picker/div/div[1]/i')
to_date.click()
# Click on year dropdown
to_year = driver.find_element_by_xpath('//*[@id="date2"]/angular2-date-picker/div/div[2]/div[3]/div')
to_year.click()

driver.find_element_by_xpath('//*[@id="{}"]'.format(year)).click()
# Click on month dropdown
to_month = driver.find_element_by_xpath('//*[@id="date2"]/angular2-date-picker/div/div[2]/div[2]/div')
to_month.click()
mm = driver.find_element_by_xpath('//*[@id="{}"]'.format(month))
mm.click()

回答1:


Mistake in code mm = driver.find_element_by_xpath('//*[@id="{}"]'.format(month)). You find first element in DOM(which combined with element data instead data2) and it is not visible yet.

There is workig code

mm = driver.find_element_by_id('date2').find_element_by_class_name('months-view').find_element_by_id(month)
mm.click()

Also good deal, to use WebDriverWait, because the site is very slow.

for example

to_date = WebDriverWait(driver, 10).until(
    expected_conditions.presence_of_element_located(
        (By.XPATH, '//*[@id="date2"]/angular2-date-picker/div/div[1]/i'))) 


来源:https://stackoverflow.com/questions/64660455/selenium-calendar-picker-i-can-click-manually-but-selenium-cant-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!