4

I have problems in clicking this button that looks in HTML code like this:

<form method="post">
<br>
<input type="hidden" value="6" name="deletetree">
<input type="submit" value="Delete Tree" name="pushed">
</form>

and the url that needs to be generated looks like this: http://mysite.com/management.php?Category=2&id_user=19&deteletree=6&pushed=Delete+Tree

Update: I tried this, but it doesnt work:

form_data = urllib.urlencode({'Category' : '2', 'suid' : '19', 'deletetree' : '6', 'pushed' : 'Delete+Tree' })
urllib2.urlopen("management.php", form_data)

This is how I log in:

cj = cookielib.CookieJar() 
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.29 Safari/525.13')] 
username = "user" 
password = "pass" 
USER_ID = '6'

    loginonsite = login("http://mysite.com/myprofile.php",
                        "login_username=%s&login_password=%s&suid=%s".format(username, password, USER_ID)

)

Mike Thunder
  • 451
  • 1
  • 9
  • 19
  • 1. This doesn't seem to have anything to do with python! 2. You can't have GET parameters in a POST request. That's not how HTTP works! – boxed Feb 25 '13 at 08:47

1 Answers1

5

You could use requests to make a post.

import requests
data = {'Category' : '2', 'suid' : '19', 'deletetree' : '6', 'pushed' : 'Delete+Tree' }
response = requests.post('http://mysite.com/management.php', data=data)

print response.text

As more and more of the content of a webpage is generated in JavaScript I find myself using Selenium's webdriver to directly drive a real browser like Chrome when I'm doing this kind of automation now...

Update: Sounds like you need to login first

Now, requests can pass cookies through as well. So you to send a logged in request you would do this

login_data = data={'username': 'user', 'password': 'pass'
post_data = {
    'Category' : '2', 'suid' : '19', 'deletetree' : '6', 'pushed' : 'Delete+Tree'
}
login_response = requests.get('http://mysite.com/myprofile.php', data=login_data)
form_response = requests.post(
    'http://mysite.com/management.php',
     data=post_data, 
     cookies=login_response.cookies
)

So, you do the login, then use the cookies in the response in the next request. Should work. But obviously I can't test that code for your exact situation.

aychedee
  • 24,871
  • 8
  • 79
  • 83
  • Python is much faster than Selenium Webdriver, that why I started to learn this language. The code return the error page, so i think the link isnt build corectly. – Mike Thunder Feb 25 '13 at 09:38
  • Or you need a session cookie, or you need an csrftoken... hence why I often use webdriver. One trick is to use it with Xvfb, a virtual frame buffer. That way you can use it on headless servers. – aychedee Feb 25 '13 at 09:48
  • What error is the page giving? A 500 or 404? the response object has a `.status_code` attribute. – aychedee Feb 25 '13 at 09:49
  • Damm its : Please log in :( . I will post the code of who I log in in the original post – Mike Thunder Feb 25 '13 at 09:55
  • Doesnt work. Still says Please log in. I think I need to simulate somwhow that im using a browser – Mike Thunder Feb 25 '13 at 13:39
  • Well yeah, like I was saying you can use webdriver. But you should be able to make this work. Being logged in is just having the right session cookie. You'll have to do some debugging. See what the response to your first logon request was. Print out the cookies attribute. Things like that. – aychedee Feb 25 '13 at 14:32