forked from TransformerOptimus/SuperAGI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added web interactor tool functionality (TransformerOptimus#350)
The SuperAGI Web Interactor Tool with Playwright equips users to interact and manipulate web content efficiently, paving the way for diverse and compelling use cases. Features: 1. Load Webpages: With SuperAGI's Web Interactor Tool, you can effortlessly load any webpage and view its content, ensuring you can access and interact with any website you need. 2. Interact with Web Elements: The tool lets you interact with various elements on a webpage, such as links, buttons, and forms, providing a highly interactive web experience. 3. Scrape Web Content: This feature allows the tool to extract specific content from webpages, useful for tasks like data collection and analysis. 4. Navigate Through Webpages: The tool helps you seamlessly navigate through webpages by clicking on links or using the browser's forward and back buttons. Authored-by: COLONAYUSH <ayushkaps9462@gmail.com>
- Loading branch information
1 parent
7c151bb
commit f6bbc7c
Showing
16 changed files
with
669 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
Xvfb :0 -screen 0 1280x1024x24 & | ||
x11vnc -display :0 -N -forever -shared & | ||
|
||
exec "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from playwright.sync_api import sync_playwright | ||
|
||
class BrowserWrapper: | ||
def __init__(self): | ||
self.page = None | ||
self.client = None | ||
self.page_element_buffer = None | ||
|
||
def start_browser_and_goto_page(self, url: str) -> str: | ||
global browser | ||
|
||
browser = ( | ||
sync_playwright() | ||
.start() | ||
.chromium.launch( | ||
headless=False, | ||
) | ||
) | ||
|
||
self.page = browser.new_page() | ||
self.page.set_viewport_size({"width": 1280, "height": 1080}) | ||
|
||
try: | ||
self.page.goto(url=url if "://" in url else "http://" + url) | ||
self.client = self.page.context.new_cdp_session(self.page) | ||
self.page_element_buffer = {} | ||
except: | ||
return "Failed to go to URL, please try again and make sure the URL is correct." | ||
|
||
return f"Browser successfully started and navigated to {url}!" | ||
|
||
|
||
browser_wrapper = BrowserWrapper() |
Empty file.
51 changes: 51 additions & 0 deletions
51
superagi/tools/web_interactor/playwright_click_element_by_id.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from superagi.tools.base_tool import BaseTool | ||
from pydantic import BaseModel, Field | ||
from superagi.helper.browser_wrapper import browser_wrapper | ||
|
||
from typing import Type, Optional, List | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from superagi.tools.base_tool import BaseTool | ||
from pydantic import BaseModel, Field, PrivateAttr | ||
black_listed_elements = set(["html", "head", "title", "meta", "iframe", "body", "style", "script", "path", "svg", "br", "::marker",]) | ||
# global page_element_buffer | ||
# page_element_buffer = {} | ||
|
||
|
||
class ClickElementByIdSchema(BaseModel): | ||
element_id: str = Field( | ||
..., | ||
description="The ID of the HTML element to click." | ||
) | ||
|
||
|
||
class ClickElementByIdTool(BaseTool): | ||
name = "PlaywrightClickElementByID" | ||
description = "A tool for clicking an element by its ID using Playwright.clicks an element. Specify the id with the unique id received from the get_dom command. CRITICAL: The ID must be the integer id from the get_dom command. It should execute after getting the DOM" | ||
args_schema: Type[ClickElementByIdSchema] = ClickElementByIdSchema | ||
|
||
def _execute(self, element_id: str) -> str: | ||
page_element_buffer = browser_wrapper.page_element_buffer | ||
client = browser_wrapper.client | ||
page = browser_wrapper.page | ||
|
||
# Inject javascript into the page which removes the target= attribute from all links | ||
js = """ | ||
links = document.getElementsByTagName("a"); | ||
for (var i = 0; i < links.length; i++) { | ||
links[i].removeAttribute("target"); | ||
} | ||
""" | ||
page.evaluate(js) | ||
|
||
element = page_element_buffer.get(int(element_id)) | ||
if element: | ||
x = element.get("center_x") | ||
y = element.get("center_y") | ||
|
||
page.mouse.click(x, y) | ||
else: | ||
return "Could not find element" | ||
|
||
return "Successfully clicked!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from superagi.tools.base_tool import BaseTool | ||
from pydantic import BaseModel, Field | ||
from superagi.helper.browser_wrapper import browser_wrapper | ||
|
||
from typing import Type, Optional, List | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from superagi.tools.base_tool import BaseTool | ||
from pydantic import BaseModel, Field, PrivateAttr | ||
class EnterSchema(BaseModel): | ||
pass | ||
|
||
|
||
class EnterTool(BaseTool): | ||
name = "PlaywrightEnter" | ||
description = "A tool for pressing the Enter key using Playwright." | ||
args_schema: Type[EnterSchema] = EnterSchema | ||
|
||
def _execute(self) -> str: | ||
page = browser_wrapper.page | ||
|
||
page.keyboard.press("Enter") | ||
return "Pressed enter!" |
Oops, something went wrong.