pythondateselenium-webdriverautomationfield

Unable to locate elements for Dates field- automation selenium using python


I am trying to do automation on Expedia.com Below steps I am following

  • Going to Expedia.com
  • click on Flights button
  • by default it will set for Round trip option
  • Click on Leaving from and select value from it
  • click on Going to and select value from it
  • click on Dates

Here on Dates I am unable to locate the elements.

Below is my code.

import time

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

driver=webdriver.Chrome()

driver.implicitly_wait(10)

driver.maximize_window()

driver.get("https://www.expedia.com/")

driver.implicitly_wait(10)

//Click on RoundTrip
driver.find_element(By.XPATH,"//span[normalize-space()='Flights']").click()

//Leaving From
leaving_from = driver.find_element(By.XPATH,"//button[@aria-label='Leaving from']")
leaving_from.click()
search_by_city= driver.find_element(By.XPATH,"//input[@id='origin_select']")
search_by_city.click()
time.sleep(10)
search_by_city.send_keys("SFO")
time.sleep(10)
search_by_city.send_keys(Keys.ENTER)

//Going To
going_to = driver.find_element(By.XPATH,"//button[@aria-label='Going to']")
going_to.click()
search_going_to = driver.find_element(By.XPATH,"//input[@id='destination_select']")
search_going_to.click()
time.sleep(10)
search_going_to.send_keys("NYC")
time.sleep(10)
search_going_to.send_keys(Keys.ENTER)

time.sleep(10)

//Dates 
dates = driver.find_element(By.CSS_SELECTOR,"#date_form_field-btn")
dates.click()
time.sleep(10)

This the error

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#date_form_field-btn"}

Please anyone help me to help me to solve this error.


Solution

  • The error you're getting suggest that there's not present in the DOM an element that has id="date_form_field-btn" and if you manually follow the steps Selenium is simulating for you in the browser, i.e., if you go to https://www.expedia.com/ and click on Flights, and the you either inspect the page (ctrl+shift+i) or read the source html (ctrl+u), you'd find that, indeed, there's no such element in the whole DOM.

    However the are two candidates for what you want:

    1. There's a button with the tag <button data-stid="uitk-date-selector-input1-default" name="EGDSDateRange-date-selector-trigger" ... > that represent the dates field, and luckily that particular value for the attr name is only present once in the DOM. So you could find the dates field by:
    dates = driver.find_element(By.CSS_SELECTOR,'button[name="EGDSDateRange-date-selector-trigger"')
    dates.click()
    
    1. There are two hidden (non-visible) input elements present in the DOM that represent, correspondingly, the start and end date for the trip:
    • <input type="hidden" data-stid="EGDSDateRangePicker-StartDate" ...>; and
    • <input type="hidden" data-stid="EGDSDateRangePicker-EndDate" ...>;

    and you could try to directly pass the values you want to each of them instead.