Search code examples
pythonseleniumwebdriver

Send keys to element selenium python


I am writing a script to automatically fill out a form for me, but I am struggling to 'find element'. I can see it when I inspect, but I cant get it to send information to the placeholder.

This is the element im trying to send_keys to.

<input type="text" placeholder="Next step" class="pl-10 pr-12 form-input block w-full py-2 px-3 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:shadow-outline-blue focus:border-blue-300 transition duration-150 ease-in-out sm:text-sm sm:leading-5" required="" value="">

This is the parent element.

<div class="nested-fields air-pipeline-step-item" draggable="true" data-handler-id="T0" style="opacity: 1;"><input name="vacancy[vacancy_pipeline_steps_attributes][0][id]" type="hidden" value=""><input name="vacancy[vacancy_pipeline_steps_attributes][0][step_order]" type="hidden" value="0"><input name="vacancy[vacancy_pipeline_steps_attributes][0][display_name]" type="hidden" value="Awaiting Review">

When trying:

self.driver.find_element_by_name('vacancy[vacancy_pipeline_steps_attributes][0][id]').click()

I get:

ElementNotVisibleException: Message: element not visible

I would like to click on this element and then give it a value. Any suggestions on how to do this ?


Solution

  • I would use a css selector here – allows for more versatile element selection:

    css_selector = 'div[class*="nested-fields"][class*="air-pipeline-step-item"] > input[type="text"]'
    
    # Easier to navigate try and excepts with find many
    my_input = self.driver.find_elements_by_css_selector(css_selector)
    if my_input:
        my_input = my_input[0]
    else:
       # Try a diff selector – or raise an error
       raise ValueError(f'couldn\'t find an element with using {css_selector=}')
    
    # Continue code here 
    my_input.send_keys('Hello World!')