Firoj112 commited on
Commit
7110c12
·
verified ·
1 Parent(s): f82a8cd

Create scroll_page.py

Browse files
Files changed (1) hide show
  1. tools/scroll_page.py +38 -0
tools/scroll_page.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents.tools import Tool
2
+ from helium import scroll_down, S
3
+ from selenium.webdriver.common.by import By
4
+
5
+ def scroll_page(driver, pixels=1000, selector=None):
6
+ """
7
+ Scroll the page by a specified number of pixels or to a specific element.
8
+
9
+ Args:
10
+ driver: Selenium WebDriver instance
11
+ pixels (int): Number of pixels to scroll down (default: 1000)
12
+ selector (str): Optional CSS selector to scroll to a specific element
13
+
14
+ Returns:
15
+ str: Observation message indicating scroll action
16
+ """
17
+ if selector:
18
+ try:
19
+ element = S(selector)
20
+ driver.execute_script("arguments[0].scrollIntoView(true);", element.web_element)
21
+ return f"Scrolled to element with selector: {selector}"
22
+ except Exception as e:
23
+ return f"Failed to scroll to selector {selector}: {str(e)}"
24
+ else:
25
+ driver.execute_script(f"window.scrollBy(0, {pixels});")
26
+ return f"Scrolled down by {pixels} pixels"
27
+
28
+ # Register the tool
29
+ tool = Tool(
30
+ name="scroll_page",
31
+ description="Scrolls the page by a specified number of pixels or to an element with a given CSS selector.",
32
+ inputs={
33
+ "pixels": {"type": "int", "default": 1000, "description": "Number of pixels to scroll down"},
34
+ "selector": {"type": "str", "default": None, "description": "CSS selector of the element to scroll to"}
35
+ },
36
+ output_type="str",
37
+ function=scroll_page
38
+ )