Search code examples
pythonseleniumxpathcss-selectorswebdriverwait

Unable to send text to Leaving from field within expedia.com using Selenium and Python


On website https://www.expedia.com/ i need to type on the Leaving from colum,but it only gives me an error:AttributeError: 'NoneType' object has no attribute 'send_keys'. How can i write on Leaving from?

import time

from selenium import webdriver


driver=webdriver.Chrome(executable_path="D:\ChromeDriverExtracted\chromedriver")

driver.implicitly_wait(5)

driver.maximize_window()

driver.get("https://www.expedia.com/")
driver.find_element_by_xpath("//span[text()='Flights']").click()

time.sleep(2)
driver.find_element_by_xpath("//*[@id=wizard-flight-tab-roundtrip]/div[2]/div[1]/div/div[1]/div/div/div/button").send_keys("SFO")

Solution

  • To send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      driver.get("https://www.expedia.com/")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[aria-controls='wizard-flight-pwa']>span"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[aria-label='Leaving from']"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#location-field-leg1-origin"))).send_keys("SFO")
      
    • Using XPATH:

      driver.get("https://www.expedia.com/")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@aria-controls='wizard-flight-pwa']//span"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Leaving from']"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='location-field-leg1-origin']"))).send_keys("SFO")
      
    • Browser Snapshot:

    expedia_from_SFO

    • Note: You have to add the following imports :

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC