File: browser.py

package info (click to toggle)
python-web-poet 0.23.2-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 908 kB
  • sloc: python: 6,112; makefile: 19
file content (46 lines) | stat: -rw-r--r-- 1,370 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import attrs

from web_poet.mixins import SelectableMixin, UrlShortcutsMixin

from .url import ResponseUrl


class BrowserHtml(SelectableMixin, str):  # noqa: SLOT000
    """HTML returned by a web browser,
    i.e. snapshot of the DOM tree in HTML format.
    """

    def _selector_input(self) -> str:
        return self


@attrs.define(auto_attribs=False, slots=False, eq=False)
class BrowserResponse(SelectableMixin, UrlShortcutsMixin):
    """Browser response: url, HTML and status code.

    ``url`` should be browser's window.location, not a URL of the request,
    if possible.

    ``html`` contains the HTML returned by the browser,
    i.e. a snapshot of DOM tree in HTML format.

    The following are optional since it would depend on the source of the
    ``BrowserResponse`` if these are available or not:

    ``status`` should represent the int status code of the HTTP response.
    """

    url: ResponseUrl = attrs.field(converter=ResponseUrl)
    html: BrowserHtml = attrs.field(converter=BrowserHtml)
    status: int | None = attrs.field(default=None, kw_only=True)

    @property
    def text(self) -> str:
        """HTML returned by the browser, identical to ``self.html``.

        Provided for compatibility with :class:`~.HttpResponse`.
        """
        return str(self.html)

    def _selector_input(self) -> str:
        return self.html