Γ after_goto() got an unexpected keyword argument 'context'
I am not able to use hooks. I followed the docs to setup this example
import asyncio
from crawl4ai import AsyncWebCrawler
from crawl4ai.async_crawler_strategy import AsyncPlaywrightCrawlerStrategy
from crawl4ai.cache_context import CacheMode
async def after_goto(page):
print("[HOOK] after_goto")
# Example customization: log the URL
print(f"Current URL: {page.url}")
async def main():
crawler_strategy = AsyncPlaywrightCrawlerStrategy(
verbose=True)
crawler_strategy.set_hook('after_goto', after_goto)
async with AsyncWebCrawler(verbose=True, crawler_strategy=crawler_strategy) as crawler:
result = await crawler.arun(
url="https://example.com",
js_code="window.scrollTo(0, document.body.scrollHeight);",
wait_for="footer",
cache_mode=CacheMode.BYPASS)
print(f"Basic crawl result: {result.markdown[:500]}")
asyncio.run(main())
Get the following error:
[INIT].... β Crawl4AI 0.4.0 [ERROR]... Γ https://example.com... | Error: βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β Γ after_goto() got an unexpected keyword argument 'context' β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Using latest version of crawl4ai
@b-sai like this
async def after_goto(page: Page, context: BrowserContext):
print("[HOOK] after_goto")
print(f"Current URL: {page.url}")
@b-sai In the new versions, more parameters are passed through hooks. However, I feel ashamed about the documentation, Iβve made a lot of changes recently and havenβt had time to update it. Thatβs something Iβll definitely focus on this weekβrecreating the whole thing. Iβm even considering building an automation pipeline using large language models to keep everything updated moving forward.
async def after_goto_hook(page, context=None, **kwargs):
# or async def after_goto_hook(page, **kwargs):
pass
I stumbled over this issue today as well.
In Version 0.4.0 everything works as per docs, but in later versions it throws this error.
This solved it for me:
async def on_browser_created(browser: Browser, context=None, **kwargs):
print("[HOOK] on_browser_created")
@streichsbaer You are very right. When I jumped to version 4, I had to change some important things, and I made those changes. Unfortunately, this brought some issues in the documentation. That's why I worked hard on the documentation, and I'm going to release it very soon. Right now, if you go to the links I will share with you, you can see that I am creating all the new docs in this folder. I apologize for any inconvenience. Just wait a bit; I will definitely update them all.
https://github.com/unclecode/crawl4ai/tree/next/docs/md_v3/tutorials