Spaces:
Running
Running
Update tools/scroll_page.py
Browse files- tools/scroll_page.py +40 -20
tools/scroll_page.py
CHANGED
@@ -1,37 +1,57 @@
|
|
1 |
from smolagents.tools import Tool
|
2 |
-
from helium import scroll_down,
|
3 |
from selenium.webdriver.common.by import By
|
4 |
|
5 |
-
def scroll_page(driver,
|
6 |
"""
|
7 |
-
Scroll the page
|
8 |
|
9 |
Args:
|
10 |
driver: Selenium WebDriver instance
|
11 |
-
|
12 |
-
|
|
|
13 |
|
14 |
Returns:
|
15 |
-
str:
|
16 |
"""
|
17 |
-
|
18 |
-
|
19 |
-
element =
|
20 |
-
driver.execute_script("arguments[0].scrollIntoView(true);", element
|
21 |
-
return f"Scrolled to element with selector
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
# Register the tool
|
29 |
-
|
30 |
name="scroll_page",
|
31 |
-
description="Scrolls the page
|
32 |
inputs={
|
33 |
-
"
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
},
|
36 |
output_type="str",
|
37 |
function=scroll_page
|
|
|
1 |
from smolagents.tools import Tool
|
2 |
+
from helium import scroll_down, scroll_up, get_driver
|
3 |
from selenium.webdriver.common.by import By
|
4 |
|
5 |
+
def scroll_page(driver, selector=None, num_pixels=1200, direction="down"):
|
6 |
"""
|
7 |
+
Scroll the page to a specific element or by a number of pixels.
|
8 |
|
9 |
Args:
|
10 |
driver: Selenium WebDriver instance
|
11 |
+
selector (str): CSS selector to scroll to (optional)
|
12 |
+
num_pixels (int): Number of pixels to scroll (default: 1200)
|
13 |
+
direction (str): Scroll direction ('down' or 'up') (default: 'down')
|
14 |
|
15 |
Returns:
|
16 |
+
str: Result of the scroll action
|
17 |
"""
|
18 |
+
try:
|
19 |
+
if selector:
|
20 |
+
element = driver.find_element(By.CSS_SELECTOR, selector)
|
21 |
+
driver.execute_script("arguments[0].scrollIntoView(true);", element)
|
22 |
+
return f"Scrolled to element with selector {selector}"
|
23 |
+
else:
|
24 |
+
if direction == "down":
|
25 |
+
scroll_down(num_pixels)
|
26 |
+
return f"Scrolled down {num_pixels} pixels"
|
27 |
+
elif direction == "up":
|
28 |
+
scroll_up(num_pixels)
|
29 |
+
return f"Scrolled up {num_pixels} pixels"
|
30 |
+
else:
|
31 |
+
return f"Invalid direction: {direction}"
|
32 |
+
except Exception as e:
|
33 |
+
return f"Failed to scroll: {str(e)}"
|
34 |
|
35 |
# Register the tool
|
36 |
+
scroll_page_tool = Tool(
|
37 |
name="scroll_page",
|
38 |
+
description="Scrolls the page to a specific element or by a number of pixels.",
|
39 |
inputs={
|
40 |
+
"selector": {
|
41 |
+
"type": "str",
|
42 |
+
"default": None,
|
43 |
+
"description": "CSS selector to scroll to"
|
44 |
+
},
|
45 |
+
"num_pixels": {
|
46 |
+
"type": "int",
|
47 |
+
"default": 1200,
|
48 |
+
"description": "Number of pixels to scroll"
|
49 |
+
},
|
50 |
+
"direction": {
|
51 |
+
"type": "str",
|
52 |
+
"default": "down",
|
53 |
+
"description": "Scroll direction: 'down' or 'up'"
|
54 |
+
}
|
55 |
},
|
56 |
output_type="str",
|
57 |
function=scroll_page
|