Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Formatting

magmueller0081d161eaeb
diff --git a/src/state_manager/state.py b/src/state_manager/state.py
index 160508dc3..0d0b49d15 100644
--- a/src/state_manager/state.py
+++ b/src/state_manager/state.py
@@ -8,47 +8,46 @@ from src.dom.service import DomService
class StateManager:
- def __init__(self, driver: webdriver.Chrome):
- self.driver = driver
- self.dom_service = DomService()
-
- def get_current_state(self) -> Dict:
- """
- Retrieves current URL and interactable elements from processed HTML.
-
- Returns:
- Dict: Current state including URL, page title, interactable elements and selector map
- """
- html_content = self.driver.page_source
- processed_content = self.dom_service.process_content(html_content)
-
- return {
- 'current_url': self.driver.current_url,
- 'page_title': self.driver.title,
- 'interactable_elements': processed_content.output_string,
- 'selector_map': processed_content.selector_map
- }
-
- def get_functions(self) -> List[Dict]:
- """
- Retrieves available functions from cleaned HTML.
- """
- return []
-
- def get_main_content(self) -> str:
- """
- Retrieves main content from the page.
- """
- try:
- # Get HTML using requests
- response = requests.get(self.driver.current_url)
- response.encoding = 'utf-8'
- content = response.text
-
- # Process content using DomService
- processed_content = self.dom_service.process_content(content)
- return processed_content.output_string
-
- except Exception as e:
- print(f'Error getting main content: {e}')
- return ''
+ def __init__(self, driver: webdriver.Chrome):
+ self.driver = driver
+ self.dom_service = DomService(driver)
+
+ def get_current_state(self) -> Dict:
+ """
+ Retrieves current URL and interactable elements from processed HTML.
+
+ Returns:
+ Dict: Current state including URL, page title, interactable elements and selector map
+ """
+ processed_content = self.dom_service.get_current_state()
+
+ return {
+ 'current_url': self.driver.current_url,
+ 'page_title': self.driver.title,
+ 'interactable_elements': processed_content.output_string,
+ 'selector_map': processed_content.selector_map,
+ }
+
+ def get_functions(self) -> List[Dict]:
+ """
+ Retrieves available functions from cleaned HTML.
+ """
+ return []
+
+ def get_main_content(self) -> str:
+ """
+ Retrieves main content from the page.
+ """
+ try:
+ # Get HTML using requests
+ response = requests.get(self.driver.current_url)
+ response.encoding = 'utf-8'
+ content = response.text
+
+ # Process content using DomService
+ processed_content = self.dom_service._process_content(content)
+ return processed_content.output_string
+
+ except Exception as e:
+ print(f'Error getting main content: {e}')
+ return ''
diff --git a/src/tests/test_kayak_search.py b/src/tests/test_kayak_search.py
index 0c6fa82d7..00953d00b 100644
--- a/src/tests/test_kayak_search.py
+++ b/src/tests/test_kayak_search.py
@@ -9,50 +9,53 @@ from src.utils.selenium_utils import setup_selenium_driver
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 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()
+ def tearDown(self):
+ self.driver.quit()
- def test_kayak_flight_search(self):
- # Define the task
- task = 'Go to directly to the url https://www.kayak.ch/ and look there for flights from Zurich to New York for Nov 5th, 2024 for 2 people'
+ def test_kayak_flight_search(self):
+ # Define the task
+ task = 'Go to directly to the url https://www.kayak.ch/ and look there for flights from Zurich to New York for Nov 5th, 2024 for 2 people'
- default_actions = self.actions.get_default_actions()
- print(f'Default actions: {default_actions}')
+ default_actions = self.actions.get_default_actions()
+ print(f'Default actions: {default_actions}')
- agent = PlaningAgent(task, default_actions)
- url_history = []
+ agent = PlaningAgent(task, default_actions)
+ url_history = []
- # Main interaction loop
- max_steps = 10
- for i in range(max_steps):
- # Get current state
- input('\n\n\nPress Enter to continue...')
+ # Main interaction loop
+ max_steps = 50
+ for i in range(max_steps):
+ # Get current state
+ # input('\n\n\nPress Enter to continue...')
- current_state = self.state_manager.get_current_state()
- save_formatted_html(current_state['interactable_elements'], f'current_state_{i}.html')
- print(f'Current state map: {current_state["selector_map"]}')
- # save_markdown(current_state["main_content"], f"current_state_{i}.md")
- # Get next action from agent
- url_history.append(self.driver.current_url)
- text = f'Elements: {current_state["interactable_elements"]}, Url history: {url_history}'
- print(f'\n{text}\n')
+ current_state = self.state_manager.get_current_state()
+ save_formatted_html(current_state['interactable_elements'], f'current_state_{i}.html')
+ print(f'Current state map: {current_state["selector_map"]}')
+ # save_markdown(current_state["main_content"], f"current_state_{i}.md")
+ # Get next action from agent
+ url_history.append(self.driver.current_url)
+ text = f'Elements: {current_state["interactable_elements"]}, Url history: {url_history}'
+ print(f'\n{text}\n')
- action = agent.chat(text, skip_call=False)
- print(f'Selected action: {action}')
+ action = agent.chat(text, skip_call=False)
+ print(f'Selected action: {action}')
- out = self.actions.execute_action(action, current_state['selector_map'])
- if out:
- print('Task completed')
- break
+ out = self.actions.execute_action(action, current_state['selector_map'])
+ if out:
+ print('Task completed')
+ break
- else:
- self.fail('Failed to complete flight search task in maximum steps')
+ # Wait for 1 second
+ time.sleep(1)
+
+ else:
+ self.fail('Failed to complete flight search task in maximum steps')
if __name__ == '__main__':
- unittest.main()
+ unittest.main()