Firoj112 commited on
Commit
abc76d6
·
verified ·
1 Parent(s): 5c0ab7d

Update tools/search_item_ctrl_f.py

Browse files
Files changed (1) hide show
  1. tools/search_item_ctrl_f.py +31 -26
tools/search_item_ctrl_f.py CHANGED
@@ -1,27 +1,32 @@
1
- from typing import Any
2
- from smolagents.tools import Tool
3
- from selenium.webdriver.common.by import By
4
-
5
- class SearchItemCtrlFTool(Tool):
6
- name = "search_item_ctrl_f"
7
- description = "Searches for text on the current page via Ctrl + F and jumps to the nth occurrence."
8
- inputs = {
9
- 'text': {'type': 'str', 'description': 'The text to search for'},
10
- 'nth_result': {'type': 'int', 'description': 'Which occurrence to jump to (default: 1)'}
11
- }
12
- output_type = "str"
13
-
14
- def forward(self, text: str, nth_result: int = 1, driver: Any = None) -> str:
15
- if not driver:
16
- raise ValueError("WebDriver instance is required.")
17
- elements = driver.find_elements(By.XPATH, f"//*[contains(text(), '{text}')]")
18
- if nth_result > len(elements):
19
- raise Exception(f"Match n°{nth_result} not found (only {len(elements)} matches found)")
20
- result = f"Found {len(elements)} matches for '{text}'."
21
- elem = elements[nth_result - 1]
22
- driver.execute_script("arguments[0].scrollIntoView(true);", elem)
23
- result += f" Focused on element {nth_result} of {len(elements)}"
24
- return result
25
-
26
- def __init__(self, *args, **kwargs):
 
 
 
 
 
27
  self.is_initialized = False
 
1
+ from typing import Any
2
+ from smolagents.tools import Tool
3
+ from selenium.webdriver.common.by import By
4
+
5
+ class SearchItemCtrlFTool(Tool):
6
+ name = "search_item_ctrl_f"
7
+ description = """
8
+ Searches for text on the current page via Ctrl + F and jumps to the nth occurrence.
9
+ Args:
10
+ text: The text to search for
11
+ nth_result: Which occurrence to jump to (default: 1)
12
+ """
13
+ inputs = {
14
+ 'text': {'type': 'str', 'description': 'The text to search for'},
15
+ 'nth_result': {'type': 'int', 'description': 'Which occurrence to jump to (default: 1)'}
16
+ }
17
+ output_type = "str"
18
+
19
+ def forward(self, text: str, nth_result: int = 1, driver: Any = None) -> str:
20
+ if not driver:
21
+ raise ValueError("WebDriver instance is required.")
22
+ elements = driver.find_elements(By.XPATH, f"//*[contains(text(), '{text}')]")
23
+ if nth_result > len(elements):
24
+ raise Exception(f"Match n°{nth_result} not found (only {len(elements)} matches found)")
25
+ result = f"Found {len(elements)} matches for '{text}'."
26
+ elem = elements[nth_result - 1]
27
+ driver.execute_script("arguments[0].scrollIntoView(true);", elem)
28
+ result += f" Focused on element {nth_result} of {len(elements)}"
29
+ return result
30
+
31
+ def __init__(self, *args, **kwargs):
32
  self.is_initialized = False