Module: Ferrum::Frame::DOM

Included in:
Ferrum::Frame
Defined in:
lib/ferrum/frame/dom.rb

Constant Summary collapse

SCRIPT_SRC_TAG =
<<~JS
  const script = document.createElement("script");
  script.src = arguments[0];
  script.type = arguments[1];
  script.onload = arguments[2];
  document.head.appendChild(script);
JS
SCRIPT_TEXT_TAG =
<<~JS
  const script = document.createElement("script");
  script.text = arguments[0];
  script.type = arguments[1];
  document.head.appendChild(script);
  arguments[2]();
JS
STYLE_TAG =
<<~JS
  const style = document.createElement("style");
  style.type = "text/css";
  style.appendChild(document.createTextNode(arguments[0]));
  document.head.appendChild(style);
  arguments[1]();
JS
<<~JS
  const link = document.createElement("link");
  link.rel = "stylesheet";
  link.href = arguments[0];
  link.onload = arguments[1];
  document.head.appendChild(link);
JS

Instance Method Summary collapse

Instance Method Details

#add_script_tag(url: nil, path: nil, content: nil, type: "text/javascript") ⇒ Object

Adds a ‘<script>` tag to the document.

Examples:

browser.add_script_tag(url: "http://example.com/stylesheet.css") # => true

Parameters:

  • url (String, nil) (defaults to: nil)
  • path (String, nil) (defaults to: nil)
  • content (String, nil) (defaults to: nil)
  • type (String) (defaults to: "text/javascript")


227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/ferrum/frame/dom.rb', line 227

def add_script_tag(url: nil, path: nil, content: nil, type: "text/javascript")
  expr, *args = if url
                  [SCRIPT_SRC_TAG, url, type]
                elsif path || content
                  if path
                    content = File.read(path)
                    content += "\n//# sourceURL=#{path}"
                  end
                  [SCRIPT_TEXT_TAG, content, type]
                end

  evaluate_async(expr, @page.timeout, *args)
end

#add_style_tag(url: nil, path: nil, content: nil) ⇒ Object

Adds a ‘<style>` tag to the document.

Examples:

browser.add_style_tag(content: "h1 { font-size: 40px; }") # => true

Parameters:

  • url (String, nil) (defaults to: nil)
  • path (String, nil) (defaults to: nil)
  • content (String, nil) (defaults to: nil)


253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/ferrum/frame/dom.rb', line 253

def add_style_tag(url: nil, path: nil, content: nil)
  expr, *args = if url
                  [LINK_TAG, url]
                elsif path || content
                  if path
                    content = File.read(path)
                    content += "\n//# sourceURL=#{path}"
                  end
                  [STYLE_TAG, content]
                end

  evaluate_async(expr, @page.timeout, *args)
end

#at_css(selector, within: nil) ⇒ Node?

Finds a node by using a CSS path selector.

Examples:

browser.go_to("https://github.com/")
browser.at_css("a[aria-label='Issues you created']") # => Node

Parameters:

  • selector (String)

    The CSS path selector.

  • within (Node, nil) (defaults to: nil)

    The parent node to search within.

Returns:

  • (Node, nil)

    The matching node.



202
203
204
205
206
207
208
209
210
211
# File 'lib/ferrum/frame/dom.rb', line 202

def at_css(selector, within: nil)
  expr = <<~JS
    function(selector, within) {
      within ||= document
      return within.querySelector(selector);
    }
  JS

  evaluate_func(expr, selector, within)
end

#at_xpath(selector, within: nil) ⇒ Node?

Finds a node by using a XPath selector.

Examples:

browser.go_to("https://github.com/")
browser.at_xpath("//a[@aria-label='Issues you created']") # => Node

Parameters:

  • selector (String)

    The XPath selector.

  • within (Node, nil) (defaults to: nil)

    The parent node to search within.

Returns:

  • (Node, nil)

    The matching node.



148
149
150
151
152
153
154
155
156
157
# File 'lib/ferrum/frame/dom.rb', line 148

def at_xpath(selector, within: nil)
  expr = <<~JS
    function(selector, within) {
      within ||= document
      let xpath = document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
      return xpath.snapshotItem(0);
    }
  JS
  evaluate_func(expr, selector, within)
end

#bodyString

Returns current page’s html.

Examples:

browser.go_to("https://google.com/")
browser.body # => '<html itemscope="" itemtype="http://schema.org/WebPage" lang="ru"><head>...

Returns:

  • (String)

    The HTML source of the current page.



94
95
96
# File 'lib/ferrum/frame/dom.rb', line 94

def body
  evaluate("document.documentElement.outerHTML")
end

#css(selector, within: nil) ⇒ Array<Node>

Finds nodes by using a CSS path selector.

Examples:

browser.go_to("https://github.com/")
browser.css("a[aria-label='Issues you created']") # => [Node]

Parameters:

  • selector (String)

    The CSS path selector.

  • within (Node, nil) (defaults to: nil)

    The parent node to search within.

Returns:

  • (Array<Node>)

    The matching nodes.



175
176
177
178
179
180
181
182
183
184
# File 'lib/ferrum/frame/dom.rb', line 175

def css(selector, within: nil)
  expr = <<~JS
    function(selector, within) {
      within ||= document
      return Array.from(within.querySelectorAll(selector));
    }
  JS

  evaluate_func(expr, selector, within)
end

#current_titleString

Returns current top window title.

Examples:

browser.go_to("https://google.com/")
browser.current_title # => "Google"

Returns:

  • (String)

    The window’s current title.



76
77
78
# File 'lib/ferrum/frame/dom.rb', line 76

def current_title
  evaluate("window.top.document.title")
end

#current_urlString

Returns current top window ‘location href`.

Examples:

browser.go_to("https://google.com/")
browser.current_url # => "https://www.google.com/"

Returns:

  • (String)

    The window’s current URL.



62
63
64
# File 'lib/ferrum/frame/dom.rb', line 62

def current_url
  evaluate("window.top.location.href")
end

#doctypeObject



80
81
82
# File 'lib/ferrum/frame/dom.rb', line 80

def doctype
  evaluate("document.doctype && new XMLSerializer().serializeToString(document.doctype)")
end

#xpath(selector, within: nil) ⇒ Array<Node>

Finds nodes by using a XPath selector.

Examples:

browser.go_to("https://github.com/")
browser.xpath("//a[@aria-label='Issues you created']") # => [Node]

Parameters:

  • selector (String)

    The XPath selector.

  • within (Node, nil) (defaults to: nil)

    The parent node to search within.

Returns:

  • (Array<Node>)

    The matching nodes.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ferrum/frame/dom.rb', line 114

def xpath(selector, within: nil)
  expr = <<~JS
    function(selector, within) {
      let results = [];
      within ||= document

      let xpath = document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
      for (let i = 0; i < xpath.snapshotLength; i++) {
        results.push(xpath.snapshotItem(i));
      }

      return results;
    }
  JS

  evaluate_func(expr, selector, within)
end