Skip to content

Commit

Permalink
Allow custom PageMethod callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemaeyer committed Sep 12, 2024
1 parent a28631a commit b3ad70d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion scrapy_playwright/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import warnings
from contextlib import suppress
from dataclasses import dataclass, field as dataclass_field
from functools import partial
from ipaddress import ip_address
from time import time
from typing import Awaitable, Callable, Dict, Optional, Tuple, Type, TypeVar, Union
Expand Down Expand Up @@ -607,7 +608,10 @@ async def _apply_page_methods(self, page: Page, request: Request, spider: Spider
for pm in page_methods:
if isinstance(pm, PageMethod):
try:
method = getattr(page, pm.method)
if callable(pm.method):
method = partial(pm.method, page)
else:
method = getattr(page, pm.method)
except AttributeError as ex:
logger.warning(
"Ignoring %r: could not find method",
Expand Down
6 changes: 3 additions & 3 deletions scrapy_playwright/page.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any
from typing import Any, Callable


__all__ = ["PageMethod"]
Expand All @@ -10,8 +10,8 @@ class PageMethod:
Playwright page, such as "click", "screenshot", "evaluate", etc.
"""

def __init__(self, method: str, *args, **kwargs) -> None:
self.method: str = method
def __init__(self, method: str | Callable, *args, **kwargs) -> None:
self.method: str | Callable = method
self.args: tuple = args
self.kwargs: dict = kwargs
self.result: Any = None
Expand Down

0 comments on commit b3ad70d

Please sign in to comment.