Python: clicking a button
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.
Related videos on Youtube
Mike Thunder
Updated on June 04, 2022Comments
-
Mike Thunder almost 2 years
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 about 11 yearsPython 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.
-
aychedee about 11 yearsOr 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 about 11 yearsWhat error is the page giving? A 500 or 404? the response object has a
.status_code
attribute. -
Mike Thunder about 11 yearsDamm its : Please log in :( . I will post the code of who I log in in the original post
-
aychedee about 11 yearsSee my updated answer
-
Mike Thunder about 11 yearsDoesnt work. Still says Please log in. I think I need to simulate somwhow that im using a browser
-
aychedee about 11 yearsWell 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.