RoboBrowser BadRequestKeyError(key)
Question: I am trying to sign in to a website using RoboBrowser and I am stuck with a
error message.

My code:

    
    
    from robobrowser import RoboBrowser
    
    browser = RoboBrowser()
    
    def login():
        browser.open('https://www.kijiji.ca/t-login.html')
        form = browser.get_form(id="login-form")
        form.fields["LoginEmailOrNickname"].value = "an_email_address"
        form.fields["login-password"].value = "a_password"
        form.fields["login-rememberMe"].value = "true"
        browser.submit_form(form)
    
    login()  
    

The error message:

    
    
      Traceback (most recent call last):
      File "/home/rojaslee/Desktop/kijiji_poster/kijiji_poster.py", line 16, in <module>
        login()
      File "/home/rojaslee/Desktop/kijiji_poster/kijiji_poster.py", line 11, in login
        form.fields["LoginEmailOrNickname"].value = ["an_email_address"]
      File "/usr/local/lib/python3.4/dist-packages/werkzeug/datastructures.py", line 744, in __getitem__
        raise exceptions.BadRequestKeyError(key)
        werkzeug.exceptions.BadRequestKeyError: 400: Bad Request
    


Answer: The HTML code from the web site you want to log in is as follows:

    
    
    <section>
      <label for="LoginEmailOrNickname">Email Address or Nickname:</label>
      <input id="LoginEmailOrNickname" name="emailOrNickname" req="req" type="text" value="" maxlength="128"><span class="field-message" data-for="LoginEmailOrNickname"></span>
    </section>
    <section>
      <label for="login-password">Password:</label>
      <input id="login-password" name="password" req="req" type="password" value="" maxlength="64"><span class="field-message" data-for="login-password"></span>
      <a id="LoginForgottenPassword" href="/t-forgot-password.html">Forgot your password?</a>
    </section>
    

To put a value on the form fields you have to get the **name** attribute, not
the id.

This code should work:

    
    
    form.fields["emailOrNickname"].value = "an_email_address"
    form.fields["password"].value = "a_password"
    form.fields["rememberMe"].value = "true"
    

If you need to get the fields of the form you can print them:

    
    
    print form.fields
    

