1

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
Community
  • 1
  • 1
user3285763
  • 157
  • 1
  • 3
  • 14

1 Answers1

1

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.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Ah ok that does make sense. What would I use in its place in order to change the controls? – user3285763 Mar 24 '14 at 00:52
  • @user3285763 ok, what do you actually want to do with the form controls? – alecxe Mar 24 '14 at 01:00
  • I'm trying to set them so I can log-in – user3285763 Mar 24 '14 at 01:04
  • @user3285763 at least you should set `username` and `password` and call `br.submit()`: see [example](http://stackoverflow.com/questions/8570920/submitting-forms-with-mechanize-python). – alecxe Mar 24 '14 at 01:18
  • I tried that first. Didn't seem to work. I experimented on a few other sites and it entered the site correctly. But for the WSJ it wouldn't go past the log-in page. No error was returned. I was checking br.title() and it produced the same output "Log In." – user3285763 Mar 24 '14 at 01:25