Selecting an unnamed text field in a mechanize form (python)

早过忘川 提交于 2019-12-01 06:28:06

问题


So i'm making a program to batch convert street addresses to gps co-ordinates using mechanize and python. this is my first time using mechanize. I can select the form ("form2') on the page. however the text box in the form has no name. how do i select the textbox so that mechanize can enter my text? I've tried selecting it by its id. but that does not work.

br.select_form("Form2") #works as far as i know
br.form["search"] = ["1 lakewood drive, christchurch"] #this is the field that i cannot select

and here is the source code from the website.

<form name="Form2" >
or  Type an <b>Address</b>
<input id="search" size="40" type="text" value=""  >
<input type="button" onClick="EnteredAddress();" value="Enter" />
</form>

any help would be much appreciated.


回答1:


form.find_control(id="search") ?




回答2:


FWIW I solved this by using the above answer by lazy1 except that I was trying to assign a value after using the find_control method. That didn't work of course because of assignment, I looked deeper into the method and found setattr() and that worked great for assigning a value to to the field.

will not work

br.form.find_control(id="field id here") = "new value here"

will work

br.form.find_control(id="field id here").__setattr__("value", "new value here")


来源:https://stackoverflow.com/questions/4787907/selecting-an-unnamed-text-field-in-a-mechanize-form-python

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