Spaces:
Paused
Paused
Update tools/search_item_ctrl_f.py
Browse files- tools/search_item_ctrl_f.py +10 -8
tools/search_item_ctrl_f.py
CHANGED
|
@@ -16,17 +16,19 @@ class SearchItemCtrlFTool(Tool):
|
|
| 16 |
}
|
| 17 |
output_type = "string"
|
| 18 |
|
| 19 |
-
def
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
|
|
| 16 |
}
|
| 17 |
output_type = "string"
|
| 18 |
|
| 19 |
+
def __init__(self, driver: Any = None, *args, **kwargs):
|
| 20 |
+
super().__init__(*args, **kwargs)
|
| 21 |
+
self.driver = driver
|
| 22 |
+
self.is_initialized = False
|
| 23 |
+
|
| 24 |
+
def forward(self, text: str, nth_result: int = 1) -> str:
|
| 25 |
+
if not self.driver:
|
| 26 |
raise ValueError("WebDriver instance is required.")
|
| 27 |
+
elements = self.driver.find_elements(By.XPATH, f"//*[contains(text(), '{text}')]")
|
| 28 |
if nth_result > len(elements):
|
| 29 |
raise Exception(f"Match n°{nth_result} not found (only {len(elements)} matches found)")
|
| 30 |
result = f"Found {len(elements)} matches for '{text}'."
|
| 31 |
elem = elements[nth_result - 1]
|
| 32 |
+
self.driver.execute_script("arguments[0].scrollIntoView(true);", elem)
|
| 33 |
result += f" Focused on element {nth_result} of {len(elements)}"
|
| 34 |
+
return result
|
|
|
|
|
|
|
|
|