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