Unable to locate element using selenium chrome webdriver in python selenium

To locate the search field you can use either of the following Locator Strategies:

  • Using css_selector:

    search = driver.find_element_by_css_selector("input[name="search-search-field"][data-cy-id='search-search-field']")
    
  • Using xpath:

    search = driver.find_element_by_xpath("//input[@name="search-search-field" and @data-cy-id='search-search-field']")
    

Ideally, to locate 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:

    search = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name="search-search-field"][data-cy-id='search-search-field']")))
    
  • Using XPATH:

    search = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name="search-search-field" and @data-cy-id='search-search-field']")))
    
  • 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
    

References

You can find a couple of relevant discussions on NoSuchElementException in:

Leave a Comment