Search code examples
pythonseleniumui-automationpython-behave

Selenium not able to find the search field


Here is the website - https://www.phptravels.net/m-hotels

I am not able to find the field called the city field(see attached screenshot) Even the selenium is not able to record that field. I want to enter the text in that field.

enter image description here

This is what i have tried but it is not working:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.phptravels.net/m-hotels")
a = driver.find_element_by_class_name("select2-choice")
a.click()
city = driver.find_element_by_css_selector(".select2-input.select2-focused")
city.send_keys("Test")

Solution

  • Try to use below code:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait as wait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium import webdriver
    
    driver = webdriver.Chrome()
    driver.get("https://www.phptravels.net/m-hotels")
    
    wait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Search by Hotel or City Name"))).click()
    wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#select2-drop .select2-input"))).send_keys("Test")