[Outdated] Using Selenium Remote (Selenium Grid) with browser_use

Python
2025-05-27 08:55 (9 months ago)
Open Doors, Digital Shores
Play a song themed on this article

Note: This article is old.

This method can no longer be used with browser-use 0.2 and later. If you are using browser-use versions after that, you need to perform remote debugging via Chrome’s debugging port.


browser_use uses Playwright internally.

Since Playwright can use Selenium Remote (Selenium Grid), you can use a remote browser simply by setting the environment variable SELENIUM_REMOTE_URL to the Selenium Remote URL (a URL like http://selenium-grid.example.com:4444).

However, by default it runs in headless mode, and you can’t specify a context, so it’s not customizable.

If you create a customized Playwright instance and inject it into the Agent, you can use Playwright exactly the way you want.

Sample code

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.
* The login button...
* In Email ...
'''

sync def main():
    async with async_playwright() as p:
        # Create and customize a 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',  # Language setting
            ignore_https_errors=True,  # Ignore certificate errors
        )
        # Create a browser_use Browser and inject the Playwright browser
        browser = Browser()
        browser.playwright_browser = playwright_browser
        browser.playwright = p
        
        # Inject the browser_use Browser 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())
Currently unrated
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Categories

Archive