DedInc / emunium

A Python module for automating interactions to mimic human behavior in standalone apps or browsers when using Selenium, Pyppeteer, or Playwright. Provides utilities to programmatically move the mouse cursor, click on page elements, type text, and scroll as if performed by a human user.
https://pypi.org/project/emunium/
MIT License
52 stars 5 forks source link
automation bypass-antibots bypass-bot-detection-systems captcha-bypass human-interactions playwright puppeteer pyppeteer selenium-automation selenium-test simulation standalone-automation web-testing

πŸ€– Emunium

A Python module for automating interactions to mimic human behavior in standalone apps or browsers when using Selenium, Pyppeteer, or Playwright. Provides utilities to programmatically move the mouse cursor, click on page elements, type text, and scroll as if performed by a human user.

Emunium preview

πŸš€ Quickstart (Standalone)

from emunium import Emunium

emunium = Emunium()

elements = emunium.find_elements('field.png', min_confidence=0.8)

emunium.type_at(elements[0], 'Automating searches')

elements = emunium.find_elements('search_icon.png', min_confidence=0.8)
emunium.click_at(elements[0])

πŸš€ Quickstart (with Selenium)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from emunium import EmuniumSelenium

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
emunium = EmuniumSelenium(driver)

driver.get('https://duckduckgo.com/')

element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[data-state="suggesting"]')))

emunium.type_at(element, 'Automating searches')

submit = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '[aria-label="Search"]')))
emunium.click_at(submit)

driver.quit()

πŸš€ Quickstart (with Pyppeteer)

import asyncio
from pyppeteer import launch
from emunium import EmuniumPpeteer

async def main():
    browser = await launch(headless=False)
    page = await browser.newPage()
    emunium = EmuniumPpeteer(page)

    await page.goto('https://duckduckgo.com/')

    element = await page.waitForSelector('[data-state="suggesting"]')
    await emunium.type_at(element, 'Automating searches')

    submit = await page.waitForSelector('[aria-label="Search"]')
    await emunium.click_at(submit)

    await browser.close()

asyncio.get_event_loop().run_until_complete(main())

πŸš€ Quickstart (with Playwright)

import asyncio
from playwright.async_api import async_playwright
from emunium import EmuniumPlaywright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        page = await browser.new_page()
        emunium = EmuniumPlaywright(page)

        await page.goto('https://duckduckgo.com/')

        element = await page.wait_for_selector('[data-state="suggesting"]')
        await emunium.type_at(element, 'Automating searches')

        submit = await page.wait_for_selector('[aria-label="Search"]')
        await emunium.click_at(submit)

        await browser.close()

asyncio.run(main())

πŸ–±οΈ Moving the Mouse

The move_to() method moves the mouse cursor smoothly to the provided element with small randomizations in speed and path to seem human.

Options:

πŸ–±οΈ Clicking Elements

The click_at() method moves via move_to() and clicks at the center of the provided element.

Emunium supports multiple mouse click types:

from emunium import ClickType

emunium.click_at(element)                   # left click
emunium.click_at(element, ClickType.RIGHT)  # right click  
emunium.click_at(element, ClickType.MIDDLE) # middle click
emunium.click_at(element, ClickType.DOUBLE) # double click

πŸ”Ž Finding Elements

In standalone mode, Emunium can locate elements on the screen using image matching with the find_elements method:

elements = emunium.find_elements('search_icon.png', min_confidence=0.8)

The find_elements method takes the following parameters:

The find_elements method returns a list of dictionaries, each containing the 'x' and 'y' coordinates of the center point of a matched element.

⌨️ Typing Text

The type_at() method moves to the provided element via move_to(), clicks it via click_to(), and types the provided text in a "silent" way, spreading out key presses over time with small randomizations to mimic human typing.

Options:

πŸ“œ Scrolling Pages

The scroll_to() method scrolls the page to bring the provided element into view using smooth scrolling.

Includes timeouts and checks to handle issues with scrolling getting stuck.

🏁 Conclusion

Emunium provides a set of utilities to help automate browser interactions in a more human-like way when using Selenium, Pyppeteer, or Playwright. By moving the mouse, clicking, typing, and scrolling in a less robotic fashion, tests can avoid detection and run more reliably.

While basic automation scripts can still get the job done, Emunium aims to make tests appear even more life-like. Using the randomizations and smooth behaviors it offers can be beneficial for automation projects that require avoiding detections.