Issue
I am trying to create a simple web scraper through which I can scrape rates of a hotel for 5 consecutive dates. However using Selenium, I am unable to navigate into the calendar dropdown to select the required date or directly insert the date I want into the date input box. Element I want to automate marked in red
How do I proceed?
Link for website: https://www.google.com/travel/search
I tried using the class for the div and input tags, but that returned an 'elementclickinterceptedexception' error.
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
checkin_query= ['Tue, Aug 1', 'Wed, Aug 2', 'Thu, Aug 3']
search_query= ['JW Marriott Mumbai', 'Sofitel Mumbai']
options = webdriver.ChromeOptions()
#options.add_argument('--headless')
for j in range(len(checkin_query)):
for i in range(len(search_query)):
browser = webdriver.Chrome("chromedriver", options= options)
browser.get('http://www.google.com/travel/search')
searchElement = browser.find_element(By.XPATH, '//input[contains(@placeholder,"Search for places, hotels and more")]')
checkinElement = browser.find_element(By.XPATH, '//input[contains(@placeholder,"Check-in")]')
checkinElement.send_keys(search_query[j])
searchElement.clear()
searchElement.send_keys(search_query[i])
time.sleep(2)
dropdownElement= browser.find_element(By.XPATH, '//li[contains(@role,"option")]').click()
I have attached the code snippet. Apart from the date insertion option, everything works fine
Solution
[You could keep clicking the next-day button until the value matched the desired check-in date, but I can see why you might want to avoid such an inefficient method...]
checkinElement.send_keys(search_query[j])
There are a couple of issues with what you're trying to do here:
first off, you probably want
checkin_query[j]
and thesearch_query
was just a typo...when I tried your code, it did input your value, but it added onto whatever was already there, so you need to clear the field first
- unfortunately,
checkinElement.click()
+checkinElement.clear()
didn't seem to be working [although you can give it a try - it could be a version problem...]
So I tried using
Keys
instead and that worked for me# from selenium.webdriver.common.keys import Keys checkinElement.send_keys(Keys.CONTROL, "a") checkinElement.send_keys(Keys.BACKSPACE) checkinElement.send_keys(checkin_query[j])
- unfortunately,
Answered By - Driftr95
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.