Spaces:
Running
Running
Update tools/search_item_ctrl_f.py
Browse files- 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 = "
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
driver.
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
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
|