Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add form action #35

Merged
merged 12 commits into from
Aug 15, 2024
Merged

Add form action #35

merged 12 commits into from
Aug 15, 2024

Conversation

AndrewKorzh
Copy link
Contributor

@AndrewKorzh AndrewKorzh commented Aug 9, 2024

Pull Request: Add FillForm Class for Form Automation

Overview

This pull request introduces a new class FillForm, designed to automate the process of filling out and submitting forms on a webpage. The FillForm class allows users to specify input fields with their corresponding values and optional delays, as well as interact with a submit button if provided.

New Feature

Class: FillForm
This class extends PuppeteerServiceAction and is used to fill out forms based on CSS selectors. It also handles optional delays between keystrokes and submits the form if a submit button is specified.
Class Definition
The FillForm class is defined as follows:

class FillForm(PuppeteerServiceAction):
    """
    Fill out and submit forms on a webpage.

    Available options:

    * ``input_mapping`` (dict): A dictionary where each key is a CSS selector, and
    each value is another dictionary containing details about the input for that element.
    Each entry in the dictionary should follow this structure:

    * ``selector`` (str): The CSS selector for the input element (used as the key).
    * ``value`` (str): The text to be inputted into the element.
    * ``delay`` (int, optional): A delay (in milliseconds) between each keystroke
        when inputting the text. Defaults to 0 if not provided.

    * ``submit_button`` (str, optional): The CSS selector for the form's submit button.
    If provided, the button will be clicked after filling in the form.
    """

    endpoint = "fill_form"

    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}

Key Points

input_mapping: A dictionary that maps CSS selectors to input details. Each selector is paired with a dictionary containing:

value: The text to be entered into the input element.
delay (optional): Delay in milliseconds between keystrokes.
submit_button (optional): CSS selector for the submit button, which will be clicked after the form is filled.

Example Usage

To use the FillForm class, initialize it with the required input_mapping and an optional submit_button:

form_action = FillForm(
    input_mapping={
        "#username": {"value": "exampleUser", "delay": 100},
        "#password": {"value": "examplePass", "delay": 100}
    },
    submit_button="#loginButton"
)

This configuration will fill in the username and password fields with the provided values and delays, and click the submit button with the ID #loginButton.

@MatthewZMSU MatthewZMSU self-requested a review August 13, 2024 14:37
MatthewZMSU
MatthewZMSU previously approved these changes Aug 13, 2024
Copy link
Contributor

@MatthewZMSU MatthewZMSU left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Good simple logic.

Please:
1.) Do not ignore Pull Request template. Fill it please so that the reviewer would see the changes and motivation. Look for other PRs in hostory for examples.
2.) Do not ignore PEP8. It is one of the main aspects of Python. Use black for formatting.

examples/spiders/form_action.py Outdated Show resolved Hide resolved
examples/spiders/har.py Outdated Show resolved Hide resolved
scrapypuppeteer/actions.py Outdated Show resolved Hide resolved
examples/spiders/form_action.py Outdated Show resolved Hide resolved
MatthewZMSU
MatthewZMSU previously approved these changes Aug 15, 2024
@MatthewZMSU MatthewZMSU merged commit 74b59a8 into master Aug 15, 2024
14 checks passed
@MatthewZMSU MatthewZMSU deleted the add-FormAction branch August 15, 2024 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants