1

I would like to fill in and submit a form on a web page using python.

This form:

<form method="POST" id="login" action="site/enter.php" >
  <input type="text" placeholder="Login" name="login" class="login" tabindex="1">
  <input type="password" placeholder="Password" name="psw" class="password" tabindex="2">
  <input type="submit" class="btn_auth" value="" title="Enter" tabindex="3">
</form>

But can't do it. I place login and pass, but can't emulate submit button.

7
  • What? What kind of web-framework are you using, if any? What are you having troubles with? We need waaaay more info. Commented Jul 31, 2012 at 14:29
  • Please post the non-working code, otherwise this question is way too vague. Commented Jul 31, 2012 at 14:34
  • This looks like a job for Selenium Webdriver! Commented Jul 31, 2012 at 14:34
  • cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) urllib2.install_opener(opener) values = dict(login=username, psw=userpass, frm='submit') data = urllib.urlencode(values) req = urllib2.Request(root_url, data) home_page = opener.open(req) page_text=home_page.read() print page_text Commented Jul 31, 2012 at 14:35
  • you have to use something like cURL to post your form. Commented Jul 31, 2012 at 14:35

1 Answer 1

2

It's much easier to use selenium's Webdriver to drive webpages than to use something like mechanize. Especially if the page is dynamically created.

You'll need to install selenium. pip install selenium should work. You'll also need a version of Firefox installed.

from selenium import webdriver

browser = webdriver.Firefox()
browser.implicitly_wait(5)
browser.get('http://some_url.com')
browser.find_element_by_id('login').send_keys('your_login') 
browser.find_element_by_name('pws').send_keys('your_password')
browser.find_element_by_class_name('btn_auth').click() 

print browser.page_source
browser.close()
Sign up to request clarification or add additional context in comments.

8 Comments

unless the page uses JavaScript. It is an overkill
Well, since so much of the web is now rendered with JS I find it's best to go there first. What part didn't work Ur Riv?
It just open Chrome and don't do anything
So there is no stacktrace? I noticed an extra space in my code which I just removed? You aren't just blindly copying this are you?
@aychedee - I'm getting a message 'WebDriver' object has no attribute 'implicitly'. What is that line supposed to do? Does leaving it off change how this works?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.