View Source Pages.Html (Pages v0.2.0)

Some simple HTML query functions, originally intended for unit tests. Delegates the hard work to Floki.

The main query functions are:

  • all: returns all elements matching the selector
  • first: returns the first element that matches the selector
  • first!: like first but raises if more than one element matches the selector

Selectors can be a valid CSS selector string, or can be a keyword list. See Pages.Css for keyword list syntax.

The attr/2 function can be used to extract attr values, and the text/1 function can be used to extract the text of an HTML fragment.

usage

Usage

Example HTML:

<div class="profile-list" id="profiles">
  <div class="profile admin" id="alice">
    <div class="name">Alice</div>
  </div>
  <div class="profile" id="billy">
    <div class="name">Billy</div>
  </div>
</div>

Get Alice's name:

iex> Pages.Html.find!(html, "#alice .name") |> Pages.Html.text()
"Alice"

Get the names of all the profiles:

iex> Pages.Html.all(html, ".profile .name") |> Enum.map(&Pages.Html.text/1)
["Alice", "Billy"]

Link to this section Summary

Functions

Finds all elements in html that match selector.

Returns the value of an attr from the outermost element of an HTML node

Like find/2 but raises unless exactly one element is found

Finds the first element in html that matches selector

Returns the text value of html

Link to this section Types

@type attr() :: binary() | atom()
@type html() :: binary() | Pages.Driver.t() | Floki.html_tree()
@type selector() :: binary() | keyword() | atom()

Link to this section Functions

@spec all(html(), selector()) :: Floki.html_tree()

Finds all elements in html that match selector.

@spec attr(html(), attr()) :: [binary()]

Returns the value of an attr from the outermost element of an HTML node

@spec find!(html(), selector()) :: Floki.html_node()

Like find/2 but raises unless exactly one element is found

@spec find(html(), selector()) :: Floki.html_node()

Finds the first element in html that matches selector

Link to this function

form_fields(html, selector)

View Source
@spec form_fields(html(), selector()) :: map()
Link to this function

inspect_html(html, label \\ "INSPECTED HTML")

View Source
@spec inspect_html(html(), binary()) :: html()
@spec meta_tags(html()) :: [map()]
@spec normalize(html()) :: binary()
@spec parse(html()) :: Floki.html_tree()
@spec parse_doc(binary()) :: Floki.html_tree()
@spec pretty(html()) :: binary()
@spec text(html()) :: binary()

Returns the text value of html