Firoj112 commited on
Commit
8940906
·
verified ·
1 Parent(s): ca65077

Update tools/scroll_page.py

Browse files
Files changed (1) hide show
  1. 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, S
3
  from selenium.webdriver.common.by import By
4
 
5
- def scroll_page(driver, pixels=600, 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: 600)
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": 600, "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
 
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