{"id":13780771,"url":"https://github.com/guettli/html_form_to_dict","last_synced_at":"2025-08-19T14:33:18.502Z","repository":{"id":54517236,"uuid":"338586558","full_name":"guettli/html_form_to_dict","owner":"guettli","description":"HTML Form to Dict","archived":false,"fork":false,"pushed_at":"2022-10-20T14:27:17.000Z","size":52,"stargazers_count":15,"open_issues_count":0,"forks_count":3,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-12-16T23:07:45.127Z","etag":null,"topics":["html-forms","python","testing"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/guettli.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-02-13T13:58:28.000Z","updated_at":"2024-09-22T23:50:27.000Z","dependencies_parsed_at":"2022-08-13T18:21:00.777Z","dependency_job_id":null,"html_url":"https://github.com/guettli/html_form_to_dict","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guettli%2Fhtml_form_to_dict","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guettli%2Fhtml_form_to_dict/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guettli%2Fhtml_form_to_dict/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guettli%2Fhtml_form_to_dict/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/guettli","download_url":"https://codeload.github.com/guettli/html_form_to_dict/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230359943,"owners_count":18214158,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["html-forms","python","testing"],"created_at":"2024-08-03T18:01:19.609Z","updated_at":"2024-12-19T01:12:42.697Z","avatar_url":"https://github.com/guettli.png","language":"Python","funding_links":[],"categories":["Third Party Packages 📦 \u003ca name = \"tools\"\u003e\u003c/a\u003e"],"sub_categories":["Tools \u003ca name = \"tools\"\u003e\u003c/a\u003e"],"readme":"\n![Python package](https://github.com/guettli/html_form_to_dict/workflows/Python%20package/badge.svg)\n\n# HTML Form to Dict\n\nThis is a tiny library which provides a method called `html_form_to_dict()`.\n\nThis method takes a string containing HTML and returns a dictionary of the values of the first form.\n\nThe data returned by `html_form_to_dict()` is a `FormDict` which has the method `submit()`. This way\nyou can submit the data like a real browser would.\n\nThis mean you can do simple end-to-end testing of form handling without a real browser (like selenium/puppeteer/playwright).\n\nThe `submit()` method supports the \"action\" and \"method\" attributes of forms and additionaly the [htmx](//htmx.org) attributes [hx-get](https://htmx.org/attributes/hx-get/), [hx-post](https://htmx.org/attributes/hx-post/).\n\nExample:\n\n```\ndef test_foo(client):\n    ...\n    \n    # client is a DjangoClient. But you could use\n    # python-requests or a different URL-lib, too\n    response = client.get(url)\n    \n    # This method parses the HTML in response.content to a dictionary.\n    # This dictionary is like request.POST or request.GET.\n    # It is a flat mapping from the input elements of the form\n    # to their value.\n    data = html_form_to_dict(response.content)\n    \n    # Now you can test the default values of the form.\n    assert data == {'city': 'Chemnitz', 'name': 'Mr. X'}\n    \n    # You can edit the data. This is like a human (or Playwright/Selenium)\n    # altering the HTML input fields\n    data['name'] = 'Mrs. Y'\n    \n    # This submits the data to the server.\n    # This methods uses the \"action\" attribute of the form.\n    # The hx-get, hx-post attributes of htmx are supported, too\n    response = data.submit(client)\n    \n    # If you use the Post/Redirect/Get pattern:\n    assert response.status == 302, response.context['form'].errors\n```\n\nAbove code uses pytest-django. See [client fixture](https://pytest-django.readthedocs.io/en/latest/helpers.html#client-django-test-client)\n\nThe `FormDict` returned by `html_form_to_dict()` does not allow adding new\nkeys, which are not in the dictionary yet. This way you get an error if your\ntest sets the value for an input which (maybe due to refactoring) does not exist.\n\nAbove example uses Django, but the library is a pure Python library which does not depend on any\nparticular web-framework.\n\nThis library was build for testing, but you can use it for all tasks where you\nwant to parse and submit html forms.\n\nThis library does not evaluate JavaScript. If you need JS support, please use Playwright (or a similar tool).\n\n# Install\n\n```shell\npip install html_form_to_dict\n```\n\n# Development\n\nYou need to upload your ssh-pub-key to github first:\n\n```shell\npip install -e git+ssh://git@github.com/guettli/html_form_to_dict#egg=html_form_to_dict\nedit-the-code\npip install pytest\npytest\ncreate Pull-Request\n```\n\n# Alternatives\n\n* [Mechanize](https://mechanize.readthedocs.io/en/latest/) This library is like a browser without JS support.\n* You could use BeautifulSoup like explained in this [Stackoverflow Answer](https://stackoverflow.com/a/65571001/633961)\n* Use [Playwright](https://playwright.dev/) for browser based end-to-end tests.\n\n# Deploy\n\nvia deploy-library.py\n\nfor py2 tgz package: `python -m twine upload dist/html_form_to_dict-*.tar.gz`\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguettli%2Fhtml_form_to_dict","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fguettli%2Fhtml_form_to_dict","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguettli%2Fhtml_form_to_dict/lists"}