browser_use
utilizes Playwright internally.
Playwright can work with Selenium Remote (Selenium Grid), so by setting the environment variable SELENIUM_REMOTE_URL
to the URL of the Selenium Remote (a URL like http://selenium-grid.example.com:4444
), you can use a remote browser just like that.
However, by default, it runs in headless mode, and you can't specify the context, so customization isn't possible.
By creating a customized Playwright and injecting it into the Agent, you can use Playwright as you desire.
from langchain_openai import ChatOpenAI
from browser_use import Agent, Browser
from playwright.async_api import async_playwright
import asyncio
import os
os.environ['SELENIUM_REMOTE_URL'] = 'http://selenium-grid.example.com:4444'
prompt = f'''
* Please open https://www.example.com.
* Click the login button...
* For the email...
'''
sync def main():
async with async_playwright() as p:
# Create and customize the Playwright browser
playwright_browser = await p.chromium.launch(
headless=False, # Disable headless mode
)
browser_context = await playwright_browser.new_context(
viewport={'width': 670, 'height': 377}, # Set viewport size
locale='ja-JP', # Set language
ignore_https_errors=True, # Ignore certificate errors
)
# Create a Browser from browser_use and inject the Playwright browser
browser = Browser()
browser.playwright_browser = playwright_browser
browser.playwright = p
# Inject the Browser from browser_use into the Agent
agent = Agent(
task=prompt,
llm=ChatOpenAI(model="gpt-4.1-mini"),
browser=browser,
browser_context=browser_context,
)
result = await agent.run(max_steps=5)
print(result)
if __name__ == "__main__":
asyncio.run(main())