python&硒无法在日期选择器中选择日期 [英] Python & Selenium Cannot select date in datepicker
问题描述
我是Python的新手,正在学习如何使用Selenium抓取数据.
I am very new to Python and learning how to scrap data with Selenium.
尝试从monmondo.com上的日期选择器表单中选择日期时遇到问题(出于示例目的)
I encounter a problem when trying to pick a date from a datepicker form on monmondo.com (for the sake of example)
这是我设法获得的最远距离:(我设法比以前走得更远,但我仍然被困住了)
This is the farthest I managed to get: (edit: I managed to go a little further than before but I am still stuck)
from selenium import webdriver
browser = webdriver.Firefox()
browser.get("https://www.momondo.com")
browser.implicitly_wait(5)
date = browser.find_element_by_name("ctl00$Content$ctl04$SearchFormv8$SearchFormFlight$InputDepart").click()
browser.implicitly_wait(5)
test= browser.find_elements_by_xpath("//*['ui-datepicker-div']//td[@data-year='2017'][@data-month='2']/a[@class='ui-state-default'][@href='#'][text()='20']")
test[0].click()
会导致
selenium.common.exceptions.ElementNotVisibleException: Message:
我已经用firepath测试了xpath,它似乎可以正常工作,因为它可以在页面的源代码中找到.
I've tester the xpath with firepath and it seems to work correctly as it is found in the page's source code.
源代码中日历日的网页结构为:
The webpage structure of the calendar's day in the source code is:
<td class=" " data-handler="selectDay" data-event="click" data-month="2" data-year="2017"><a class="ui-state-default" href="#">20</a></td>
<a class="ui-state-default" href="#">20</a>
我模糊的猜测是,即使单击数据也可以触发选择,但它似乎位于我可以找到该数字的类的上方.话虽这么说,我不确定是这样.
My vague guess is that the data-even click triggers the selection but it seems to be located a step above the class where I can find the number. This being said I am not sure it's the case.
如果您能帮助像我这样的新来者,我将不胜感激!
I would really appreciate if you could help a newcomer like me!
谢谢!
推荐答案
尝试添加一些时间等待元素变得可见:
Try to add some time to wait until element become visible:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Firefox()
browser.get("https://www.momondo.com")
browser.implicitly_wait(5)
# Click to open drop-down
date = browser.find_element_by_xpath("//div[@class='input _date-depart']/div[@class='ui-calendar']/input").click()
# Choose depart date
wait(browser, 10).until(EC.visibility_of_element_located((By.XPATH, "//td[@data-handler='selectDay']/a[text()='20']"))).click()
# Choose return date
wait(browser, 10).until(EC.visibility_of_element_located((By.XPATH, "//td[@data-handler='selectDay']/a[text()='30']"))).click()
这篇关于python&硒无法在日期选择器中选择日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!