MechanicalSoup Documentation, Release 1.3.0
Param soup: Page contents to display, supplied as a bs4 soup object. Defaults to the current
page of the StatefulBrowser instance.
links(url_regex=None, link_text=None, *args, **kwargs)
Return links in the page, as a list of bs4.element.Tag objects.
To return links matching specific criteria, specify url_regex to match the href -attribute, or
link_text to match the text-attribute of the Tag. All other arguments are forwarded to the .find_all()
method in BeautifulSoup.
list_links(*args, **kwargs)
Display the list of links in the current page. Arguments are forwarded to links().
new_control(type, name, value, **kwargs)
Call Form.new_control() on the currently selected form.
open(url, *args, **kwargs)
Open the URL and store the Browser’s state in this object. All arguments are forwarded to Browser.
get().
Returns Forwarded from Browser.get().
open_fake_page(page_text, url=None, soup_config=None)
Mock version of open().
Behave as if opening a page whose text is page_text, but do not perform any network access. If url is
set, pretend it is the page’s URL. Useful mainly for testing.
open_relative(url, *args, **kwargs)
Like open(), but url can be relative to the currently visited page.
page
Get the current page as a soup object.
refresh()
Reload the current page with the same request as originally done. Any change (select_form, or any value
filled-in in the form) made to the current page before refresh is discarded.
Raises ValueError – Raised if no refreshable page is loaded, e.g., when using the shallow
Browser wrapper functions.
Returns Response of the request.
select_form(selector=’form’, nr=0)
Select a form in the current page.
Parameters
• selector – CSS selector or a bs4.element.Tag object to identify the form to select. If
not specified, selector defaults to “form”, which is useful if, e.g., there is only one
form on the page. For selector syntax, see the .select() method in BeautifulSoup.
• nr – A zero-based index specifying which form among those that match selector will
be selected. Useful when one or more forms have the same attributes as the form you want
to select, and its position on the page is the only way to uniquely identify it. Default is the
first matching form (nr=0).
Returns The selected form as a soup object. It can also be retrieved later with the form attribute.
set_debug(debug)
Set the debug mode (off by default).
Set to True to enable debug mode. When active, some actions will launch a browser on the current page
on failure to let you inspect the page content.
3.1. StatefulBrowser 13