Class: HeadlessBrowserTool::Tools::FindAllTool

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/headless_browser_tool/tools/find_all_tool.rb

Instance Method Summary collapse

Instance Method Details

#execute(selector:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/headless_browser_tool/tools/find_all_tool.rb', line 15

def execute(selector:)
  elements = browser.find_all(selector)

  {
    selector: selector,
    count: elements.size,
    elements: elements.map.with_index do |element, index|
      result = {
        index: index,
        selector: "#{selector}:nth-of-type(#{index + 1})",
        tag_name: element[:tag_name],
        text: element[:text].strip,
        visible: element[:visible]
      }

      # Include attributes if present
      result[:attributes] = element[:attributes] unless element[:attributes].empty?

      # Include value for form elements
      result[:value] = element[:value] if element[:value] && !element[:value].empty?

      result
    end
  }
end