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
fix few bugs due to api changes, conditional logics, and null type-checks
yasithdevb491c8714462
diff --git a/browser_use/agent/service.py b/browser_use/agent/service.pyindex 55517bdaf..f627f7e1c 100644--- a/browser_use/agent/service.py+++ b/browser_use/agent/service.py@@ -427,7 +427,7 @@ class Agent(Generic[Context]):# Azure OpenAI API requires 'tools' parameter for GPT-4# The error 'content must be either a string or an array' occurs when# the API expects a tools array but gets something else- if 'gpt-4' in self.model_name.lower():+ if 'gpt-4-' in self.model_name.lower():return 'tools'else:return 'function_calling'diff --git a/browser_use/browser/session.py b/browser_use/browser/session.pyindex c2920e7fc..1ef9bd166 100644--- a/browser_use/browser/session.py+++ b/browser_use/browser/session.py@@ -166,7 +166,7 @@ class BrowserSession(BaseModel):session_own_fields = type(self).model_fields.keys()# get all the extra BrowserProfile kwarg overrides passed to BrowserSession(...) that are not Fields on self- overrides = self.model_dump(exclude=session_own_fields)+ overrides = self.model_dump(exclude=set(session_own_fields))# FOR REPL DEBUGGING ONLY, NEVER ALLOW CIRCULAR REFERENCES IN REAL CODE:# self.browser_profile._in_use_by_session = self@@ -198,7 +198,7 @@ class BrowserSession(BaseModel):await self.setup_playwright()await self.setup_browser_connection() # connects to existing browser if availableawait self.setup_browser_context() # creates a new context in existing browser or launches a new persistent context- assert self.browser_context+ assert self.browser_context is not None, 'BrowserContext object is not set'# resize the existing pages and set up foreground tab detectionawait self.setup_viewport_sizing()@@ -254,7 +254,7 @@ class BrowserSession(BaseModel):# # patchright handles all its own default args, dont mess with them# self.browser_profile.ignore_default_args = True- return self.playwright+ # return self.playwrightasync def setup_browser_connection(self) -> None:"""Override to customize the set up of the connection to an existing browser"""@@ -274,6 +274,7 @@ class BrowserSession(BaseModel):if self.wss_url:logger.info(f'🌎 Connecting to remote chromium playwright node.js server over WSS: {self.wss_url}')+ assert self.playwright, 'Playwright object is not set'self.browser = self.browser or await self.playwright.chromium.connect(self.wss_url,**self.browser_profile.kwargs_for_connect().model_dump(),@@ -295,7 +296,7 @@ class BrowserSession(BaseModel):# self.setup_browser_context() will be called next and if it finds self.browser is None, it will# launch a new browser+context all in one go using launch_persistent_context()- return self.browser+ # return self.browserasync def setup_browser_context(self) -> None:# if we have a browser_context but no browser, use the browser from the context@@ -400,7 +401,7 @@ class BrowserSession(BaseModel):logger.debug(f'🌎 {connection_method} Browser connected: v{self.browser.version}')assert self.browser_context, f'BrowserContext {self.browser_context} is not set up'- return self.browser_context+ # return self.browser_contextasync def setup_foreground_tab_detection(self) -> None:# Uses a combination of:@@ -419,6 +420,7 @@ class BrowserSession(BaseModel):# - https://github.com/microsoft/playwright/issues/13989# set up / detect foreground page+ assert self.browser_context is not None, 'BrowserContext object is not set'pages = self.browser_context.pagesforeground_page = Noneif pages:@@ -439,6 +441,8 @@ class BrowserSession(BaseModel):# Update human foreground tab stateold_foreground = self.human_current_page+ assert self.browser_context is not None, 'BrowserContext object is not set'+ assert old_foreground is not None, 'Old foreground page is not set'old_tab_idx = self.browser_context.pages.index(old_foreground)self.human_current_page = new_pagenew_tab_idx = self.browser_context.pages.index(new_page)@@ -570,6 +574,7 @@ class BrowserSession(BaseModel):@require_initializationasync def switch_tab(self, tab_index: int) -> Page:+ assert self.browser_context is not None, 'BrowserContext object is not set'pages = self.browser_context.pagesif not pages or tab_index >= len(pages):raise IndexError('Tab index out of range')@@ -621,60 +626,60 @@ class BrowserSession(BaseModel):selector_map = await self.get_selector_map()return selector_map.get(index)- @time_execution_async('--input_text_element_node')- async def _input_text_element_node(self, element_node: DOMElementNode, text: str):- """- Input text into an element with proper error handling and state management.- Handles different types of input fields and ensures proper element state before input.- """- try:- # Highlight before typing- # if element_node.highlight_index is not None:- # await self._update_state(focus_element=element_node.highlight_index)-- element_handle = await self.get_locate_element(element_node)-- if element_handle is None:- raise BrowserError(f'Element: {repr(element_node)} not found')-- # Ensure element is ready for input- try:- await element_handle.wait_for_element_state('stable', timeout=1000)- is_visible = await self._is_visible(element_handle)- if is_visible:- await element_handle.scroll_into_view_if_needed(timeout=1000)- except Exception:- pass-- # Get element properties to determine input method- tag_handle = await element_handle.get_property('tagName')- tag_name = (await tag_handle.json_value()).lower()- is_contenteditable = await element_handle.get_property('isContentEditable')- readonly_handle = await element_handle.get_property('readOnly')- disabled_handle = await element_handle.get_property('disabled')-- readonly = await readonly_handle.json_value() if readonly_handle else False- disabled = await disabled_handle.json_value() if disabled_handle else False-- # always click the element first to make sure it's in the focus- await element_handle.click()- await asyncio.sleep(0.1)-- try:- if (await is_contenteditable.json_value() or tag_name == 'input') and not (readonly or disabled):- await element_handle.evaluate('el => {el.textContent = ""; el.value = "";}')- await element_handle.type(text, delay=5)- else:- await element_handle.fill(text)- except Exception:- # last resort fallback, assume it's already focused after we clicked on it,- # just simulate keypresses on the entire page- page = await self.get_current_page()- await page.keyboard.type(text)-- except Exception as e:- logger.debug(f'❌ Failed to input text into element: {repr(element_node)}. Error: {str(e)}')- raise BrowserError(f'Failed to input text into index {element_node.highlight_index}')+ # @time_execution_async('--input_text_element_node')+ # async def _input_text_element_node(self, element_node: DOMElementNode, text: str):+ # """+ # Input text into an element with proper error handling and state management.+ # Handles different types of input fields and ensures proper element state before input.+ # """+ # try:+ # # Highlight before typing+ # # if element_node.highlight_index is not None:+ # # await self._update_state(focus_element=element_node.highlight_index)++ # element_handle = await self.get_locate_element(element_node)++ # if element_handle is None:+ # raise BrowserError(f'Element: {repr(element_node)} not found')++ # # Ensure element is ready for input+ # try:+ # await element_handle.wait_for_element_state('stable', timeout=1000)+ # is_visible = await self._is_visible(element_handle)+ # if is_visible:+ # await element_handle.scroll_into_view_if_needed(timeout=1000)+ # except Exception:+ # pass++ # # Get element properties to determine input method+ # tag_handle = await element_handle.get_property('tagName')+ # tag_name = (await tag_handle.json_value()).lower()+ # is_contenteditable = await element_handle.get_property('isContentEditable')+ # readonly_handle = await element_handle.get_property('readOnly')+ # disabled_handle = await element_handle.get_property('disabled')++ # readonly = await readonly_handle.json_value() if readonly_handle else False+ # disabled = await disabled_handle.json_value() if disabled_handle else False++ # # always click the element first to make sure it's in the focus+ # await element_handle.click()+ # await asyncio.sleep(0.1)++ # try:+ # if (await is_contenteditable.json_value() or tag_name == 'input') and not (readonly or disabled):+ # await element_handle.evaluate('el => {el.textContent = ""; el.value = "";}')+ # await element_handle.type(text, delay=5)+ # else:+ # await element_handle.fill(text)+ # except Exception:+ # # last resort fallback, assume it's already focused after we clicked on it,+ # # just simulate keypresses on the entire page+ # page = await self.get_current_page()+ # await page.keyboard.type(text)++ # except Exception as e:+ # logger.debug(f'❌ Failed to input text into element: {repr(element_node)}. Error: {str(e)}')+ # raise BrowserError(f'Failed to input text into index {element_node.highlight_index}')@time_execution_async('--click_element_node')async def _click_element_node(self, element_node: DOMElementNode) -> str | None:@@ -1151,6 +1156,8 @@ class BrowserSession(BaseModel):not necessarily the tab that is visible to the user (human_current_page).If they are the same tab, both references will be updated."""+ assert self.browser_context is not None, 'Browser context is not set'+ assert self.agent_current_page is not None, 'Agent current page is not set'# Check if this is the foreground tab as wellis_foreground = self.agent_current_page == self.human_current_page@@ -1369,6 +1376,7 @@ class BrowserSession(BaseModel):"""Returns a base64 encoded screenshot of the current page."""+ assert self.agent_current_page is not None, 'Agent current page is not set'# We no longer force tabs to the foreground as it disrupts user focus# await self.agent_current_page.bring_to_front()@@ -1757,6 +1765,7 @@ class BrowserSession(BaseModel):@time_execution_async('--switch_to_tab')async def switch_to_tab(self, page_id: int) -> Page:"""Switch to a specific tab by its page_id (aka tab index exposed to LLM)"""+ assert self.browser_context is not None, 'Browser context is not set'pages = self.browser_context.pagesif page_id >= len(pages):diff --git a/examples/custom-functions/action_filters.py b/examples/custom-functions/action_filters.pyindex 625e9d7de..216336594 100644--- a/examples/custom-functions/action_filters.py+++ b/examples/custom-functions/action_filters.py@@ -71,7 +71,7 @@ async def main():"""Main function to run the example"""browser_session = BrowserSession()await browser_session.start()- llm = ChatOpenAI(model_name='gpt-4o')+ llm = ChatOpenAI(model='gpt-4o')# Create the agentagent = Agent( # disco mode will not be triggered on apple.com because the LLM won't be able to see that action available, it should work on Google.com though.diff --git a/examples/models/azure_openai.py b/examples/models/azure_openai.pyindex fafc55e10..1e8da90b9 100644--- a/examples/models/azure_openai.py+++ b/examples/models/azure_openai.py@@ -27,10 +27,9 @@ if not azure_openai_api_key or not azure_openai_endpoint:# Initialize the Azure OpenAI clientllm = AzureChatOpenAI(- model_name='gpt-4o',- openai_api_key=azure_openai_api_key,+ model='gpt-4o',+ api_key=azure_openai_api_key,azure_endpoint=azure_openai_endpoint, # Corrected to use azure_endpoint instead of openai_api_base- deployment_name='gpt-4o', # Use deployment_name for Azure modelsapi_version='2024-08-01-preview', # Explicitly set the API version here)