Skip to main content

A tool that makes working with selenium easier.

Project description

A list of functionalities that makes working with Selenium much easier.

Requirements

  • Chrome or Firefox
  • Python 3.7+

Installation

pip install easy-py-selenium

TOC






Create Undetectable Chrome Driver

easy-selenium will download and patch a Chrome driver to make it undetectable. So that you can use an undetectable Chrome driver for your Python Selenium code.

Features:

  • Download the exact chrome driver based on your OS and installed chrome version.

  • Remove browser control flag

  • Remove signature in javascript

  • Set User-Agent

  • Use maximum resolution

  • Run Chrome driver on headless mode.

  • Unmute the sounds of the browser.

Passed the antibot test

Usage


from easy_selenium.driver.chrome.driver import Driver
driver = Driver()
chrome = driver.create()
# Change useragent
driver.useragent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
chrome.get("https://selmi.tech")

Options

Load Profile

Launch Chrome with its default or custom profile so that you can use cookies and site preferences from that profile.

chrome = driver.create_driver(profile="")

Note: Make sure to provide the absolute path of your profile and if the given profile folder doesn't exist, it'll be created.

To find path to your chrome profile data you need to type chrome://version/ into address bar .

Headless

We can use a headless chrome browser to lower memory overhead and faster execution for the scripts that we write.

chrome = driver.create_driver(headless=False)





Remote browser (debugging mode)

It launches a new Chrome session from a new Terminal (cmd) window on a given port number and profile path. It allows you to control an existing Chrome session.

Features

  • Open Chrome Instance on debugging mode

  • Control an existing Chrome instance.

  • Save/ load Chrome profiles.

Usage

from easy_selenium.driver.chrome.driver import Remote
remote = Remote()
chrome = remote.create()

chrome.get("https://selmi.tech")

Options

Control an existing Chrome session

Make sure that the session is up and running. Remined:

  • Windows:
    • chrome.exe --remote-debugging-port=9000
  • Linux/ Mac:
    • /usr/bin/google-chrome --remote-debugging-port=9000
from easy_selenium.driver.chrome.driver import Remote
remote = Remote()
chrome = remote.create(control_existing_instance=True, debug_port=9000)

Launch a new Chrome session and save cookies in a folder (profile)

To use a custom profile, put the its path in profile_path, and make sure to use the absolute path.

from easy_selenium.driver.chrome.driver import Remote
remote = Remote()
chrome = remote.create(profile="C:\path")





Create Firefox Driver (Geckodriver)

easy-selenium will download and launch Geckodriver. But it will be detected as bot.

Features:

  • Download the exact the latest geckodriver.

  • Set User-Agent

  • Use maximum resolution

  • Run geckodriver on headless mode.

  • Unmute the sounds of the browser.

Usage


from easy_selenium.driver.firefox.driver import Driver
driver = Driver(binary_path)
firefox = driver.create()

firefox.get("https://selmi.tech")

Options

Load Profile

Launch Firefox with its default or custom profile so that you can use cookies and site preferences from that profile.

firefox = driver.create_driver(profile="")

Note: Make sure to provide the absolute path of your profile.

For Windows users, you can find your profile path by pressing the Windows Key key and then start typing: %APPDATA%\Mozilla\Firefox\Profiles\

Headless

We can use a headless Firefox browser to lower memory overhead and faster execution for the scripts that we write.

firefox = driver.create_driver(headless=False)





Easy authentication

Automate the login process to a list of websites. Including:

  • Linkedin

from easy_selenium.driver.chrome.driver import Driver
from easy_selenium.authentication.login.linkedin import Login
driver = Driver()
chrome = driver.create()
login = Login("example@gmail.com", "password")
login.start(chrome)
  • Google

from easy_selenium.driver.chrome.driver import Remote
from easy_selenium.authentication.login.google import Login
driver = Remote()
chrome = driver.create()
login = Login("example@gmail.com", "password")
login.start(chrome)





Cookies

Get and set cookies with the Selenium methods get_cookies() and add_cookie()

Example

Save cookies

from easy_selenium.cookies.cookies import Cookies
cookies = Cookies()
cookies.save(driver, url, cookies_file_name, cookies_folder_path=None)

Load cookies

from easy_selenium.cookies.cookies import Cookies
cookies = Cookies()
cookies.load(driver, url, cookies_file_name, cookies_folder_path=None)





Utility functions

A list of functions that you might need when you use Selenium.

Mimic real user input

Write texts in fields like a real human, send keys one by one.

from easy_selenium.common.funcs import send_keys_one_by_one
send_keys_one_by_one(controller, keys, min_delay=0.05, max_delay=0.25)

Driver safe quit

When using headless browser, it's important to make sure that there are no unnecessary driver left running. Set exit to False to avoid sys.exit() once the function is fired.

from easy_selenium.common.funcs import driver_safe_quit
driver_safe_quit(driver, exit=True)

Send emails

Many times I had to copy the same function in many Selenium projects.

from easy_selenium.common.funcs import send_email
send_email(message, sender, password, receiver)

Scrolling

Keep scrolling until the end of the page

from easy_selenium.common.funcs import scroll_until_the_end
scroll_until_the_end(driver)

Scroll to an element

from easy_selenium.common.funcs import scroll_to
scroll_to(driver, element)

Scroll from x position to y position

from easy_selenium.common.funcs import scroll_from_to
scroll_from_to(driver, x, y)

userAgent

from easy_selenium.common.funcs import get_user_agent
get_user_agent(driver)

Screenshots

Page Screenshot

from easy_selenium.common.funcs import page_screenshot
page_screenshot(driver, file_name)

Element Screenshot

from easy_selenium.common.funcs import element_screenshot
element_screenshot(driver, file_name)





Logging

To make logging simple, I'm using Loguru.

Loguru logo

Keep logging the same way:

from easy_selenium.logger import logger





Testing

I'm using Pytest. pytest requires: Python 3.7+ or PyPy3.

Installation

pip install -U pytest

Downlaod the project

git clone https://github.com/SelmiAbderrahim/easy-selenium
cd easy-selenium

Environment variables

To do all the testing, create a '.env' file in your working directory with these values:

LINKEDIN_EMAIL_ADDRESS=emial@example.com
LINKEDIN_PASSWORD=password

Otherwise uncomment the functions you want to exclude.

Run tests

pytest easy_selenium/tests/





Was it useful ?

then ⭐it.

Thanks.

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

easy py selenium-0.1.5.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

easy_py_selenium-0.1.5-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file easy py selenium-0.1.5.tar.gz.

File metadata

  • Download URL: easy py selenium-0.1.5.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for easy py selenium-0.1.5.tar.gz
Algorithm Hash digest
SHA256 7dc1ff51c52122f1cb48bc290efe870c15cc10988c6f128c9b9cb7f10a372b4d
MD5 a84a51e3dff199700341374af5903f17
BLAKE2b-256 47d69820dc995239cb17bf25e14910b7e7a9f5a9e5f1ca2e6f575bbe290cbe1c

See more details on using hashes here.

File details

Details for the file easy_py_selenium-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for easy_py_selenium-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4518f2567c24f3717b57bbdaf28032df6d6286666a17873fb887a913b1c0140a
MD5 dc3325abdde910dfaad844ea28646b5b
BLAKE2b-256 f489dd247af902c51f01aaf917ed374aeaeb1a2053cd20e9d356632cee4bbe4f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page