Filling Out Web Form Data Using Built-In Python Modules Filling Out Web Form Data Using Built-In Python Modules selenium selenium

Filling Out Web Form Data Using Built-In Python Modules


You do want Selenium. It simulates GUI interactions on a browser.When doing things like entering competition form data, this is going to be the way that is least detectable.

A note about selenium: It is not a language-specific library. There are client specific bindings for each language. Most examples and how-to's you'll see are actually written in Java.

A good resource is Selenium-python

Here's your working example. Including submit button.

from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECi = 2 # do it 2 timeswhile i > 0:    driver = webdriver.Firefox()    driver.get("http://www.jonessoda.com/contests/back2school")    def find_by_xpath(locator):        element = WebDriverWait(driver, 10).until(            EC.presence_of_element_located((By.XPATH, locator))        )        return element    class FormPage(object):        def fill_form(self, data):            find_by_xpath('//input[@name = "fname"]').send_keys(data['fname'])            find_by_xpath('//input[@name = "lname"]').send_keys(data['lname'])            find_by_xpath('//input[@name = "email"]').send_keys(data['email'])            find_by_xpath('//select[@name = "birthday_month"]').send_keys(data['month'])            find_by_xpath('//select[@name = "birthday_day"]').send_keys(data['day'])            find_by_xpath('//select[@name = "birthday_year"]').send_keys(data['year'])            return self # makes it so you can call .submit() after calling this function        def submit(self):            find_by_xpath('//input[@value = "Submit"]').click()    data = {        'fname': 'Sheep',        'lname': 'Test',        'email': 'jess@sheeptest.com',        'month': 'October',        'day': '29',        'year': '1920'    }    FormPage().fill_form(data).submit()    driver.quit() # closes the webbrowser    i = i - 1