Spaces:
Running
Running
Upload 4 files
Browse files- tools/close_popups.py +19 -0
- tools/final_answer.py +14 -0
- tools/go_back.py +16 -0
- tools/search_item_ctrl_f.py +27 -0
tools/close_popups.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any
|
2 |
+
from smolagents.tools import Tool
|
3 |
+
from selenium.webdriver.common.keys import Keys
|
4 |
+
from selenium.webdriver import ActionChains
|
5 |
+
|
6 |
+
class ClosePopupsTool(Tool):
|
7 |
+
name = "close_popups"
|
8 |
+
description = "Closes any visible modal or pop-up on the page by pressing ESCAPE. Does not work on cookie consent banners."
|
9 |
+
inputs = {}
|
10 |
+
output_type = "str"
|
11 |
+
|
12 |
+
def forward(self, driver: Any = None) -> str:
|
13 |
+
if not driver:
|
14 |
+
raise ValueError("WebDriver instance is required.")
|
15 |
+
ActionChains(driver).send_keys(Keys.ESCAPE).perform()
|
16 |
+
return "Pop-up closed."
|
17 |
+
|
18 |
+
def __init__(self, *args, **kwargs):
|
19 |
+
self.is_initialized = False
|
tools/final_answer.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any
|
2 |
+
from smolagents.tools import Tool
|
3 |
+
|
4 |
+
class FinalAnswerTool(Tool):
|
5 |
+
name = "final_answer"
|
6 |
+
description = "Provides a final answer to the given problem."
|
7 |
+
inputs = {'answer': {'type': 'any', 'description': 'The final answer to the problem'}}
|
8 |
+
output_type = "any"
|
9 |
+
|
10 |
+
def forward(self, answer: Any) -> Any:
|
11 |
+
return answer
|
12 |
+
|
13 |
+
def __init__(self, *args, **kwargs):
|
14 |
+
self.is_initialized = False
|
tools/go_back.py
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any
|
2 |
+
from smolagents.tools import Tool
|
3 |
+
|
4 |
+
class GoBackTool(Tool):
|
5 |
+
name = "go_back"
|
6 |
+
description = "Goes back to the previous page."
|
7 |
+
inputs = {}
|
8 |
+
output_type = "None"
|
9 |
+
|
10 |
+
def forward(self, driver: Any = None) -> None:
|
11 |
+
if not driver:
|
12 |
+
raise ValueError("WebDriver instance is required.")
|
13 |
+
driver.back()
|
14 |
+
|
15 |
+
def __init__(self, *args, **kwargs):
|
16 |
+
self.is_initialized = False
|
tools/search_item_ctrl_f.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|