Selenium can do this for you if you do not mind the browser displaying on your screen. It can also work without a head, but it is more difficult. A simple solution:
from selenium import webdriver driver = webdriver.Firefox() url = "http://www.w3schools.com/html/html_forms.asp" driver.get(url) # get a list of the page forms as Selenium WebElements # (webdriver API ref: http://selenium-python.readthedocs.org/en/latest/api.html) forms = driver.find_elements_by_xpath(".//form") for i, form in enumerate(forms): print i, form.text # the last form, index number 5, has input tags of type "text" and "submit" """ <form name="input0" target="_blank" action="html_form_action.asp" method="get"> " Username: " <input type="text" name="user" size="20"> <input type="submit" value="Submit"> </form> """ # get the input WebElements from this form WebElement inputs = forms[5].find_elements_by_xpath(".//input") # write text to the text input, then submit the form inputs[0].send_keys('hihi frds!') inputs[1].submit()
source share