Automating filling in input fields and clicking a button using selenium

Question:

I am trying to create a flight scraper between two locations using google flights.

I have tried the following:

from selenium import webdriver

if __name__ == "__main__":
  driver=webdriver.Chrome()
  driver.get('https://www.google.com/travel/flights')
  driver.maximize_window()

  where_from = driver.find_element('xpath',
                               '/html/body/c-wiz[2]/div/div[2]/c-wiz/div[1]/c-wiz/div[2]/div[1]/div[1]/div[1]/div/div[2]/div[1]/div[1]/div/div/div[1]/div/div/input')
  where_from.send_keys('Sydney')
  where_to = driver.find_element('xpath',
                                 '/html/body/c-wiz[2]/div/div[2]/c-wiz/div[1]/c-wiz/div[2]/div[1]/div[1]/div[1]/div/div[2]/div[1]/div[4]/div/div/div[1]/div/div/input')
  where_to.send_keys('Auckland')
  driver.implicitly_wait(30)
  search = driver.find_element('xpath', '/html/body/c-wiz[2]/div/div[2]/c-wiz/div[1]/c-wiz/div[2]/div[1]/div[1]/div[2]/div/button/div[1]')
  search.click()

I am trying to open up a web browser (chrome) and fill up the where from and where to input fields and click search. The where from and where to fields get filled in, but the search doesn’t seem to work

Asked By: z star

||

Answers:

They didn’t make that easy… You have to click the From, then use .send_keys(), then down arrow and ENTER. Same thing for the To. The code below is working.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.google.com/travel/flights')

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[aria-placeholder='Where from?'] input"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[aria-label='Enter your origin'] input"))).send_keys("Sydney" + Keys.ARROW_DOWN + Keys.ENTER)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[aria-placeholder='Where to?'] input"))).click()
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[aria-label='Enter your destination'] input"))).send_keys("Auckland" + Keys.ARROW_DOWN + Keys.ENTER)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Search']"))).click()

driver.quit()
Answered By: JeffC
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.