Skip to content

Commit

Permalink
add-FormAction
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewKorzh committed Aug 9, 2024
1 parent 957708b commit da3b321
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
12 changes: 12 additions & 0 deletions scrapypuppeteer/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ class Har(PuppeteerServiceAction):

def payload(self):
return {}


class FormAction(PuppeteerServiceAction):
endpoint = "form_action"

def __init__(self, input_mapping: dict, submit_button: str = None):
self.input_mapping = input_mapping
self.submit_button = submit_button

def payload(self):
return {"inputMapping": self.input_mapping, "submitButton": self.submit_button}




Expand Down
34 changes: 30 additions & 4 deletions scrapypuppeteer/browser_managers/local_browser_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def __init__(self):
"screenshot": self.screenshot,
"action": self.action,
"recaptcha_solver": self.recaptcha_solver,
"har": self.har
"har": self.har,
"form_action": self.form_action
}

def process_request(self, request):
Expand Down Expand Up @@ -243,6 +244,34 @@ async def async_scroll():

return syncer.sync(async_scroll())


def form_action(self, request: PuppeteerRequest):
context_id, page_id = syncer.sync(self.context_manager.check_context_and_page(request.context_id, request.page_id))
page = self.context_manager.get_page_by_id(context_id, page_id)

async def async_form_action():
input_mapping = request.action.payload().get("inputMapping")
submit_button = request.action.payload().get("submitButton", None)
cookies = request.cookies

for selector, params in input_mapping.items():
value = params.get("value", "no value was provided")
delay = params.get("delay", 0)
await page.type(selector, value, {"delay": delay})

if submit_button:
await page.click(submit_button)

response_html = await page.content()
return PuppeteerHtmlResponse(request.url,
request,
context_id = context_id,
page_id = page_id,
html = response_html,
cookies=cookies)

return syncer.sync(async_form_action())


def action(self, request: PuppeteerRequest):
raise ValueError("CustomJsAction is not available in local mode")
Expand All @@ -252,6 +281,3 @@ def recaptcha_solver(self, request: PuppeteerRequest):

def har(self, request: PuppeteerRequest):
raise ValueError("Har is not available in local mode")



3 changes: 2 additions & 1 deletion scrapypuppeteer/browser_managers/service_browser_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
Scroll,
CustomJsAction,
Har,
FormAction
)
from scrapypuppeteer.response import (
PuppeteerResponse,
Expand Down Expand Up @@ -210,7 +211,7 @@ def _form_response(

@staticmethod
def _get_response_class(request_action):
if isinstance(request_action, (GoTo, GoForward, GoBack, Click, Scroll)):
if isinstance(request_action, (GoTo, GoForward, GoBack, Click, Scroll, FormAction)):
return PuppeteerHtmlResponse
if isinstance(request_action, Screenshot):
return PuppeteerScreenshotResponse
Expand Down
1 change: 0 additions & 1 deletion scrapypuppeteer/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
PuppeteerJsonResponse,
)
from scrapypuppeteer.request import ActionRequest, PuppeteerRequest, CloseContextRequest
#

from scrapypuppeteer.browser_managers.local_browser_manager import LocalBrowserManager
from scrapypuppeteer.browser_managers.service_browser_manager import ServiceBrowserManager
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def read_long_description(file_path):

setup(
name="scrapy-puppeteer-client",
version="0.3.4",
version="0.3.5",
description="A library to use Puppeteer-managed browser in Scrapy spiders",
long_description=read_long_description("README.md"),
long_description_content_type="text/markdown",
Expand Down

0 comments on commit da3b321

Please sign in to comment.