Search code examples
javascriptpythonseleniumselenium-webdriverauto-populate

How to input a text field which is autopopulating in Python Selenium


I have a address input text field on website which is basically auto populated by java script. This is not a drop down field through which ou can select some value, neither you can input your own text field.

enter image description here

enter image description here

I am performing some tests using selenium but I am not able to input this field with some text value. The below option is not working, and test is failing at this point. I basically want to input 328 West 91st Street address in the field but selenium is failing here.

input_text(driver, locator, '328 West 91st Street')

Reason why it is failing

This doesn't seem like a regular input text field since a javascript object is attached to it for auto populating. Selenium is not able to recognize this. Could you tell me any work around? My locator is correct


Solution

  • With Selenium, you want to simulate a normal user's actions.

    First, enter the input and wait for the browser to respond.

    browser = webdriver.Firefox()
    input_box = b.find_element_by_name("address-search-box")
    input_box.send_keys('328 West 91st Street')
    time.sleep(3)
    

    The browser should be sending a request to retrieve search results from the server based on the user input. Once it does that, the browser should render the results.

    With Selenium, "click" the DOM element of the first result presuming your user input returns the search result you are looking for.

    browser.find_element_by_name("search-result-1").click()
    

    The input should now contain a valid result.