Coder Social home page Coder Social logo

umihico / pythonista-chromeless Goto Github PK

View Code? Open in Web Editor NEW
50.0 4.0 10.0 103 MB

Serverless selenium which dynamically execute any given code.

Home Page: https://pypi.org/project/chromeless/

License: MIT License

Python 90.04% Dockerfile 9.71% Shell 0.25%
selenium scraping webtester chromeless serverless integration-testing headless-chrome headless chrome python

pythonista-chromeless's Introduction

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)

The project is licensed under the MIT license.

pythonista-chromeless's People

Contributors

faizanalhassan avatar umihico avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pythonista-chromeless's Issues

Can't install PIL on lambda.

tried to be able to import 'time' , 'io' and 'PIL' by Chromeless.activate_library() method, but I realized even simple import of PIL fail somehow in this environment. saved in another branch and gave up so far about this problem

binascii.Error: Incorrect padding

First of all - Thank you very much for your effort on this! This is awesome.

I did exactly what the readme told me, but when running the function in examples.py I get an error:

Traceback (most recent call last):
File "/Users/enrico.semrau/PycharmProjects/pythonista-chromeless/pypi/chromeless/chromeless/client_pickler.py", line 19, in _unpickle_result
return pickle.loads(zlib.decompress(base64.b64decode(base64str_data.encode())))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/base64.py", line 87, in b64decode
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding

When checking the cloudwatch logs there is an error about:
[ERROR] UnboundLocalError: local variable 'screenshothandler' referenced before assignment Traceback (most recent call last): File "/var/task/lambda_function.py", line 47, in lambda_handler result = screenshothandler.wrap_with_screenshots(result)
[ERROR] UnboundLocalError: local variable 'screenshothandler' referenced before assignment
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 47, in lambda_handler
result = screenshothandler.wrap_with_screenshots(result)

------server printing error start------
File "/var/task/lambda_function.py", line 23, in lambda_handler
if event["httpMethod"] == "POST":
KeyError: 'httpMethod'

Can you help me here?

gateway api time out after 30 seconds

some method requires more than 30 seconds.
also, chrome and selenium try save memory if lambda limit it, but it increase time consumption instead.

WebDriverException: Chrome Not Reachable

When invoking a lambda repeatedly, after allowing for a cooldown, I frequently receive this error. It seems to have some randomness to it. For example, 10 invocations in rapid succession might all succeed, but if I wait 1-2 minutes and try 10 more, half of them will fail due to this error. Hoping someone else has seen this before and has some guidance.

[ERROR] WebDriverException: Message: chrome not reachable

Using the default chrome settings provided here and using selenium to navigate to a few different pages with short waits in between.

Unable to attach function

Followed exact steps given in readme to run the code.

But got this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\repos\chromeless\chromeless\chromeless.py", line 24, in attach
    self.codes[method.__name__] = inspect.getsource(
  File "C:\Users\faizan\AppData\Local\Programs\Python\Python39\lib\inspect.py", line 1024, in getsource
    lines, lnum = getsourcelines(object)
  File "C:\Users\faizan\AppData\Local\Programs\Python\Python39\lib\inspect.py", line 1006, in getsourcelines
    lines, lnum = findsource(object)
  File "C:\Users\faizan\AppData\Local\Programs\Python\Python39\lib\inspect.py", line 835, in findsource
    raise OSError('could not get source code')
OSError: could not get source code

I have not used sls earlier, just deployed to lambda directly, so maybe some steps are not written for devs who are unfamiliar with sls.

Please don't provide ImageUri when updating a function with packageType Zip.

 Serverless Error ---------------------------------------

 An error occurred: ServerLambdaFunction - Please don't provide ImageUri when updating a function with packageType Zip. (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException; Request ID: xyz; Proxy: null).

 Get Support --------------------------------------------
    Docs:          docs.serverless.com
    Bugs:          github.com/serverless/serverless/issues
    Issues:        forum.serverless.com

 Your Environment Information ---------------------------
    Operating System:          darwin
    Node Version:              15.5.0
    Framework Version:         2.23.0 (local)
    Plugin Version:            4.4.3
    SDK Version:               2.3.2
    Components Version:        3.6.2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.