我正在使用robobrowser库,我遇到了一个表单:
<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>
我的代码:
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
我收到以下错误和追溯:
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'
可以使用robobrowser提交此表单吗?
答案 0 :(得分:3)
问题在于您设置表单字段值的方式。要更改字段的值,请写入其value
属性:
form.fields['ln'].value = name
# Or, as a shortcut...
form['ln'].value = name
您的示例代码实际上用unicode对象替换了字段,而不是设置表单字段的值。正在运行form['ln'] = name
并未将ln
字段的值设置为name
; 删除 ln
字段,并将其替换为name
字符串。然后,当表单尝试序列化其内容时,它会尝试调用其每个字段的serialize
方法。但字符串不能定义该方法,这会导致您遇到的追溯。