- Chrome or Firefox
- Python 3.7+
pip install easy-py-selenium
- Create Undetectable Chrome Driver
- Remote browser (debugging mode)
- Create Firefox Driver (Geckodriver)
- Easy authentication
- Cookies
- Utility functions
- Logging
- Testing
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.
-
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.
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")
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)
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.
-
Open Chrome Instance on debugging mode
-
Control an existing Chrome instance.
-
Save/ load Chrome profiles.
from easy_selenium.driver.chrome.driver import Remote
remote = Remote()
chrome = remote.create()
chrome.get("https://selmi.tech")
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")
easy-selenium will download and launch Geckodriver. But it will be detected as bot.
-
Download the exact the latest geckodriver.
-
Set User-Agent
-
Use maximum resolution
-
Run geckodriver on headless mode.
-
Unmute the sounds of the browser.
from easy_selenium.driver.firefox.driver import Driver
driver = Driver(binary_path)
firefox = driver.create()
firefox.get("https://selmi.tech")
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)
Automate the login process to a list of websites. Including:
from easy_selenium.driver.chrome.driver import Driver
from easy_selenium.authentication.login.linkedin import Login
driver = Driver()
chrome = driver.create()
login = Login("[email protected]", "password")
login.start(chrome)
from easy_selenium.driver.chrome.driver import Remote
from easy_selenium.authentication.login.google import Login
driver = Remote()
chrome = driver.create()
login = Login("[email protected]", "password")
login.start(chrome)
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)
A list of functions that you might need when you use Selenium.
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)
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)
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)
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)
from easy_selenium.common.funcs import get_user_agent
get_user_agent(driver)
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)
To make logging simple, I'm using Loguru.
Keep logging the same way:
from easy_selenium.logger import logger
I'm using Pytest. pytest requires: Python 3.7+ or PyPy3.
pip install -U pytest
git clone https://github.com/SelmiAbderrahim/easy-selenium
cd easy-selenium
To do all the testing, create a '.env' file in your working directory with these values:
[email protected]
LINKEDIN_PASSWORD=password
Otherwise uncomment the functions you want to exclude.
Run tests
pytest easy_selenium/tests/
Was it useful ?
then ⭐it.
Thanks.