How to submit a form with a get action using Robobrowser

浪子不回头ぞ 提交于 2019-12-10 15:28:11

问题


I am using the robobrowser library, I have come across a form:

<form action="/results" id="search">
            <div class="s_ctr">
                <fieldset>
                    <label class="jaw" for="ln">Search by Name</label><input type="text" placeholder="Search by Name" autocomplete="off" value="" tabindex="1" name="ln" maxlength="255" class="name" id="ln" data-key="true"><span>near</span><label class="jaw" for="loc">Enter City, State</label><input type="text" placeholder="Enter City, State" autocomplete="off" value="" tabindex="2" name="loc" maxlength="255" class="location" id="loc" data-key="true">
                </fieldset>
                <input type="submit" value="Find Physician" class="orange-btn" tabindex="4" id="btn-submit">

            </div>
        </form>

My code:

search_form = browser.get_form(id='search')
search_form        
search_form.fields['1']= name
search_form.fields['2']= address
# # Submit the form
browser.submit_form(search_form)
browser

I get the following error and traceback:

Traceback:
File "C:\r1\lib\site-packages\django\core\handlers\base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\r1\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "C:\r1\mlist\ml1\views.py" in ph
  48.             p = getPhone(A.name,A.address)
File "C:\r1\mlist\ml1\views.py" in getPhone
  191.     browser.submit_form(search_form)
File "C:\r1\lib\site-packages\robobrowser\browser.py" in submit_form
  343.         payload = form.serialize(submit=submit)
File "C:\r1\lib\site-packages\robobrowser\forms\form.py" in serialize
  225.         return Payload.from_fields(include_fields)
File "C:\r1\lib\site-packages\robobrowser\forms\form.py" in from_fields
  118.             payload.add(field.serialize(), field.payload_key)    

Exception Type: AttributeError at /ph/
Exception Value: 'unicode' object has no attribute 'serialize'

Can this form be submitted with robobrowser?


回答1:


The issue is the way you're setting the values of the form fields. To change the value of a field, write to its value attribute:

form.fields['ln'].value = name
# Or, as a shortcut...
form['ln'].value = name

Instead of setting the values of the form fields, your example code actually replaces the fields with unicode objects. Running form['ln'] = name doesn't set the value of the ln field to name; it deletes the ln field and replaces it with the name string. Then, when the form attempts to serialize its contents, it tries to call the serialize method of each of its fields. But strings don't define that method, which leads to the traceback you encountered.



来源:https://stackoverflow.com/questions/25067874/how-to-submit-a-form-with-a-get-action-using-robobrowser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!