crawl4ai icon indicating copy to clipboard operation
crawl4ai copied to clipboard

Γ— after_goto() got an unexpected keyword argument 'context'

Open b-sai opened this issue 1 year ago β€’ 1 comments

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 avatar Dec 04 '24 22:12 b-sai

@b-sai like this

async def after_goto(page: Page, context: BrowserContext):
    print("[HOOK] after_goto")
    print(f"Current URL: {page.url}")

wfj6ccff avatar Dec 05 '24 12:12 wfj6ccff

@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

unclecode avatar Dec 09 '24 11:12 unclecode

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 avatar Jan 03 '25 13:01 streichsbaer

@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

unclecode avatar Jan 05 '25 11:01 unclecode