Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
a download of a file. All other arguments are forwarded to
:func:`Browser.submit`.
"""
self.form.choose_submit(btnName)
referer = self.url
if referer is not None:
if 'headers' in kwargs:
kwargs['headers']['Referer'] = referer
else:
kwargs['headers'] = {'Referer': referer}
resp = self.submit(self.__state.form, url=self.__state.url,
*args, **kwargs)
if update_state:
self.__state = _BrowserState(page=resp.soup, url=resp.url,
request=resp.request)
return resp
Any change (`select_form`, or any value filled-in in the form) made to
the current page before refresh is discarded.
:raise ValueError: Raised if no refreshable page is loaded, e.g., when
using the shallow ``Browser`` wrapper functions.
:return: Response of the request."""
old_request = self.__state.request
if old_request is None:
raise ValueError('The current page is not refreshable. Either no '
'page is opened or low-level browser methods '
'were used to do so')
resp = self.session.send(old_request)
Browser.add_soup(resp, self.soup_config)
self.__state = _BrowserState(page=resp.soup, url=resp.url,
request=resp.request)
return resp
def open(self, url, *args, **kwargs):
"""Open the URL and store the Browser's state in this object.
All arguments are forwarded to :func:`Browser.get`.
:return: Forwarded from :func:`Browser.get`.
"""
if self.__verbose == 1:
sys.stdout.write('.')
sys.stdout.flush()
elif self.__verbose >= 2:
print(url)
resp = self.get(url, *args, **kwargs)
self.__state = _BrowserState(page=resp.soup, url=resp.url,
request=resp.request)
return resp
def _update_state(self, response):
"""Dry up the setters from http post and get methods."""
self._set_raw_content(response.content)
self._set_status(response.status_code)
self._add_soup(response, self.soup_config)
self.__state = _BrowserState(page=response.soup,
url=response.url,
request=response.request)
def open_fake_page(self, page_text, url=None, soup_config=None):
"""Mock version of :func:`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.
"""
soup_config = soup_config or self.soup_config
self.__state = _BrowserState(
page=bs4.BeautifulSoup(page_text, **soup_config),
url=url)
def __init__(self, *args, **kwargs):
self._set_raw_content(content=b'')
self._set_status(status='')
self.__state = _BrowserState()
self._test_true = False
self.timeout_exception = False
self.flags = kwargs.pop('flags', None)
self.priority = kwargs.pop('priority', 0)
if kwargs.pop('meta', None):
self._meta = dict(kwargs.pop('meta'))
else:
self._meta = None
self.callback = None
self.errback = None
super().__init__(*args, **kwargs)
def open_fake_page(self, page_text, status_code=None, url=None, soup_config=None):
"""Mock version of :func:`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.
"""
soup_config = soup_config or self.soup_config
self._test_true = True
self._set_raw_content(page_text.encode())
self._set_status(status_code)
self.__state = _BrowserState(
page=bs4.BeautifulSoup(page_text, **soup_config),
url=url)