Utility to make automating processes using Selenium and Chromedriver easier
Project description
Admitted
/ədˈmɪtɪd/ verb : allowed entry (as to a place, fellowship, or privilege)
This project is very new. The API is very likely to change.
This library aims to make automating tasks that require authentication on websites simpler. In general, it would be better to make HTTP requests using an appropriate library, but at times it is not obvious how to replicate the login process, and you don't want to have to reverse engineer the site just to get your task done. That is where this library comes in.
We use Selenium to automate a Chrome for Testing instance, and set the user data directory to the Chrome default so that "remember me" settings will persist to avoid 2FA on every instance. We automatically install Chrome For Testing and ChromeDriver in a user binary location and manage ensuring the versions are aligned.
We expose a fetch method to make HTTP requests to the site with credentials through Chrome, eliminating the need to
copy cookies and headers; and a direct_request method that uses urllib3 (which is also a dependency of Selenium) to
make anonymous HTTP requests.
We also introduce a couple of methods that support exploring a site's Javascript environment from within Python:
page.window.new_keys() lists non-default global variables, and page.window[key] will access global variables.
page.browser.debug_show_page will dump a text version of the current page to the console (if html2text is
installed, otherwise the raw page source).
Installation
pip
pip install admitted, orpip install admitted[debug]to includehtml2textfor exploratory work, orpip install admitted[dev]for the development environment.
Requirement format for this git repo as a dependency
admitted @ git+https://git@git.accountingdatasolutions.com/indepndnt/admitted@main
Usage
Generally, the admitted API is intended to follow the
encouraged practice of page object models
by establishing a pattern of defining Page classes each with one initialization method that defines selectors for
all relevant elements on the page and one or more action methods defining the desired interaction with the page.
Define your Site
The Site is a special version of a Page object that defines your login page and the method to complete the login action. All other Page objects will have a reference to this for testing if you are authenticated and repeating the login if necessary.
The default behavior of Site.__init__ is to call the login method; this can be avoided by passing postpone=True
to Site.
from admitted import Site, Page
class MySite(Site):
def __init__(self):
# get login credentials from secure location
credentials = {
"username": "user",
"password": "do_not_actually_put_your_password_in_your_code",
}
super().__init__(
login_url="https://www.example.com/login",
credentials=credentials,
)
def _init_login(self):
self.username_selector = "input#username"
self.password_selector = "input#password"
self.submit_selector = "button#login"
def _do_login(self) -> "MySite":
self.css(self.username_selector).clear().send_keys(self.credentials["username"])
self.css(self.password_selector).clear().send_keys(self.credentials["password"])
self.css(self.submit_selector).click()
return self
def is_authenticated(self) -> bool:
return self.window["localStorage.accessToken"] is not None
Define a Page
The default behavior of Page.navigate is to call self.site.login on the first retry if navigation fails.
class MyPage(Page):
def __init__(self):
super().__init__(MySite())
self.navigate("https://www.example.com/interest")
def _init_page(self) -> None:
self.element_of_interest = "//div[@id='interest']"
self.action_button = "#action-btn"
def get_interesting_text(self) -> str:
element = self.xpath(self.element_of_interest)
return element.text
def do_page_action(self) -> None:
self.css(self.action_button).click()
Use your Page object
page = MyPage()
print(f"Received '{page.get_interesting_text()}'. Interesting!")
page.do_page_action()
print(f"Non-default global variables are {page.window.new_keys()}")
print(f"The document title is '{page.window['document.title']}'.")
response = page.window.fetch(method="post", url="/api/option", payload={"showInterest": True},
headers={"x-snowflake": "example-option-header"})
print(f"Fetch returned '{response.json}'.")
response = page.direct_request(method="get", url="https://www.google.com")
print(f"The length of Google's page source is {len(response.text)} characters.")
HTTP Response API
The Page.window.fetch and Page.direct_request methods both return a Response object with the following API.
contentproperty: Response body asbytes.textproperty: Response body asstr, orNone.jsonproperty: JSON parsed response body, orNone.htmlproperty:lxmlparsed HTML element tree, orNone.write_streammethod: Stream response data to the provided file pointer ifdirect_requestmethod was called withstream=True, otherwise writesResponse.content.destinationargument: file pointer for a file opened with a write binary mode.chunck_sizeargument: (optional) number of bytes to write at a time.- Returns
destination.
References
- Selenium Python bindings documentation
- Selenium project documentation
- lxml html parser documentation
Development
Configure pre-commit hooks to format, lint, and test code before commit.
.git/hooks/pre-commit
ln -s ./pre-commit.sh .git/hooks/pre-commit
Release
Run ./release.sh to increment the version number, push the release tag, and publish to PyPI.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file admitted-2025.2.tar.gz.
File metadata
- Download URL: admitted-2025.2.tar.gz
- Upload date:
- Size: 32.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
95f691a18a91c3e6d933b1e3478e2a2b4830095d60b2bee1726a65ea18c27e98
|
|
| MD5 |
bc201b2601ece3cc45cb96b8950501c0
|
|
| BLAKE2b-256 |
25fbeb1746d1c69eeacf47af32a7031a8b49108e92f494b9ece43bb71c9e8c42
|
File details
Details for the file admitted-2025.2-py3-none-any.whl.
File metadata
- Download URL: admitted-2025.2-py3-none-any.whl
- Upload date:
- Size: 26.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39cba79e9666bd3fc2c608c28950185a79e6e588a32cf4d2daed169700cd1f35
|
|
| MD5 |
f7694a98f262e84f1a031817ec2c1761
|
|
| BLAKE2b-256 |
7c8b2fa1a65a102bc4595f48db87cab0e1c783574bd8a4120379d72010aa1044
|