Selenium 日历选择器:我可以手动点击但 Selenium 无法点击 [英] Selenium calendar picker: I can click manually but Selenium cant click
问题描述
我正在尝试在 本网站.在第一个日历(日期)上,我可以使用 Selenium 选择所需的日期,但是,即使找到了确切的元素,我在单击所需的月份时也会收到以下错误.
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:元素不可交互
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()
推荐答案
代码错误 mm = driver.find_element_by_xpath('//*[@id={}"]'.format(month))
.您在 DOM 中找到第一个元素(它与元素 data
而不是 data2
组合)并且它还不可见.
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.
有工作代码
mm = driver.find_element_by_id('date2').find_element_by_class_name('months-view').find_element_by_id(month)
mm.click()
也很划算,使用WebDriverWait,因为网站很慢.
Also good deal, to use WebDriverWait, because the site is very slow.
例如
to_date = WebDriverWait(driver, 10).until(
expected_conditions.presence_of_element_located(
(By.XPATH, '//*[@id="date2"]/angular2-date-picker/div/div[1]/i')))
这篇关于Selenium 日历选择器:我可以手动点击但 Selenium 无法点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!