Module: Ferrum::Page::DOM

Included in:
Ferrum::Page
Defined in:
lib/ferrum/page/dom.rb

Instance Method Summary collapse

Instance Method Details

#at_css(selector, within: nil) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/ferrum/page/dom.rb', line 72

def at_css(selector, within: nil)
  node_id = within&.node_id || @document_id

  id = command("DOM.querySelector",
               nodeId: node_id,
               selector: selector)["nodeId"]
  build_node(id)
end

#at_xpath(selector, within: nil) ⇒ Object



35
36
37
# File 'lib/ferrum/page/dom.rb', line 35

def at_xpath(selector, within: nil)
  xpath(selector, within: within).first
end

#bodyObject



31
32
33
# File 'lib/ferrum/page/dom.rb', line 31

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

#css(selector, within: nil) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/ferrum/page/dom.rb', line 63

def css(selector, within: nil)
  node_id = within&.node_id || @document_id

  ids = command("DOM.querySelectorAll",
                nodeId: node_id,
                selector: selector)["nodeIds"]
  ids.map { |id| build_node(id) }.compact
end

#current_urlObject



23
24
25
# File 'lib/ferrum/page/dom.rb', line 23

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

#titleObject



27
28
29
# File 'lib/ferrum/page/dom.rb', line 27

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

#xpath(selector, within: nil) ⇒ Object

FIXME: Check within



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ferrum/page/dom.rb', line 40

def xpath(selector, within: nil)
  evaluate_async(%(
    try {
      let selector = arguments[0];
      let within = arguments[1] || document;
      let results = [];

      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));
      }

      arguments[2](results);
    } catch (error) {
      // DOMException.INVALID_EXPRESSION_ERR is undefined, using pure code
      if (error.code == DOMException.SYNTAX_ERR || error.code == 51) {
        throw "Invalid Selector";
      } else {
        throw error;
      }
    }), timeout, selector, within)
end