Mechanize v0.1.0 Mechanize.Page.Element View Source

The HMTL element.

This module provides functions to manipulate and extract information from HMTL element nodes.

Public fields

  • name - name of the HTML tag.
  • attrs - List of the element attributes.
  • text - Inner text of the element.
  • page - page where this element was extracted.

Private fields

Fields reserved for interal use of the library and it is not recommended to access it directly.

  • parser_node - internal data used by the parser.
  • parser - parser used to parse this element.

Link to this section Summary

Types

t()

The HTML Element struct.

Functions

Returns attribute value of a given element.

Returns true if attribute is present, otherwise false.

Returns a list containing all attributes from a HMTL element and its values.

Returns the page where this element was extracted.

Returns the HTML tag name of the element.

Returns the inner text from the HTML element.

Link to this section Types

Specs

attributes() :: [{String.t(), String.t()}]

Specs

t() :: %Mechanize.Page.Element{
  attrs: attributes(),
  name: String.t(),
  page: Page.t(),
  parser: module(),
  parser_node: Mechanize.HTMLParser.parser_node(),
  text: String.t()
}

The HTML Element struct.

Link to this section Functions

Link to this function

attr(element, attr_name, opts \\ [])

View Source

Returns attribute value of a given element.

This functions accepts opts:

  • default - Returns a default value in case of an attribute is not present. Default: nil.
  • normalize - Returns the value of the attribute downcased and without dangling spaces.

Examples

iex> Element.attr(element, :href)
"/home"
Link to this function

attr_present?(element, attr_name)

View Source

Specs

attr_present?(any(), atom()) :: boolean()

Returns true if attribute is present, otherwise false.

Element may be any struct implementing Mechanize.Page.Elementable protocol.

Example

iex> Element.attr_present?(checkbox, :selected)
true

Specs

attrs(any()) :: attributes()

Returns a list containing all attributes from a HMTL element and its values.

In case element doest have any attributes, an empty list is returned.

Element may be any struct implementing Mechanize.Page.Elementable protocol.

Example

iex> Element.attrs(element)
[{"href", "/home"}, {"rel", "nofollow"}]

Specs

get_page(any()) :: Page.t()

Returns the page where this element was extracted.

Element may be any struct implementing Mechanize.Page.Elementable protocol.

Specs

name(any()) :: String.t()

Returns the HTML tag name of the element.

For instance, it will return "a" and "area" for hyperlinks, "img" for images, etc.

Element may be any struct implementing Mechanize.Page.Elementable protocol.

Example

iex> Element.name(image)
"img"

Specs

text(any()) :: String.t()

Returns the inner text from the HTML element.

Text from elements without inner text are extracted as empty strings by the parser. It means that this function will return an empty string in case of the element does not have inner text.

Element may be any struct implementing Mechanize.Page.Elementable protocol.