Skip to main content

chromeless

AWS lambda with selenium & python is powerful solution. Let's access this benefit easily!

  • Don't create lambda functions every time. Just create this once.
  • Write the method.
  • Selenium dynamically execute your script.

Example

# Write the method to a file. (NOT interactive mode.)
def get_title(self, url):
    self.get(url)
    return self.title

# Attach the method and then call it.
from chromeless import Chromeless
chrome = Chromeless()
chrome.attach(get_title)
print(chrome.get_title("https://google.com")) # Returns Google

You can also provide boto3 session object if you don't want to use default aws profile:

from chromeless import Chromeless
from boto3.session import Session

session = Session(aws_access_key_id='<YOUR ACCESS KEY ID>',
                  aws_secret_access_key='<YOUR SECRET KEY>',
                  region_name='<REGION NAME>')
# or
session = Session(profile_name='<YOUR_PROFILE_NAME>')
chrome = Chromeless(boto3_session=session)

Or also you can just set appropriate environment vars works with boto3, so it will auto detect your choice e.g.:

# In mac or linux
export AWS_DEFAULT_REGION=<your aws region>

# In windows
set AWS_DEFAULT_REGION=<your aws region>

Installing

  • git clone --depth 1 https://github.com/umihico/pythonista-chromeless.git chromeless && cd $_
  • sls deploy --region YOUR_REGION
  • pip install chromeless

That's it! Now run the example.py and confirm your selenium works in lambda functions!

Tips

  • Don't call selenium native methods directly. Solution is wrapping.
# BAD EXAMPLE
chrome = Chromeless()
chrome.get("https://google.com") # Not a attached method. AttributeError will be raised.
chrome.title # Same. AttributeError.

# SOLUTION
def wrapper(self, url):
    self.get(url)
    return self.title

chrome = Chromeless()
chrome.attach(wrapper)
print(chrome.wrapper("https://google.com")) # prints 'Google'.
print(chrome.wrapper("https://microsoft.com")) # But you can execute as many times as you want.
print(chrome.wrapper("https://apple.com")) # Arguments are adjustable each time.
  • Multiple methods are also attachable.
def login(self):
    self.get("https://example.com/login")
    self.find_element_by_id("username").send_keys("umihico")
    self.find_element_by_id("password").send_keys("password")
    self.find_element_by_name("submit").click()

def test1(self):
    self.login()
    self.get("https://example.com/")

def test2(self):
    self.login()
    self.get("https://example.com/logout")

chrome = Chromeless()
chrome.attach(login) # You can attach multiple methods too.
chrome.attach(test1) # It means you can also refactor the common code.
chrome.attach(test2)
print(chrome.test1())
print(chrome.test2())
  • To screenshot
# BAD EXAMPLE
def bad_wrapper(self):
  self.get("https://google.com")
  self.save_screenshot("screenshot.png")
  # There's no sense in saving files in AWS Lambda.

# SOLUTION
def good_wrapper(self):
  self.get("https://google.com")
  return self.get_screenshot_as_png()
  # return image in binary format.

chrome = Chromeless()
chrome.attach(good_wrapper)
png = chrome.good_wrapper()
# then write then image down locally.
with open("screenshot.png", 'wb') as f:
    f.write(png)

License

The project is licensed under the MIT license.

Metadata

Release files for chromeless 0.9.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for chromeless 0.9.1
File Interpreter ABI Platform
chromeless-0.9.1-py3-none-any.whl Python 3 none any Details

Release files / chromeless-0.9.1-py3-none-any.whl

Download URL chromeless-0.9.1-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a6bf5cf0a8a4d5395dea4efe8c7fdc6a1d07ec1ffd237c66b87720cfcc53de7b
BLAKE2b-256 checksum
How to use checksums
9ebc07861ebc177ffb3af6eeb62f4a1d997311597e93fb10c502d1de63df8376
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

Security for chromeless 0.9.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.9.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page