I am very new to Python and learning how to scrap data with Selenium.
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()
Which results in
selenium.common.exceptions.ElementNotVisibleException: Message:
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!
Thanks!
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With