Reputation: 149
I'm trying to set the controls on a form from disabled readonly to something usable. But the problem is the control has no name. I saw a previous post where someone else solved this problem. But I have been unable to implement their solution correctly. Since I am new to python, I was hoping someone could shed some light on what I'm doing wrong.
Previous post: Use mechanize to submit form without control name
Here is my code:
import urllib2
from bs4 import BeautifulSoup
import cookielib
import urllib
import requests
import mechanize
# Log-in to wsj.com
cj = mechanize.CookieJar()
br = mechanize.Browser()
br.set_cookiejar(cj)
br.set_handle_robots(False)
# The site we will navigate into, handling it's session
br.open('https://id.wsj.com/access/pages/wsj/us/login_standalone.html?mg=id-wsj')
# Select the first (index zero) form
br.select_form(nr=0)
# Returns None
forms = [f for f in br.forms()]
print forms[0].controls[6].name
# Returns set_value not defined
set_value(value,
name=None, type=None, kind=None, id=None, nr=None,
by_label=False, # by_label is deprecated
label=None)
forms[0].set_value("LOOK!!!! I SET THE VALUE OF THIS UNNAMED CONTROL!",
nr=6)
control.readonly = False
control.disabled = True
Upvotes: 1
Views: 1962
Reputation: 474031
The control which value you are trying to set is actually a button, submit button:
print [(control.name, control.type) for control in forms[0].controls]
prints:
[('landing_page', 'hidden'),
('login_realm', 'hidden'),
('login_template', 'hidden'),
('username', 'text'),
('password', 'password'),
('savelogin', 'checkbox'),
(None, 'submit')]
And, you cannot use set_value()
for a submit button:
submit_button = forms[0].controls[6]
print submit_button.set_value('test')
results into:
AttributeError: SubmitControl instance has no attribute 'set_value'
Hope that helps.
Upvotes: 1