Firoj112 commited on
Commit
8430937
·
verified ·
1 Parent(s): 0a124b0

Update tools/interact_element.py

Browse files
Files changed (1) hide show
  1. 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
- def interact_element(driver, selector=None, text=None, action="click", input_text=None, key=None):
6
- """
7
- Interact with a web element (click, fill text, press keys).
8
-
9
- Args:
10
- driver: Selenium WebDriver instance
11
- selector (str): CSS selector to target the element (optional)
12
- text (str): Text to locate the element (optional, for Helium)
13
- action (str): Action to perform ('click', 'fill', 'press') (default: 'click')
14
- input_text (str): Text to input if action='fill' (optional)
15
- key (str): Key to press if action='press' (e.g., 'ENTER') (optional)
16
-
17
- Returns:
18
- str: Result of the interaction
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
- if action == "click":
32
- click(element)
33
- return f"Clicked element with {selector or text}"
34
- elif action == "fill":
35
- if not input_text:
36
- return "input_text required for fill action"
37
- write(input_text, into=element)
38
- return f"Filled {input_text} into element with {selector or text}"
39
- elif action == "press":
40
- if not key:
41
- return "key required for press action"
42
- key_map = {"ENTER": Keys.ENTER, "TAB": Keys.TAB, "RETURN": Keys.RETURN}
43
- if key.upper() in key_map:
44
- element.send_keys(key_map[key.upper()])
45
  else:
46
- press(key)
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
- # Register the tool
54
- interact_element_tool = Tool(
55
- name="interact_element",
56
- description="Interacts with a web element (click, fill text, press keys).",
57
- inputs={
58
- "selector": {
59
- "type": "str",
60
- "default": None,
61
- "description": "CSS selector to target element"
62
- },
63
- "text": {
64
- "type": "str",
65
- "default": None,
66
- "description": "Text to locate element"
67
- },
68
- "action": {
69
- "type": "str",
70
- "default": "click",
71
- "description": "Action: 'click', 'fill', or 'press'"
72
- },
73
- "input_text": {
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)}"