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
apply ruff formatting to onepassword.py
ShawnPanacf422f156ca8
diff --git a/examples/use-cases/onepassword.py b/examples/use-cases/onepassword.pyindex d5c02c82e..4fc2ccf04 100644--- a/examples/use-cases/onepassword.py+++ b/examples/use-cases/onepassword.py@@ -20,37 +20,32 @@ How to setup 1Password with Browser UseNote: In this example, we assume that you created a vault named "prod-secrets" and added an item named "X" with fields "username" and "password"."""+async def main():- # Gets your service account token from environment variable- token = os.getenv("OP_SERVICE_ACCOUNT_TOKEN")-- # Authenticate with 1Password- op_client = await Client.authenticate(- auth=token,- integration_name="Browser Use Secure Login",- integration_version="v1.0.0"- )-- # Initialize tools- tools = Tools()-- @tools.registry.action('Apply CSS blur filter to entire page content')- async def blur_page(- browser_session: BrowserSession- ):- """- Applies CSS blur filter directly to document.body to obscure all page content.- The blur will remain until unblur_page is called.- DOM remains accessible for element finding while page is visually blurred.- """- try:- # Get CDP session- cdp_session = await browser_session.get_or_create_cdp_session()-- # Apply blur filter to document.body- result = await cdp_session.cdp_client.send.Runtime.evaluate(- params={- 'expression': '''+ # Gets your service account token from environment variable+ token = os.getenv('OP_SERVICE_ACCOUNT_TOKEN')++ # Authenticate with 1Password+ op_client = await Client.authenticate(auth=token, integration_name='Browser Use Secure Login', integration_version='v1.0.0')++ # Initialize tools+ tools = Tools()++ @tools.registry.action('Apply CSS blur filter to entire page content')+ async def blur_page(browser_session: BrowserSession):+ """+ Applies CSS blur filter directly to document.body to obscure all page content.+ The blur will remain until unblur_page is called.+ DOM remains accessible for element finding while page is visually blurred.+ """+ try:+ # Get CDP session+ cdp_session = await browser_session.get_or_create_cdp_session()++ # Apply blur filter to document.body+ result = await cdp_session.cdp_client.send.Runtime.evaluate(+ params={+ 'expression': """(function() {// Check if already blurredif (document.body.getAttribute('data-page-blurred') === 'true') {@@ -67,47 +62,36 @@ async def main():console.log('[BLUR] Applied CSS blur to page');return true;})();- ''',- 'returnByValue': True,- },- session_id=cdp_session.session_id,- )-- success = result.get('result', {}).get('value', False)- if success:- print(f"[BLUR] Applied CSS blur to page")- return ActionResult(- extracted_content='Successfully applied CSS blur to page',- include_in_memory=True- )- else:- return ActionResult(- error='Failed to apply blur',- include_in_memory=True- )-- except Exception as e:- print(f"[BLUR ERROR] {e}")- return ActionResult(- error=f'Failed to blur page: {str(e)}',- include_in_memory=True- )-- @tools.registry.action('Remove CSS blur filter from page')- async def unblur_page(- browser_session: BrowserSession- ):- """- Removes the CSS blur filter from document.body, restoring normal page visibility.- """- try:- # Get CDP session- cdp_session = await browser_session.get_or_create_cdp_session()-- # Remove blur filter from body- result = await cdp_session.cdp_client.send.Runtime.evaluate(- params={- 'expression': '''+ """,+ 'returnByValue': True,+ },+ session_id=cdp_session.session_id,+ )++ success = result.get('result', {}).get('value', False)+ if success:+ print(f'[BLUR] Applied CSS blur to page')+ return ActionResult(extracted_content='Successfully applied CSS blur to page', include_in_memory=True)+ else:+ return ActionResult(error='Failed to apply blur', include_in_memory=True)++ except Exception as e:+ print(f'[BLUR ERROR] {e}')+ return ActionResult(error=f'Failed to blur page: {str(e)}', include_in_memory=True)++ @tools.registry.action('Remove CSS blur filter from page')+ async def unblur_page(browser_session: BrowserSession):+ """+ Removes the CSS blur filter from document.body, restoring normal page visibility.+ """+ try:+ # Get CDP session+ cdp_session = await browser_session.get_or_create_cdp_session()++ # Remove blur filter from body+ result = await cdp_session.cdp_client.send.Runtime.evaluate(+ params={+ 'expression': """(function() {if (document.body.getAttribute('data-page-blurred') !== 'true') {console.log('[BLUR] Page not blurred');@@ -122,75 +106,56 @@ async def main():console.log('[BLUR] Removed CSS blur from page');return true;})();- ''',- 'returnByValue': True,- },- session_id=cdp_session.session_id,- )-- removed = result.get('result', {}).get('value', False)- if removed:- print(f"[BLUR] Removed CSS blur from page")- return ActionResult(- extracted_content='Successfully removed CSS blur from page',- include_in_memory=True- )- else:- print(f"[BLUR] Page was not blurred")- return ActionResult(- extracted_content='Page was not blurred (may have already been removed)',- include_in_memory=True- )-- except Exception as e:- print(f"[BLUR ERROR] {e}")- return ActionResult(- error=f'Failed to unblur page: {str(e)}',- include_in_memory=True- )-- # LLM can call this action to use actors to fill in sensitive fields using 1Password values.- @tools.registry.action('Fill in a specific field for a website using value from 1Password vault')- async def fill_field(- vault_name: str,- item_name: str,- field_name: str,- browser_session: BrowserSession- ):- """- Fills in a specific field for a website using the value from 1Password.- Note: Use blur_page before calling this if you want visual security.- """- try:- # Resolve field value from 1Password- field_value = await op_client.secrets.resolve(- f"op://{vault_name}/{item_name}/{field_name}"- )-- # Get current page- page = await browser_session.must_get_current_page()-- # Find and fill the element- target_field = await page.must_get_element_by_prompt(f'{field_name} input field', llm)- await target_field.fill(field_value)-- return ActionResult(- extracted_content=f'Successfully filled {field_name} field for {vault_name}/{item_name}',- include_in_memory=True- )- except Exception as e:- return ActionResult(- error=f'Failed to fill {field_name} field: {str(e)}',- include_in_memory=True- )--- browser_session = Browser()-- llm = ChatOpenAI(model="o3")-- agent = Agent(- task="""+ """,+ 'returnByValue': True,+ },+ session_id=cdp_session.session_id,+ )++ removed = result.get('result', {}).get('value', False)+ if removed:+ print(f'[BLUR] Removed CSS blur from page')+ return ActionResult(extracted_content='Successfully removed CSS blur from page', include_in_memory=True)+ else:+ print(f'[BLUR] Page was not blurred')+ return ActionResult(+ extracted_content='Page was not blurred (may have already been removed)', include_in_memory=True+ )++ except Exception as e:+ print(f'[BLUR ERROR] {e}')+ return ActionResult(error=f'Failed to unblur page: {str(e)}', include_in_memory=True)++ # LLM can call this action to use actors to fill in sensitive fields using 1Password values.+ @tools.registry.action('Fill in a specific field for a website using value from 1Password vault')+ async def fill_field(vault_name: str, item_name: str, field_name: str, browser_session: BrowserSession):+ """+ Fills in a specific field for a website using the value from 1Password.+ Note: Use blur_page before calling this if you want visual security.+ """+ try:+ # Resolve field value from 1Password+ field_value = await op_client.secrets.resolve(f'op://{vault_name}/{item_name}/{field_name}')++ # Get current page+ page = await browser_session.must_get_current_page()++ # Find and fill the element+ target_field = await page.must_get_element_by_prompt(f'{field_name} input field', llm)+ await target_field.fill(field_value)++ return ActionResult(+ extracted_content=f'Successfully filled {field_name} field for {vault_name}/{item_name}', include_in_memory=True+ )+ except Exception as e:+ return ActionResult(error=f'Failed to fill {field_name} field: {str(e)}', include_in_memory=True)++ browser_session = Browser()++ llm = ChatOpenAI(model='o3')++ agent = Agent(+ task="""Navigate to https://x.com/i/flow/loginWait for the page to load.Use fill_field action with vault_name='prod-secrets' and item_name='X' and field_name='username'.@@ -203,14 +168,16 @@ async def main():Only use unblur_page action after you see the logged in user's X timeline.Your priority is to keep the username and password hidden while filling sensitive fields.""",- browser_session=browser_session,- llm=llm,- tools=tools,- file_system_path="./agent_data"- )+ browser_session=browser_session,+ llm=llm,+ tools=tools,+ file_system_path='./agent_data',+ )++ await agent.run()+- await agent.run()+if __name__ == '__main__':+ import asyncio-if __name__ == "__main__":- import asyncio- asyncio.run(main())+ asyncio.run(main())