0

I have been working on this for couple days now. Can not really find how to make this work. I am fairly new to aspx websites and fetching information out of them.

I am trying to login/authenticate on a website that uses aspx pages. So I followed this thread which really helped me get this in motion. (Last Answer)

Following those directions, I write:

url = "http://samplewebsite/Main/Index.aspx" # Logon page
username = "user"
password = "password"

browser = RoboBrowser(history=True)
# This retrieves __VIEWSTATE and friends
browser.open(url)
signin = browser.get_form(id='form1')
print(signin)

This is the outcome of that print statement:

<RoboForm __VIEWSTATE=/wEPDwULLTE5ODM2NTU1MzJkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQlidG5TdWJtaXRriD1xvrfrHuJ/0xbQM08yEjyoUg==, __VIEWSTATEGENERATOR=E78488FE, adminid=, btnSubmit=, pswd=>

So it is obvious that I am retrieving the information correctly. Now I have 3 input fields:

adminid
btnSubmit
pswd

Which I can use in the following manner:

signin["adminid"].value = username
signin["pswd"].value = password
signin["btnSubmit"].value = "btnSubmit.x=29&btnSubmit.y=22"

My only problem is the last field btnSubmit which I do not know how to input a value since this is of the following type:

<input type="image" name="btnSubmit" id="btnSubmit" tabindex="3" src="../image/login_btn.gif" style="height:41px;width:57px;border-width:0px;" />

when I submit on the website, using the Chrome Tools I get the following outcome:

    __VIEWSTATE:/wEPDwULLTE5ODM2NTU1MzJkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQlidG5TdWJtaXRriD1xvrfrHuJ/0xbQM08yEjyoUg==
__VIEWSTATEGENERATOR:E78488FE
adminid:user
btnSubmit.x:23
btnSubmit.y:15
pswd:password

Where basically the x,y positions are where I clicked on the page. Really do not know how to do this request through Python. Used this to no avail.

4

1 回答 1

1

When you click on an input object of type image, two form values are set, the button name plus .x for the first, and .y for the other.

However, pressing Enter in a regular text input field will also submit a form, so you don't have to click on a submit button. I'd just leave the value empty altogether.

There is not much flexibility in the way robobrowser handles form submits, to avoid using the submit button you'd have to delete it from the form outright:

del signin.fields['btnSubmit']

before submitting.

If you must submit using the image button, then you'll have to teach Robobrowser how to handle that type; currently it has no handling for these. The following adds that:

from functools import wraps
from robobrowser.forms import form
from robobrowser.forms.fields import Submit, Input

class ImageSubmit(Submit):
    def serialize(self):
        return {self.name + '.x': '0', self.name + '.y': '0'}

def include_image_submit(parse_field):
    @wraps(parse_field)
    def wrapper(tag, tags):
        field = parse_field(tag, tags)
        if type(field) is Input:  # not a subclass, exactly this class
            if field._parsed.get('type') == 'image':
                field = ImageSubmit(field._parsed)
        return field
    return wrapper

form._parse_field = include_image_submit(form._parse_field)

at which point you can use browser.submit_form(signin, signin['btnSubmit']) to submit the form and the correct fields will be included.

I've submitted a pull request to the robobrowser project to add image submit support.

于 2018-03-09T18:50:27.970 回答