Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
Cleanup functions
magmueller05ae2f64075e
diff --git a/src/actions/action_validator.py b/src/actions/action_validator.pyindex b1817ae29..ce3e51c66 100644--- a/src/actions/action_validator.py+++ b/src/actions/action_validator.py@@ -17,16 +17,7 @@ class ActionValidator:bool: True if action was successful, False otherwise"""try:- # Check for common failure indicators- if "error" in self.driver.title.lower():- return False-- # Add specific checks based on action type- if action["type"] == "click_element":- # Verify element was actually clicked- pass-- return True+ passexcept (TimeoutException, WebDriverException):return Falsediff --git a/src/actions/browser_actions.py b/src/actions/browser_actions.pyindex c8af58b20..0817988f4 100644--- a/src/actions/browser_actions.py+++ b/src/actions/browser_actions.py@@ -13,6 +13,7 @@ class BrowserActions:"""Performs a Google search with the provided query."""+ # TODO: replace with search apiself.driver.get("https://www.google.com")search_box = self.wait.until(EC.presence_of_element_located((By.NAME, "q"))diff --git a/src/agent_interface/agent.py b/src/agent_interface/agent.pyindex ad6751ce8..b6e8dc4ee 100644--- a/src/agent_interface/agent.py+++ b/src/agent_interface/agent.py@@ -1,4 +1,5 @@-from typing import Dict, List+from typing import Dict, List, Optional+import jsonclass Agent:@@ -7,6 +8,17 @@ class Agent:self.available_actions = Noneself.task = Noneself.context = None+ self.task_progress = []++ def set_task(self, task: Dict):+ """+ Sets the task for the agent to complete.++ Args:+ task (Dict): Task description with type and parameters+ """+ self.task = task+ self.task_progress = []def receive_state(self, state: Dict, actions: List):"""@@ -18,13 +30,4 @@ class Agent:"""self.current_state = stateself.available_actions = actions-- def decide_next_action(self) -> Dict:- """- Determines the next action based on the current state.-- Returns:- Dict: Action to be executed with its parameters- """- # Implementation will use LLM to decide next action- pass+ self.task_progress.append(state)diff --git a/src/agent_interface/prompts.py b/src/agent_interface/prompts.pyindex 162bd3bc8..3890f8f48 100644--- a/src/agent_interface/prompts.py+++ b/src/agent_interface/prompts.py@@ -6,9 +6,6 @@ you get the current state and a list of actions you can take.Your task is:{task}-Extra context:-{extra_context}-In every step, please only control the web browser with actions.Actions can also give you back follow-up questions about the state of the websiteif your instructions are ambiguous.diff --git a/src/state_manager/html_cleaner.py b/src/state_manager/html_cleaner.pyindex da0e3d0ec..bc0f2171a 100644--- a/src/state_manager/html_cleaner.py+++ b/src/state_manager/html_cleaner.py@@ -1,4 +1,4 @@-from bs4 import BeautifulSoup, Comment+from bs4 import BeautifulSoup, Comment, Tagimport rediff --git a/src/state_manager/state.py b/src/state_manager/state.pyindex d15f36c5a..a2450fd8d 100644--- a/src/state_manager/state.py+++ b/src/state_manager/state.py@@ -1,6 +1,8 @@from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom typing import Dict, List+from bs4 import BeautifulSoup+from .html_cleaner import cleanup_htmlclass StateManager:@@ -9,47 +11,16 @@ class StateManager:def get_current_state(self) -> Dict:"""- Retrieves current URL and interactable elements.+ Retrieves current URL and interactable elements from cleaned HTML.Returns:- Dict: Current state including URL and interactable elements+ Dict: Current state including URL, page title, and interactable elements"""+ html_content = self.driver.page_source+ cleaned_html = cleanup_html(html_content)+return {"current_url": self.driver.current_url,- "interactable_elements": self.get_interactable_elements()+ "page_title": self.driver.title,+ "interactable_elements": cleaned_html}-- def get_interactable_elements(self) -> List:- """- Extracts interactable elements from the page.-- Returns:- List: List of interactable elements with their properties- """- interactable_elements = []-- # Find buttons- buttons = self.driver.find_elements(By.TAG_NAME, "button")- for button in buttons:- interactable_elements.append({- "type": "button",- "identifier": {- "id": button.get_attribute("id"),- "class": button.get_attribute("class"),- "text": button.text- }- })-- # Find input fields- inputs = self.driver.find_elements(By.TAG_NAME, "input")- for input_field in inputs:- interactable_elements.append({- "type": "input",- "identifier": {- "id": input_field.get_attribute("id"),- "name": input_field.get_attribute("name"),- "placeholder": input_field.get_attribute("placeholder")- }- })-- return interactable_elementsdiff --git a/src/tests/test_actions.py b/src/tests/test_actions.pydeleted file mode 100644index c3d6bb02c..000000000--- a/src/tests/test_actions.py+++ /dev/null@@ -1,18 +0,0 @@-import unittest-from selenium import webdriver-from src.actions.browser_actions import BrowserActions-from src.utils.selenium_utils import setup_selenium_driver---class TestBrowserActions(unittest.TestCase):- def setUp(self):- self.driver = setup_selenium_driver(headless=True)- self.actions = BrowserActions(self.driver)-- def tearDown(self):- self.driver.quit()-- def test_go_to_url(self):- test_url = "https://www.example.com"- self.actions.go_to_url(test_url)- self.assertEqual(self.driver.current_url, test_url)diff --git a/src/tests/test_agent.py b/src/tests/test_agent.pydeleted file mode 100644index 8891b66a2..000000000--- a/src/tests/test_agent.py+++ /dev/null@@ -1,18 +0,0 @@-import unittest-from src.agent_interface.agent import Agent---class TestAgent(unittest.TestCase):- def setUp(self):- self.agent = Agent()-- def test_receive_state(self):- state = {"current_url": "https://example.com"}- actions = [{"type": "click", "params": {}}]- self.agent.receive_state(state, actions)- self.assertEqual(self.agent.current_state, state)- self.assertEqual(self.agent.available_actions, actions)-- def test_decide_next_action(self):- # Test will be implemented when decide_next_action is completed- passdiff --git a/src/tests/test_kayak_search.py b/src/tests/test_kayak_search.pynew file mode 100644index 000000000..ae91dd241--- /dev/null+++ b/src/tests/test_kayak_search.py@@ -0,0 +1,18 @@+import unittest+from src.utils.selenium_utils import setup_selenium_driver+from src.actions.browser_actions import BrowserActions+from src.state_manager.state import StateManager+++class TestKayakSearch(unittest.TestCase):+ def setUp(self):+ self.driver = setup_selenium_driver(headless=False)+ self.actions = BrowserActions(self.driver)+ self.state_manager = StateManager(self.driver)++ def tearDown(self):+ self.driver.quit()+++if __name__ == '__main__':+ unittest.main()diff --git a/src/tests/test_state_manager.py b/src/tests/test_state_manager.pydeleted file mode 100644index 88a2b347f..000000000--- a/src/tests/test_state_manager.py+++ /dev/null@@ -1,19 +0,0 @@-import unittest-from selenium import webdriver-from src.state_manager.state import StateManager-from src.utils.selenium_utils import setup_selenium_driver---class TestStateManager(unittest.TestCase):- def setUp(self):- self.driver = setup_selenium_driver(headless=True)- self.state_manager = StateManager(self.driver)-- def tearDown(self):- self.driver.quit()-- def test_get_current_state(self):- test_url = "https://www.example.com"- self.driver.get(test_url)- state = self.state_manager.get_current_state()- self.assertEqual(state["current_url"], test_url)diff --git a/src/tests/test_utils.py b/src/tests/test_utils.pydeleted file mode 100644index 8e75e4ffa..000000000--- a/src/tests/test_utils.py+++ /dev/null@@ -1,14 +0,0 @@-import unittest-from src.utils.selenium_utils import setup_selenium_driver---class TestSeleniumUtils(unittest.TestCase):- def test_setup_selenium_driver(self):- driver = setup_selenium_driver(headless=True)- self.assertIsNotNone(driver)- driver.quit()-- def test_headless_mode(self):- driver = setup_selenium_driver(headless=True)- self.assertTrue("--headless" in driver.options.arguments)- driver.quit()