Spaces:
Running
Running
Update tools/interact_element.py
Browse files- tools/interact_element.py +50 -79
tools/interact_element.py
CHANGED
@@ -2,85 +2,56 @@ from smolagents.tools import Tool
|
|
2 |
from helium import write, press, click, Text, Link, S
|
3 |
from selenium.webdriver.common.keys import Keys
|
4 |
|
5 |
-
|
6 |
-
""
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
"""
|
20 |
-
try:
|
21 |
-
if selector:
|
22 |
-
element = driver.find_element(By.CSS_SELECTOR, selector)
|
23 |
-
elif text:
|
24 |
-
if action == "click" and Link(text).exists():
|
25 |
-
element = Link(text).web_element
|
26 |
-
else:
|
27 |
-
element = Text(text).web_element
|
28 |
-
else:
|
29 |
-
return "Must provide selector or text"
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
else:
|
46 |
-
|
47 |
-
return f"Pressed {key} on element with {selector or text}"
|
48 |
-
else:
|
49 |
-
return f"Unknown action: {action}"
|
50 |
-
except Exception as e:
|
51 |
-
return f"Failed to interact with element {selector or text}: {str(e)}"
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
"type": "str",
|
75 |
-
"default": None,
|
76 |
-
"description": "Text to input for fill action"
|
77 |
-
},
|
78 |
-
"key": {
|
79 |
-
"type": "str",
|
80 |
-
"default": None,
|
81 |
-
"description": "Key to press (e.g., 'ENTER')"
|
82 |
-
}
|
83 |
-
},
|
84 |
-
output_type="str",
|
85 |
-
function=interact_element
|
86 |
-
)
|
|
|
2 |
from helium import write, press, click, Text, Link, S
|
3 |
from selenium.webdriver.common.keys import Keys
|
4 |
|
5 |
+
class InteractElementTool(Tool):
|
6 |
+
name = "interact_element"
|
7 |
+
description = "Interacts with a web element (click, fill text, press keys)."
|
8 |
+
inputs = {
|
9 |
+
"selector": {"type": "str", "default": None, "description": "CSS selector to target element"},
|
10 |
+
"text": {"type": "str", "default": None, "description": "Text to locate element"},
|
11 |
+
"action": {"type": "str", "default": "click", "description": "Action: 'click', 'fill', or 'press'"},
|
12 |
+
"input_text": {"type": "str", "default": None, "description": "Text to input for fill action"},
|
13 |
+
"key": {"type": "str", "default": None, "description": "Key to press (e.g., 'ENTER')"}
|
14 |
+
}
|
15 |
+
output_type = "str"
|
16 |
+
|
17 |
+
def __init__(self, driver):
|
18 |
+
self.driver = driver
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
def forward(self, **kwargs):
|
21 |
+
selector = kwargs.get("selector")
|
22 |
+
text = kwargs.get("text")
|
23 |
+
action = kwargs.get("action", "click")
|
24 |
+
input_text = kwargs.get("input_text")
|
25 |
+
key = kwargs.get("key")
|
26 |
+
try:
|
27 |
+
if selector:
|
28 |
+
element = self.driver.find_element(By.CSS_SELECTOR, selector)
|
29 |
+
elif text:
|
30 |
+
if action == "click" and Link(text).exists():
|
31 |
+
element = Link(text).web_element
|
32 |
+
else:
|
33 |
+
element = Text(text).web_element
|
34 |
else:
|
35 |
+
return "Must provide selector or text"
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
if action == "click":
|
38 |
+
click(element)
|
39 |
+
return f"Clicked element with {selector or text}"
|
40 |
+
elif action == "fill":
|
41 |
+
if not input_text:
|
42 |
+
return "input_text required for fill action"
|
43 |
+
write(input_text, into=element)
|
44 |
+
return f"Filled {input_text} into element with {selector or text}"
|
45 |
+
elif action == "press":
|
46 |
+
if not key:
|
47 |
+
return "key required for press action"
|
48 |
+
key_map = {"ENTER": Keys.ENTER, "TAB": Keys.TAB, "RETURN": Keys.RETURN}
|
49 |
+
if key.upper() in key_map:
|
50 |
+
element.send_keys(key_map[key.upper()])
|
51 |
+
else:
|
52 |
+
press(key)
|
53 |
+
return f"Pressed {key} on element with {selector or text}"
|
54 |
+
else:
|
55 |
+
return f"Unknown action: {action}"
|
56 |
+
except Exception as e:
|
57 |
+
return f"Failed to interact with element {selector or text}: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|