Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      CodeSOD: A Unique Way to Primary Key

      July 22, 2025

      BrowserStack launches Figma plugin for detecting accessibility issues in design phase

      July 22, 2025

      Parasoft brings agentic AI to service virtualization in latest release

      July 22, 2025

      Node.js vs. Python for Backend: 7 Reasons C-Level Leaders Choose Node.js Talent

      July 21, 2025

      The best CRM software with email marketing in 2025: Expert tested and reviewed

      July 22, 2025

      This multi-port car charger can power 4 gadgets at once – and it’s surprisingly cheap

      July 22, 2025

      I’m a wearables editor and here are the 7 Pixel Watch 4 rumors I’m most curious about

      July 22, 2025

      8 ways I quickly leveled up my Linux skills – and you can too

      July 22, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025
      Recent

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025

      Zero Trust & Cybersecurity Mesh: Your Org’s Survival Guide

      July 22, 2025

      Execute Ping Commands and Get Back Structured Data in PHP

      July 22, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025
      Recent

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025

      “I don’t think I changed his mind” — NVIDIA CEO comments on H20 AI GPU sales resuming in China following a meeting with President Trump

      July 22, 2025

      Galaxy Z Fold 7 review: Six years later — Samsung finally cracks the foldable code

      July 22, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Machine Learning»An Advanced Coding Implementation: Mastering Browser‑Driven AI in Google Colab with Playwright, browser_use Agent & BrowserContext, LangChain, and Gemini

    An Advanced Coding Implementation: Mastering Browser‑Driven AI in Google Colab with Playwright, browser_use Agent & BrowserContext, LangChain, and Gemini

    April 20, 2025

    In this tutorial, we will learn how to harness the power of a browser‑driven AI agent entirely within Google Colab. We will utilize Playwright’s headless Chromium engine, along with the browser_use library’s high-level Agent and BrowserContext abstractions, to programmatically navigate websites, extract data, and automate complex workflows. We will wrap Google’s Gemini model via the langchain_google_genai connector to provide natural‑language reasoning and decision‑making, secured by pydantic’s SecretStr for safe API‑key handling. With getpass managing credentials, asyncio orchestrating non‑blocking execution, and optional .env support via python-dotenv, this setup will give you an end‑to‑end, interactive agent platform without ever leaving your notebook environment.

    Copy CodeCopiedUse a different Browser
    !apt-get update -qq
    !apt-get install -y -qq chromium-browser chromium-chromedriver fonts-liberation
    !pip install -qq playwright python-dotenv langchain-google-generative-ai browser-use
    !playwright install

    We first refresh the system package lists and install headless Chromium, its WebDriver, and the Liberation fonts to enable browser automation. It then installs Playwright along with python-dotenv, the LangChain GoogleGenerativeAI connector, and browser-use, and finally downloads the necessary browser binaries via playwright install.

    Copy CodeCopiedUse a different Browser
    import os
    import asyncio
    from getpass import getpass
    from pydantic import SecretStr
    from langchain_google_genai import ChatGoogleGenerativeAI
    from browser_use import Agent, Browser, BrowserContextConfig, BrowserConfig
    from browser_use.browser.browser import BrowserContext

    We bring in the core Python utilities, os for environment management and asyncio for asynchronous execution, plus getpass and pydantic’s SecretStr for secure API‑key input and storage. It then loads LangChain’s Gemini wrapper (ChatGoogleGenerativeAI) and the browser_use toolkit (Agent, Browser, BrowserContextConfig, BrowserConfig, and BrowserContext) to configure and drive a headless browser agent.

    Copy CodeCopiedUse a different Browser
    os.environ["ANONYMIZED_TELEMETRY"] = "false"

    We disable anonymous usage reporting by setting the ANONYMIZED_TELEMETRY environment variable to “false”, ensuring that neither Playwright nor the browser_use library sends any telemetry data back to its maintainers.

    Copy CodeCopiedUse a different Browser
    async def setup_browser(headless: bool = True):
        browser = Browser(config=BrowserConfig(headless=headless))
        context = BrowserContext(
            browser=browser,
            config=BrowserContextConfig(
                wait_for_network_idle_page_load_time=5.0,
                highlight_elements=True,
                save_recording_path="./recordings",
            )
        )
        return browser, context

    This asynchronous helper initializes a headless (or headed) Browser instance and wraps it in a BrowserContext configured to wait for network‑idle page loads, visually highlight elements during interactions, and save a recording of each session under ./recordings. It then returns both the browser and its ready‑to‑use context for your agent’s tasks.

    Copy CodeCopiedUse a different Browser
    async def agent_loop(llm, browser_context, query, initial_url=None):
        initial_actions = [{"open_tab": {"url": initial_url}}] if initial_url else None
        agent = Agent(
            task=query,
            llm=llm,
            browser_context=browser_context,
            use_vision=True,
            generate_gif=False,  
            initial_actions=initial_actions,
        )
        result = await agent.run()
        return result.final_result() if result else None

    This async helper encapsulates one “think‐and‐browse” cycle: it spins up an Agent configured with your LLM, the browser context, and optional initial URL tab, leverages vision when available, and disables GIF recording. Once you call agent_loop, it runs the agent through its steps and returns the agent’s final result (or None if nothing is produced).

    Copy CodeCopiedUse a different Browser
    async def main():
        raw_key = getpass("Enter your GEMINI_API_KEY: ")
    
    
        os.environ["GEMINI_API_KEY"] = raw_key
    
    
        api_key = SecretStr(raw_key)
        model_name = "gemini-2.5-flash-preview-04-17"
    
    
        llm = ChatGoogleGenerativeAI(model=model_name, api_key=api_key)
    
    
        browser, context = await setup_browser(headless=True)
    
    
        try:
            while True:
                query = input("nEnter prompt (or leave blank to exit): ").strip()
                if not query:
                    break
                url = input("Optional URL to open first (or blank to skip): ").strip() or None
    
    
                print("n🤖 Running agent…")
                answer = await agent_loop(llm, context, query, initial_url=url)
                print("n📊 Search Resultsn" + "-"*40)
                print(answer or "No results found")
                print("-"*40)
        finally:
            print("Closing browser…")
            await browser.close()
    
    
    await main()

    Finally, this main coroutine drives the entire Colab session: it securely prompts for your Gemini API key (using getpass and SecretStr), sets up the ChatGoogleGenerativeAI LLM and a headless Playwright browser context, then enters an interactive loop where it reads your natural‑language prompts (and optional start URL), invokes the agent_loop to perform the browser‑driven AI task, prints the results, and finally ensures the browser closes cleanly.

    In conclusion, by following this guide, you now have a reproducible Colab template that integrates browser automation, LLM reasoning, and secure credential management into a single cohesive pipeline. Whether you’re scraping real‑time market data, summarizing news articles, or automating reporting tasks, the combination of Playwright, browser_use, and LangChain’s Gemini interface provides a flexible foundation for your next AI‑powered project. Feel free to extend the agent’s capabilities, re‑enable GIF recording, add custom navigation steps, or swap in other LLM backends to tailor the workflow precisely to your research or production needs.


    Here is the Colab Notebook. Also, don’t forget to follow us on Twitter and join our Telegram Channel and LinkedIn Group. Don’t Forget to join our 90k+ ML SubReddit.

    🔥 [Register Now] miniCON Virtual Conference on AGENTIC AI: FREE REGISTRATION + Certificate of Attendance + 4 Hour Short Event (May 21, 9 am- 1 pm PST) + Hands on Workshop

    The post An Advanced Coding Implementation: Mastering Browser‑Driven AI in Google Colab with Playwright, browser_use Agent & BrowserContext, LangChain, and Gemini appeared first on MarkTechPost.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleDistroWatch Weekly, Issue 1118
    Next Article Fourier Neural Operators Just Got a Turbo Boost: Researchers from UC Riverside Introduce TurboFNO, a Fully Fused FFT-GEMM-iFFT Kernel Achieving Up to 150% Speedup over PyTorch

    Related Posts

    Machine Learning

    How to Evaluate Jailbreak Methods: A Case Study with the StrongREJECT Benchmark

    July 22, 2025
    Machine Learning

    Boolformer: Symbolic Regression of Logic Functions with Transformers

    July 22, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    CVE-2025-53509 – Advantech iView Argument Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Hackers exploited Windows WebDav zero-day to drop malware

    Security

    CVE-2025-49846 – Wire iOS Unauthenticated System Log Disclosure

    Common Vulnerabilities and Exposures (CVEs)
    Cryptocurrency Miner and Clipper Malware Spread via SourceForge Cracked Software Listings

    Cryptocurrency Miner and Clipper Malware Spread via SourceForge Cracked Software Listings

    Development

    Highlights

    CVE-2025-46617 – Quantum StorNext Web GUI API Unauthorized Configuration Access and Modification

    April 25, 2025

    CVE ID : CVE-2025-46617

    Published : April 25, 2025, 7:15 a.m. | 14 minutes ago

    Description : Quantum StorNext Web GUI API before 7.2.4 grants access to internal StorNext configuration and unauthorized modification of some software configuration parameters via undocumented user credentials. This affects StorNext RYO before 7.2.4, StorNext Xcellis Workflow Director before 7.2.4, and ActiveScale Cold Storage.

    Severity: 7.2 | HIGH

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    CVE-2025-6659 – PDF-XChange Editor PRC File Parsing Remote Code Execution Vulnerability

    June 25, 2025

    CVE-2024-12273 – CalculatedRoute Form WordPress Stored Cross-Site Scripting

    April 29, 2025

    Closing your Apple Watch rings can lower less stress, improve sleep – and win you prizes

    April 14, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.