Skip to content

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

Dont close browser and direct tab switch

magmueller49492bf1a628
diff --git a/src/browser/service.py b/src/browser/service.py
index 93e9718bf..1642f7453 100644
--- a/src/browser/service.py
+++ b/src/browser/service.py
@@ -157,6 +157,8 @@ class BrowserService:
self.driver = None
else:
input('Press Enter to close Browser...')
+ self.keep_open = False
+ self.close()
def __del__(self):
"""
@@ -344,47 +346,66 @@ class BrowserService:
# endregion
def handle_new_tab(self) -> dict:
- """Get info about newly opened tab without switching"""
+ """Handle newly opened tab and switch to it"""
driver = self._get_driver()
handles = driver.window_handles
- new_handle = handles[-1]
+ new_handle = handles[-1] # Get most recently opened handle
- # Only switch if we need to get info about the new tab
- current_handle = driver.current_window_handle
+ # Switch to new tab
driver.switch_to.window(new_handle)
+ self._current_handle = new_handle # Update current handle
+ # Wait for page load
+ self.wait_for_page_load()
+
+ # Get and cache tab info
tab_info = {
'handle': new_handle,
'url': driver.current_url,
'title': driver.title,
+ 'is_current': True,
}
-
- # Cache the info
self._tab_cache[new_handle] = tab_info
- # Switch back
- driver.switch_to.window(current_handle)
+ # Update is_current for all tabs
+ for handle in self._tab_cache:
+ self._tab_cache[handle]['is_current'] = handle == new_handle
+
return tab_info
def get_tabs_info(self) -> list[dict]:
- """Get cached information about all tabs"""
+ """Get information about all tabs"""
driver = self._get_driver()
current_handle = driver.current_window_handle
-
- # Update cache for current tab
- self._tab_cache[current_handle] = {
- 'handle': current_handle,
- 'url': driver.current_url,
- 'title': driver.title,
- 'is_current': True,
- }
-
- # Return all cached tab info
- return [
- {**tab_info, 'is_current': handle == current_handle}
- for handle, tab_info in self._tab_cache.items()
- if handle in driver.window_handles # Only return existing tabs
- ]
+ self._current_handle = current_handle # Update current handle
+
+ tabs_info = []
+ for handle in driver.window_handles:
+ is_current = handle == current_handle
+
+ # Use cached info if available, otherwise get new info
+ if handle in self._tab_cache:
+ tab_info = self._tab_cache[handle]
+ tab_info['is_current'] = is_current
+ else:
+ # Only switch if we need to get info
+ if not is_current:
+ driver.switch_to.window(handle)
+ tab_info = {
+ 'handle': handle,
+ 'url': driver.current_url,
+ 'title': driver.title,
+ 'is_current': is_current,
+ }
+ self._tab_cache[handle] = tab_info
+
+ tabs_info.append(tab_info)
+
+ # Switch back to current tab if we moved
+ if driver.current_window_handle != current_handle:
+ driver.switch_to.window(current_handle)
+
+ return tabs_info
def switch_tab(self, handle: str) -> dict:
"""Switch to specified tab and return its info"""
diff --git a/src/tests/test_search.py b/src/tests/test_search.py
index dccdef346..f84b32e87 100644
--- a/src/tests/test_search.py
+++ b/src/tests/test_search.py
@@ -132,13 +132,13 @@ async def test_wikipedia_tabs():
@observe()
async def test_albert_image_search():
# task = 'open 3 wikipedia pages in different tabs and then go back to the first one and summarize me the content of the page.'
- task = 'find an image of albert einstein and ask me for further instructions.'
+ task = 'find an image of albert einstein, elon musk, julius caesar and steve jobs in new tabs and ask me for further instructions.'
run_folder = setup_run_folder('albert_einstein_image_search')
controller = ControllerService(keep_open=True)
model = ChatAnthropic(
- model_name='claude-3-5-sonnet-20240620', timeout=25, stop=None, temperature=0.3
+ model_name='claude-3-5-sonnet-20240620', timeout=60, stop=None, temperature=0.3
)
model = ChatOpenAI(model='gpt-4o', temperature=0.3)