Am trying to develop a code that uses browser-use and playwright to open a website, and apply on jobs. I have automated the login and search and only want AI to kick in when the code has searched the jobs so that AI can open the results one by one and apply on them , however I keep getting this error:
'Browser' object has no attribute 'get_playwright_browser'
here is the code:
from browser_use.browser.context import BrowserContext
from playwright.async_api import BrowserContext as PlaywrightBrowserContext
class BrowserContextAdapter(BrowserContext):
def __init__(self, playwright_context: PlaywrightBrowserContext):
self.playwright_context = playwright_context
self.browser = playwright_context.browser # Add the browser attribute
self.config = self.initialize_config() # Initialize the config attribute
self.session = None # Add the session attribute
def initialize_config(self):
# Initialize the config with the necessary attributes
class Config:
minimum_wait_page_load_time = 1 # Set a default value or customize as needed
return Config()
async def new_page(self):
return await self.playwright_context.new_page()
# Add other methods as needed to match the expected interface
# ...
def __del__(self):
# Ensure proper cleanup
if self.session is not None:
print("NA")
# Example usage in your main code
import os
import sys
from langchain_openai import ChatOpenAI
from playwright.async_api import async_playwright
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import asyncio
import csv
from pathlib import Path
from playwright.async_api import async_playwright
from langchain_openai import ChatOpenAI
from browser_use import Agent, Browser, Controller, ActionResult
from browser_use.browser.context import BrowserContext
from PyPDF2 import PdfReader
from typing import List, Optional
from dotenv import load_dotenv
from pydantic import BaseModel
import logging
load_dotenv()
EMAIL = "[email protected]"
PASSWORD = "fcvghbjnkm"
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch(headless=False)
context = await browser.new_context()
page = await context.new_page()
model = ChatOpenAI(model='gpt-4o')
await page.goto('https://www.naukri.com')
print("Navigated to Naukri.com")
# Login process
try:
# Click login button
await page.click('a#login_Layer')
print("Clicked login button")
# Wait for the login form to load
await page.wait_for_selector('form[name="login-form"]', timeout=10000)
# Wait a bit for the form to be fully interactive
await asyncio.sleep(2)
# Fill email
await page.fill('input[placeholder="Enter your active Email ID / Username"]', EMAIL)
print("Filled email")
# Fill password
await page.fill('input[type="password"]', PASSWORD)
print("Filled password")
# Click the login submit button
await page.click('button.loginButton')
print("Clicked login button")
# Wait for login to complete
await page.wait_for_selector('div.nI-gNb-drawer__bars', timeout=30000)
print("Login successful")
# Wait for page to stabilize after login
await asyncio.sleep(3)
except Exception as e:
print(f"Login failed: {str(e)}")
await browser.close()
return
try:
# Wait for search bar after login
await page.wait_for_selector('.nI-gNb-search-bar input.suggestor-input', timeout=10000)
print("Search bar found")
await page.fill('.nI-gNb-search-bar input.suggestor-input', 'devsecops engineer')
print("Entered search term")
# Click search button
await page.click('.nI-gNb-sb__icon-wrapper')
print("Clicked search")
# Press Enter after filling search term
await page.press('.nI-gNb-search-bar input.suggestor-input', 'Enter')
print("Entered search term and pressed Enter")
await page.wait_for_timeout(5000) # Increased wait time for search results to load
except Exception as e:
print(f"Search failed: {str(e)}")
await browser.close()
return
# Use the adapter when creating the Agent
context_adapter = BrowserContextAdapter(context)
jobapply = Agent(
task='On the existing browser tab that open, search for apply button and click on apply',
llm=model,
browser_context=context_adapter, # Pass the adapter here
)
await jobapply.run()
asyncio.run(main())```
--------------------------------------------------------
I have tried multiple codes, used OpenAI and Claude, but this error keeps popping up