3

I'm trying to write a script that fills out the destination box on the google flights page. My code is behaving very inconsistently. Sometimes it works perfectly, other times it only types the letter 'B', rather than the full string 'Barcelona'. Sometimes, I get this error message:

"element click intercepted: Element (redacted html code) is not clickable at point (547, 472). Other element would receive the click"

Any idea why it's freaking out like this? Here's my code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import os


#Give path to chrome driver using service argument so it doesn't throw the path deprecation warning
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
chromedriver_path = '/path/to/chromedriver'
abs_chromedriver_path = os.path.join(script_dir, chromedriver_path)

driver_service = Service(executable_path = abs_chromedriver_path)
browser = webdriver.Chrome(service = driver_service)


url = 'https://www.google.com/travel/flights'
selector = "div[aria-placeholder='Where from?'] input"
phrase = "Barcelona"

browser.get(url)

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector))).click()

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector))).send_keys(phrase)

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector))).send_keys(Keys.ENTER)
3
  • An element might be in the way. Commented Aug 27, 2022 at 2:28
  • @ArundeepChohan how would I verify that and then move around it? Commented Aug 27, 2022 at 2:52
  • 1
    Most likely it's a timing issue. Your script is starting to type in the box and then the box reloads. Commented Aug 27, 2022 at 4:27

1 Answer 1

1

This is one way to look for a flight from Barcelona to Vancouver, select the first suggestion from the suggestion lists for both starting/endpoint, and clicking 'Search':

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time as t


chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1280,720")

webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)

url = 'https://www.google.com/travel/flights'

browser.get(url) 

try:
    cookie_button = WebDriverWait(browser, 3).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'button[aria-label="Reject all"]')))
    print(cookie_button.location_once_scrolled_into_view)
    t.sleep(1)
    cookie_button.click()
    print('rejected cookies')
except Exception as e:
    print('no cookie button')
t.sleep(1)
comboboxes = WebDriverWait(browser, 3).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'input[role="combobox"]')))
comboboxes[0].click()
comboboxes[0].clear()
comboboxes[1].send_keys('Barcelona')
t.sleep(1)
suggestion_listbox = WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "ul[role='listbox']")))
print(suggestion_listbox[-1].get_attribute('outerHTML'))
suggestion_listbox[-1].find_elements(By.TAG_NAME, 'li')[0].click()

comboboxes[2].click()
comboboxes[3].send_keys('Vancouver')
t.sleep(1)
suggestion_listbox = WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "ul[role='listbox']")))
print(suggestion_listbox[-1].get_attribute('outerHTML'))
suggestion_listbox[-1].find_elements(By.TAG_NAME, 'li')[0].click()
t.sleep(1)
WebDriverWait(browser, 3).until(EC.element_to_be_clickable((By.XPATH,'//span[text() = "Search"]'))).click()

You just have to adapt it to your own selenium setup. Selenium docs: https://www.selenium.dev/documentation/

Sign up to request clarification or add additional context in comments.

2 Comments

Hey thank you so much for the answer! Your solution works, but I'd really like to be able to send in 'Barcelona' without entering the combo box, if that makes sense. Do you know of a way to just directly access the departure input box, instead of choosing it from a list of combo boxes? Thanks!
Those 'comboboxes' are elements (with the same role) tied together with javascript, and you need to interact with one to be able to write in the next one. This is a direct way of accessing that input box.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.