Skip to content

Commit

Permalink
FormAction to FillForm
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewKorzh committed Aug 14, 2024
1 parent 9521c09 commit 6749b0e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions scrapypuppeteer/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def payload(self):
return {}


class FormAction(PuppeteerServiceAction):
class FillForm(PuppeteerServiceAction):
"""
Fill out and submit forms on a webpage.
Expand All @@ -265,7 +265,7 @@ class FormAction(PuppeteerServiceAction):
If provided, the button will be clicked after filling in the form.
"""

endpoint = "form_action"
endpoint = "fill_form"

def __init__(self, input_mapping: dict, submit_button: str = None):
self.input_mapping = input_mapping
Expand Down
20 changes: 11 additions & 9 deletions scrapypuppeteer/browser_managers/local_browser_manager.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import asyncio
import base64

from pyppeteer import launch
import syncer
import uuid

from scrapypuppeteer.response import (
PuppeteerHtmlResponse,
PuppeteerScreenshotResponse,
)
from scrapypuppeteer.request import ActionRequest, PuppeteerRequest, CloseContextRequest

import asyncio
from pyppeteer import launch
import syncer
import uuid
import base64
from scrapypuppeteer.browser_managers import BrowserManager


Expand Down Expand Up @@ -71,7 +73,7 @@ def __init__(self):
"action": self.action,
"recaptcha_solver": self.recaptcha_solver,
"har": self.har,
"form_action": self.form_action,
"fill_form": self.fill_form,
}

def process_request(self, request):
Expand Down Expand Up @@ -274,15 +276,15 @@ async def async_scroll():

return syncer.sync(async_scroll())

def form_action(self, request: PuppeteerRequest):
def fill_form(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():
async def async_fill_form():
input_mapping = request.action.payload().get("inputMapping")
submit_button = request.action.payload().get("submitButton", None)
cookies = request.cookies
Expand All @@ -305,7 +307,7 @@ async def async_form_action():
cookies=cookies,
)

return syncer.sync(async_form_action())
return syncer.sync(async_fill_form())

def action(self, request: PuppeteerRequest):
raise ValueError("CustomJsAction is not available in local mode")
Expand Down
6 changes: 2 additions & 4 deletions scrapypuppeteer/browser_managers/service_browser_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from scrapy.http import Headers, TextResponse, Response
from scrapy.utils.log import failure_to_exc_info
from twisted.python.failure import Failure
import time

from scrapypuppeteer.actions import (
Click,
Expand All @@ -23,7 +22,7 @@
Scroll,
CustomJsAction,
Har,
FormAction,
FillForm,
)
from scrapypuppeteer.response import (
PuppeteerResponse,
Expand All @@ -34,7 +33,6 @@
PuppeteerJsonResponse,
)
from scrapypuppeteer.request import ActionRequest, PuppeteerRequest, CloseContextRequest

from scrapypuppeteer.browser_managers import BrowserManager


Expand Down Expand Up @@ -210,7 +208,7 @@ def _form_response(
@staticmethod
def _get_response_class(request_action):
if isinstance(
request_action, (GoTo, GoForward, GoBack, Click, Scroll, FormAction)
request_action, (GoTo, GoForward, GoBack, Click, Scroll, FillForm)
):
return PuppeteerHtmlResponse
if isinstance(request_action, Screenshot):
Expand Down
4 changes: 3 additions & 1 deletion scrapypuppeteer/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
)
from scrapypuppeteer.request import ActionRequest, PuppeteerRequest, CloseContextRequest

from scrapypuppeteer.browser_managers.local_browser_manager import LocalBrowserManager
from scrapypuppeteer.browser_managers.local_browser_manager import (
LocalBrowserManager,
)
from scrapypuppeteer.browser_managers.service_browser_manager import (
ServiceBrowserManager,
)
Expand Down

0 comments on commit 6749b0e

Please sign in to comment.