Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

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

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 = 'pathtochromedriverchromedriver.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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'))) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...