Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save steven-mercatante/f4ff72b7d9a20b04146eed8e977dab65 to your computer and use it in GitHub Desktop.
Save steven-mercatante/f4ff72b7d9a20b04146eed8e977dab65 to your computer and use it in GitHub Desktop.
Helper function to quickly fill out inputs using Selenium
def fill_inputs(driver, inputs):
"""
Fill out one or many input fields.
Usage:
inputs = {'#title': 'foo', '.age': 42}
fill_inputs(driver, inputs)
This is meant to replace repetitive and verbose calls like:
title_input = driver.find_element_by_id('title')
title_input.send_keys('foo')
age_input = driver.find_element_by_classname('age')
age_input.send_keys(42)
:param driver: A Selenium web driver
:type driver: selenium.webdriver.remote.webdriver.WebDriver
:param inputs: Dictionary of HTML element selectors and the values to fill them in with
:type inputs: dict
:return: Dictionary of the HTML elements
:rtype: dict
"""
elements = {}
for selector, value in inputs.iteritems():
elements[selector] = driver.find_element_by_css_selector(selector)
elements[selector].send_keys(value)
return elements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment