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

Viewed 125

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")
2 Answers

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
    

It seems like your last "find_element_by_xpath" does not correctly locate the element you are looking for. An xpath that does locate your element is

"//input[@id='location-field-leg1-origin-input']"

I don't think this will completely solve your problem though. One thing is that you need to click on this element rather than simply send keys to it. You need to open the modal where you actually enter and select the airport you are leaving from.

Related