"""playwright_search_tool.py Playwright-driven web search tools for Open Deep Research / rdtii, used ALONGSIDE the existing paid SerpAPI/Serper `GoogleSearchTool` and `DuckDuckGoSearchTool` (not a replacement for either). Two search tools are provided: - PlaywrightDuckDuckGoSearchTool (RECOMMENDED as primary): scrapes DuckDuckGo's server-rendered HTML results page. No JavaScript wait, no CAPTCHA/consent wall in normal use, and stable markup -- the most scrape-friendly of the two. - PlaywrightGoogleSearchTool (fallback): scrapes Google's JS-rendered results page. Broader index (useful for some non-English/regional government sites DuckDuckGo misses), but far more bot-detection friction -- occasional CAPTCHA/consent walls and markup that shifts to resist scraping. Why either exists at all: SerpAPI/Serper cost money per query and DuckDuckGo (via the `ddgs` library) gets rate-limited under heavy/rapid use. These instead launch a real (headless) Chromium browser via Playwright and scrape results directly -- no paid API, no third-party search library, no per-query cost or account quota. They're drop-in-shaped tools: single `query` input, and a similar "numbered list of title/link/snippet" output shape as smolagents' GoogleSearchTool. Once wired in alongside the other search tools, the search_agent keeps using its existing tools (VisitTool, PageUpTool, PageDownTool, FinderTool, FindNextTool, ArchiveSearchTool, TextInspectorTool) to open, crawl, and read whatever pages these tools find -- only the initial "search" step differs. Requirements (not in requirements.txt by default -- add both): pip install playwright playwright install chromium Usage: from playwright_search_tool import PlaywrightDuckDuckGoSearchTool, PlaywrightGoogleSearchTool WEB_TOOLS = [GoogleSearchTool(...), DuckDuckGoSearchTool(), PlaywrightDuckDuckGoSearchTool(), PlaywrightGoogleSearchTool(), ...] --------------------------------------------------------------------------- Why PlaywrightGoogleSearchTool's extraction logic is NOT based on Google's CSS class names (div.g, div.MjjYud, etc.): Google's result-page markup (a) changes its class names frequently and without notice, and (b) is frequently served *differently* to traffic it suspects is automated -- a plain, unauthenticated, headless-looking browser can get a stripped-down or reflowed results page where those old class names simply don't exist, even though a person opening the same query in a normal browser sees full results. Relying on class names is why an earlier version of this tool could report "No Google results found" for a query that clearly has results. Instead, extraction here is structural: Google's results almost always still put each organic result's title inside an