[Python] Is it possible to use browser-use with undetected-chromedriver?

Discussão em 'Python' iniciado por Stack, Maio 29, 2025.

  1. Stack

    Stack Membro Participativo

    I’m trying to drive an automated web search with the browser-use library, but I need to swap out the default Playwright for an undetected-chromedriver instance so that I can interact with pages guarded by reCAPTCHA (e.g. https://recaptcha-demo.appspot.com/recaptcha-v2-invisible.php).

    undetected-chromedriver: https://github.com/ultrafunkamsterdam/undetected-chromedriver browser-use: https://github.com/browser-use/browser-use

    When I replace the browser in my script with an undetected_chromedriver.Chrome(...), I see failures in the agent steps and eventually get:

    ERROR [agent] ❌ Stopping due to 3 consecutive failures
    None
    INFO [uc] ensuring close
    Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x0000024128D00FE0>
    Traceback (most recent call last):
    File "C:\Program Files\Python311\Lib\asyncio\proactor_events.py", line 116, in __del__
    _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Program Files\Python311\Lib\asyncio\proactor_events.py", line 80, in __repr__
    info.append(f'fd={self._sock.fileno()}')
    ^^^^^^^^^^^^^^^^^^^


    Below is a simplified version of my script:

    import os
    from dotenv import load_dotenv
    from langchain_google_genai import ChatGoogleGenerativeAI
    from browser_use import Agent, BrowserSession, Controller
    from pydantic import BaseModel
    import undetected_chromedriver as uc
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC

    load_dotenv()

    # Initialize Gemini LLM
    llm = ChatGoogleGenerativeAI(
    model=os.getenv("GEMINI_MODEL_NAME"),
    api_key=os.getenv("GEMINI_API_KEY"),
    )

    # Launch undetected Chrome
    driver = uc.Chrome(headless=False)
    browser_session = BrowserSession(driver=driver)

    class ResponseResult(BaseModel):
    response: str

    controller = Controller(output_model=ResponseResult)

    async def main():
    try:
    driver.get('https://recaptcha-demo.appspot.com/recaptcha-v2-invisible.php')
    WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.TAG_NAME, 'body')))
    await asyncio.sleep(2)

    agent = Agent(
    task="Find the text input fields, enter any data, send, and extract the response",
    llm=llm,
    browser_session=browser_session,
    initial_actions=[]
    )

    result = await agent.run()
    print(result.final_result())

    except Exception as e:
    print(f"Error during execution: {e!s}")
    finally:
    driver.quit()
    await browser_session.close()

    if __name__ == "__main__":
    asyncio.run(main())


    Expected behavior: The agent should be able to open the invisible reCAPTCHA demo page, perform the actions, and return the extracted response text.

    Actual behavior: The agent fails at Step 1 immediately, logging consecutive failures and then an unclosed transport warning from asyncio.

    Environment: Python Python 3.11.1 undetected-chromedriver 3.5.5 browser-use: 0.2.5 OS: Windows 10

    Continue reading...

Compartilhe esta Página