1

I am getting the following error when using Selenium in Python to enter text into a search field on the USPS careers website:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="WD40"]"}

The URL is: https://wp1-ext.usps.gov/sap/bc/webdynpro/sap/hrrcf_a_unreg_job_search#

driver = webdriver.Chrome("C:/Users/NAME/Downloads/chromedriver_win32/chromedriver.exe")
driver.get("https://wp1-ext.usps.gov/sap/bc/webdynpro/sap/hrrcf_a_unreg_job_search#")
elem = driver.find_element_by_name("WD40")
elem.send_keys("denver")

When I dive into the selectors, the following is in the input field.

<input id="WD40" ct="I" lsdata="{29:'WD3E'}" lsevents="{Change:[{ResponseData:'delta',EnqueueCardinality:'single'},{}]}" type="text" tabindex="0" ti="0" class="lsField__input" autocomplete="off" autocorrect="off" name="WD40" style="width:350px;" title="">

So I should be able to enter the ID of 'WD40' into the ID field, but I am still getting that error.

Please send help.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Mike Jani
  • 13
  • 3
  • You can try doing `find_element_by_id` but this should work as well. Your code works for me. Perhaps you should upgrade your Python, Selenium or Chrome driver. – Ronald May 29 '20 at 20:36

1 Answers1

0

The desired element is a dynamic element so to locate/click() on the element using Selenium 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://wp1-ext.usps.gov/sap/bc/webdynpro/sap/hrrcf_a_unreg_job_search#")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.lsField__input"))).send_keys("denver")
    
  • Using XPATH:

    driver.get("https://wp1-ext.usps.gov/sap/bc/webdynpro/sap/hrrcf_a_unreg_job_search#")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='lsField__input']"))).send_keys("denver")
    
  • 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
    
  • Browser Snapshot:

denver

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you DebanjanB! This worked quite well. I had no idea that the element needed to be clicked manually before typing could occur. Does this mean that the input technically doesn't exist until I click it? – Mike Jani May 31 '20 at 13:57
  • @MikeJani Factually, we haven't clicked, but we waited for the `element_to_be_clickable` for the JavaScript/AJAX to complete before invoking `send_keys("denver")` – undetected Selenium May 31 '20 at 18:37