Selecting a form by name, not id using mechanicalsoup

6.2k Views Asked by At

Even after reading some docs, I am still having trouble understanding what mechanical soup's stateful browser's select_form() does. Does it take an ID or does it take a name? The form that I am using does not have an id, but a name. Will using select_form() take an ID or a name? The form I am trying to use looks like this (no ID).

<div class="fsLoginForms">
    <div id="fsLoginForm" class="fsLogin" >
        <form name="userlogin" action="" method="post">

            <p class="fsLoginTitle">Site Login</p>
            <p class="fsLoginMessage">Please provide your username and password to log in:</p>

            <label for="username">username</label><input type="text" class="fsLoginUser" name="username" placeholder="Username" id="username" maxlength="50" value=""><br>
            <label for="password">password</label><input type="password" class="fsLoginPass" name="password" placeholder="Password" id="password" maxlength="50" value="" ><br>

        </form>
    </div>
</div>

I've tried using

stateful_browser.select_form("#userlogin")

and

stateful_browser.select_form("userlogin")

It keeps throwing this

Traceback (most recent call last):
  File "loginScrape.py", line 25, in <module>
    browser.select_form('#userlogin')
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mechanicalsoup/stateful_browser.py", line 175, in select_form
    raise LinkNotFoundError()
mechanicalsoup.utils.LinkNotFoundError

UPDATE: I tried authenticating into that website with incorrect credentials and I got the same error. I'm not too sure what this means.

1

There are 1 best solutions below

1
On BEST ANSWER

According to the docs at http://mechanicalsoup.readthedocs.io/en/stable/mechanicalsoup.html?highlight=select_form#mechanicalsoup.StatefulBrowser.select_form

you should be able to use no arguments to get a form, if it is the only form on the page.

Alternatively, use CSS selectors:

stateful_browser.select_form('form[name="userlogin"]')

Details of CSS selectors at: https://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors