The error message TypeError: Browser.__init__() got an unexpected keyword argument 'BrowserConfig' indicates that you are passing a keyword argument named BrowserConfig to the Browser class constructor, but it doesn't accept such an argument.
Based on the provided context, the Browser class is initialized with the following signature:
From the code example in the query, it appears you are trying to configure the Browser using a BrowserConfig object. However, the Browser class does not accept such configuration directly. Instead, you need to pass the configuration to the BrowserContext and configure the Browser directly if needed.
Here's how you can configure the browser and browser context based on the provided context and code snippet:
python
from playwright.sync_api import sync_playwright
from browser_use.browser.browser import Browser
from browser_use.browser.context import BrowserContext, BrowserContextConfig
from browser_use.agent.service import Agent
from browser_use.controller.service import Controller
import asyncio
from langchain_openai import ChatOpenAI
# Define your BrowserContextConfig
context_config = BrowserContextConfig(
# Add any context-specific configurations here if needed
)
# define browser config if needed. By default it uses playwright
browser = Browser(
# headless=False, # you can specify headless here if you create a new browser
# chrome_instance_path=r"C:\Program Files\Google\Chrome\Application\chrome.exe", # you can specify chrome path here if you create a new browser
# extra_chromium_args=[
# r"--user-data-dir=C:\Users\YINL\AppData\Local\Google\Chrome\User Data"
# ],
)
# Create the browser context with the configurations
browser_context = BrowserContext(
browser=browser,
config=context_config,
)
llm = ChatOpenAI(model="gpt-4-turbo-preview", temperature=0)
async def main():
agent = Agent(
task="search 'openai' on google.com and describe the result page.",
llm=llm,
use_vision=True,
browser_context=browser_context, # Pass the configured browser_context
save_conversation_path="logs/conversation.json",
)
result = await agent.run()
print(result)
asyncio.run(main())
Key changes and explanation:
Removed BrowserConfig: The Browser class does not have a BrowserConfig argument.
Configured BrowserContext: A BrowserContextConfig object named context_config is created and passed to the BrowserContext constructor. The BrowserContextConfig contains settings for the browser context.
Passed browser_context to Agent: Instead of passing browser to the Agent, we are now passing browser_context. The agent will use this preconfigured browser_context for its operations.
Browser config: The browser config can be done directly in the Browser class, when a new browser instance is created. By default the browser uses playwright.
Removed headless, chrome_instance_path and extra_chromium_args from the Browser constructor to use default settings. They can be uncommented and customized if needed.
To summarize:
The Browser class is initialized with optional parameters such as headless, chrome_instance_path, and extra_chromium_args if a new browser instance is being created.
The BrowserContext class is initialized with a Browser instance and a BrowserContextConfig object.
The Agent class is initialized with the configured BrowserContext object rather than a Browser object if you want to use a specific config for your browser.
This revised configuration should resolve the TypeError and allow you to configure the browser and browser context correctly.