Method: Ferrum::Frame::DOM#xpath

Defined in:
lib/ferrum/frame/dom.rb

#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.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ferrum/frame/dom.rb', line 130

def xpath(selector, within: nil)
  expr = "    function(selector, within) {\n      let results = [];\n      within ||= document\n\n      let xpath = document.evaluate(selector, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);\n      for (let i = 0; i < xpath.snapshotLength; i++) {\n        results.push(xpath.snapshotItem(i));\n      }\n\n      return results;\n    }\n  JS\n\n  evaluate_func(expr, selector, within)\nend\n"