File size: 1,960 Bytes
7110c12
8940906
7110c12
 
8940906
7110c12
8940906
7110c12
 
 
8940906
 
 
7110c12
 
8940906
7110c12
8940906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7110c12
 
8940906
7110c12
8940906
7110c12
8940906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7110c12
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from smolagents.tools import Tool
from helium import scroll_down, scroll_up, get_driver
from selenium.webdriver.common.by import By

def scroll_page(driver, selector=None, num_pixels=1200, direction="down"):
    """
    Scroll the page to a specific element or by a number of pixels.
    
    Args:
        driver: Selenium WebDriver instance
        selector (str): CSS selector to scroll to (optional)
        num_pixels (int): Number of pixels to scroll (default: 1200)
        direction (str): Scroll direction ('down' or 'up') (default: 'down')
    
    Returns:
        str: Result of the scroll action
    """
    try:
        if selector:
            element = driver.find_element(By.CSS_SELECTOR, selector)
            driver.execute_script("arguments[0].scrollIntoView(true);", element)
            return f"Scrolled to element with selector {selector}"
        else:
            if direction == "down":
                scroll_down(num_pixels)
                return f"Scrolled down {num_pixels} pixels"
            elif direction == "up":
                scroll_up(num_pixels)
                return f"Scrolled up {num_pixels} pixels"
            else:
                return f"Invalid direction: {direction}"
    except Exception as e:
        return f"Failed to scroll: {str(e)}"

# Register the tool
scroll_page_tool = Tool(
    name="scroll_page",
    description="Scrolls the page to a specific element or by a number of pixels.",
    inputs={
        "selector": {
            "type": "str",
            "default": None,
            "description": "CSS selector to scroll to"
        },
        "num_pixels": {
            "type": "int",
            "default": 1200,
            "description": "Number of pixels to scroll"
        },
        "direction": {
            "type": "str",
            "default": "down",
            "description": "Scroll direction: 'down' or 'up'"
        }
    },
    output_type="str",
    function=scroll_page
)