Filling Out Web Form Data Using Built-In Python Modules

10,391

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 webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
i = 2 # do it 2 times
while 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': '[email protected]',
        'month': 'October',
        'day': '29',
        'year': '1920'
    }

    FormPage().fill_form(data).submit()
    driver.quit() # closes the webbrowser
    i = i - 1
Share:
10,391

Related videos on Youtube

DarkPirate
Author by

DarkPirate

Updated on June 15, 2022

Comments

  • DarkPirate
    DarkPirate almost 2 years

    Alright so I have used mechanize, requests, beautiful soup, and even selenium on my venture to do something like this and I have come to the conclusion that urllib and the other default modules are the best way to go. Only problem is I can't figure out how to use it at all.. So can someone please show me some good places to learn about that specifically? Also I learn best by examples so if someone would convert this to what I am asking for that would be great (also include a submit button lol)

    from selenium import webdriver
    
    driver = webdriver.Firefox()
    
    driver.get("http://www.jonessoda.com/contests/back2school")
    element = driver.find_element_by_name("fname")
    element.send_keys("Ben")
    
  • DarkPirate
    DarkPirate over 9 years
    Thank you so much! EXACTLY what I was looking for... and I geuss GUI is pretty cool anyways. I am a beginner to python (A few weeks) I know most basic commands but one think I dont understand is the loop function. Could you edit this code to have a loop function? (Also submit isn't working)
  • Jess
    Jess over 9 years
    Added. Submit is working if you call it. You need to do FormPage().submit() for it to submit.
  • Jess
    Jess over 9 years
    Also, if you do it like 1000x in a row it's very visible that your same ip address is spamming submissions in. I would look into scheduling it at various random times in the day.
  • DarkPirate
    DarkPirate over 9 years
    Great works perfectly now thanks a lot man, and yes I know about submissions. I have been botting for a while now. Also how are proxies used using selenium? That is my last question :)
  • AdjunctProfessorFalcon
    AdjunctProfessorFalcon almost 9 years
    @sheeptest Could this be adopted to login into Facebook or Instagram? Or would it not work with an OAuth login?
  • Jess
    Jess almost 9 years
    GUI automation uses javascript to simulate browser interactions. There can be close to 0 differences between a real user and GUI automation. I don't know why you wouldn't call the OAuth login api directly tho. GUI automation should be your last resort.
  • Jess
    Jess almost 9 years
    If you're trying to do anything malicious, you'll run into issues using this method or hitting the API. (CAPTCHAs and Server-side lockout for N # invalid login attempts)
  • AdjunctProfessorFalcon
    AdjunctProfessorFalcon almost 9 years
    It is my last resort because there's no way to get the access code at the end of the redirect URL using raw API calls, I've tried a slew of methods and nothing's working....
  • AdjunctProfessorFalcon
    AdjunctProfessorFalcon almost 9 years
    Definitely not doing anything malicious, just trying to automate a part of my dev workflow...
  • Jess
    Jess almost 9 years
    Access code? like 2 factor authentication? I don't know what you're trying to get out of logging in to Facebook/Instagram for anything work-related. E2E tests should stub out third party integrations as much as possible.
  • Jess
    Jess almost 9 years