Skip to main content

The package providing ways to interact with Selenium Web Driver actions, such as clicking, sending keys etc.

Project description

croco-selenium

Croco Logo

PyPi Version PyPI Downloads License Last Commit Development Status

The package providing ways to interact with Selenium Web Driver actions, such as clicking, sending keys etc.

When we use Selenium, it's not convenient to use WebDriverWait with its cluttered chain actions. Instead of many imports and instances (By, WebDriverWait, expected_conditions) you can use fast and robust actions.

Package's source code is made available under the MIT License

The project is made by the Croco Factory team

Quick Start

You can use actions, by the following way.

from selenium.webdriver.chrome.webdriver import WebDriver
from croco_selenium import click
driver = WebDriver()
click(driver, 10, '//input[@type="submit"]')

If you don't want to pass driver every using of actions, you can create an instance of ActionPerformer

from selenium.webdriver.chrome.webdriver import WebDriver
from croco_selenium import ActionPerformer
driver = WebDriver()
action_performer = ActionPerformer(driver)
timeout = 10

action_performer.send_keys(timeout, '//input[@type="password"]', 'password')
action_performer.click(timeout, '//input[@type="submit"]')

One of the best ways to use actions is create an instance of ChromeDriver and perform actions by calling methods on it. That class derived from ActionPerformer and ChromiumDriver

import os
from croco_selenium import ChromeDriver, Proxy
proxy = Proxy(host='123.89.46.72', port=8000, username='croco', password='webDriver')
project_dir = os.path.dirname(os.path.abspath(__file__))
extension_paths = [os.path.join(project_dir, 'extensions/metamask.crx')]

driver = ChromeDriver(proxy=proxy, extension_paths=extension_paths)
driver.get('https://facebook.com')
driver.send_keys(15, '//input[@id="email"]', 'hello@world.com')

Actions Overview

You can perform the following actions, using croco-selenium:

And there are 3 useful decorators:

Actions

add_cookies

Adds cookies to a current page. It takes valid string containing json, list of cookies or one cookie as dictionary.
from croco_selenium import ChromeDriver

timeout = 10
driver = ChromeDriver()

cookies = '{"domain":".facebook.com","expirationDate":1689249734,"httpOnly":true,"name":"c_user","path":"/","secure":true,"value":"100083466604886"}'
driver.get('facebook.com')
driver.add_cookies(cookies)

click

Clicks on element in browser
from croco_selenium import ChromeDriver

timeout = 10
driver = ChromeDriver()
driver.click(timeout, '//input[@type="submit"]')

close_tabs

Closes all tabs in browser. It's convenient to use, when you add extensions to your browser and their windows occur with starting a driver.
import os
from croco_selenium import ChromeDriver

project_dir = os.path.dirname(os.path.abspath(__file__))
extension_paths = [os.path.join(project_dir, 'extensions/metamask.crx')]

driver = ChromeDriver(extension_paths=extension_paths)
driver.close_tabs()
driver.get('https://facebook.com')
driver.send_keys(15, '//input[@id="email"]', 'hello@world.com')

get_element

Returns an element in browser
from croco_selenium import ChromeDriver

timeout = 10
driver = ChromeDriver()

element = driver.get_element(timeout, '//input[@type="submit"]')
element.click()

get_elements

Returns an elements in browser
from croco_selenium import ChromeDriver

timeout = 10
driver = ChromeDriver()

elements = driver.get_elements(timeout, '//input')

for element in elements:
    element.click()

get_element_attribute

Returns an element's attribute in browser
from croco_selenium import ChromeDriver

timeout = 10
driver = ChromeDriver()

assert driver.get_element_attribute(timeout, '//input[@type="checkbox"]', 'checked')

get_element_text

Returns element's text in browser
from croco_selenium import ChromeDriver

timeout = 10
driver = ChromeDriver()

print(driver.get_element_text(timeout, '//h1'))

send_keys

Sends keys in browser
from croco_selenium import ChromeDriver

timeout = 10
driver = ChromeDriver()

driver.send_keys(timeout, '//input[@type="password"]', 'password')
driver.click(timeout, '//input[@type="submit"]')

silent_send_keys

Sends keys with delay between characters in browser. It's convenient to use when you would like to hide your bot activity
from croco_selenium import ChromeDriver

timeout = 10
driver = ChromeDriver()

driver.silent_send_keys(timeout, '//input[@type="password"]', 'password')
driver.click(timeout, '//input[@type="submit"]')

switch_to_another_window

Switches to a different window from current window in browser. It's convenient to use, when you have two windows to be handled
from croco_selenium import ChromeDriver

timeout = 10
driver = ChromeDriver()

driver.silent_send_keys(timeout, '//input[@type="password"]', 'password')
driver.click(timeout, '//input[@type="submit"]')
driver.switch_to_another_window(timeout)

driver.click(timeout, '//input[@type="submit"]')
driver.switch_to_another_window(timeout)

switch_to_frame

Switches to the frame
from croco_selenium import ChromeDriver

timeout = 10
driver = ChromeDriver()

driver.switch_to_frame(timeout, '//iframe[@data-hcaptcha-widget-id]')
driver.click(timeout, '//input[@type="submit"]')

switch_to_parent_frame

Switches to the parent frame
from croco_selenium import ChromeDriver

timeout = 10
driver = ChromeDriver()

driver.switch_to_frame(timeout, '//iframe[@data-hcaptcha-widget-id]')
driver.click(timeout, '//input[@type="submit"]')
driver.switch_to_parent_frame()

wait_for_invisibility

Wait for element's invisibility in browser
from croco_selenium import ChromeDriver

timeout = 10
driver = ChromeDriver()

driver.click(timeout, '//button')
driver.wait_for_invisibility(timeout, '//*[@id="popup"]')

wait_for_windows

Wait for occurring of number of windows
from croco_selenium import ChromeDriver

timeout = 10
driver = ChromeDriver()

driver.wait_for_windows(timeout, 2)

Decorators

handle_pop_up

Switches to another window, performs decorated function and switches back. Pop up has to be closed after performing decorated function.

This decorator is usually used for browser extensions' pop-ups. Example of function performing a third-party Metamask connection:

from croco_selenium import ChromeDriver, handle_pop_up
from selenium.common import TimeoutException

@handle_pop_up()
def connect(driver: ChromeDriver, password: str) -> None:
    try:
        password_xpath = '//*[@id="password"]'
        driver.send_keys(7, password_xpath, password)

        unlock_xpath = '//button[@data-testid="unlock-submit"]'
        driver.click(5, unlock_xpath)
    except TimeoutException:
        pass

    for _ in range(3):
        next_xpath = '//button[@data-testid="page-container-footer-next"]'
        driver.click(10, next_xpath, ignored_exceptions=TimeoutException)

handle_in_new_tab

Opens new tab, performs decorated function, closes new tab and switches back. Here is example of function performing getting of 2FA code from browser extension.
from croco_selenium import ChromeDriver, handle_in_new_tab

@handle_in_new_tab()
def get_code(driver: ChromeDriver, account) -> str:
    timeout = 15

    driver.get('<EXTENSION_URL>')

    code_field_xpath = '//*[contains(@class, "code")]'
    code_fields = driver.get_elements(timeout, code_field_xpath)

    code_field = code_fields[account.id]

    code = code_field.text
    return code

handle_new_tab

Performs decorated function in new tab and switches back. New tab has to be opened during performing decorated function.

Installing croco-selenium

To install the package from PyPi you can use:

pip install croco-selenium

To install the package from GitHub you can use:

pip install git+https://github.com/CrocoFactory/croco-selenium.git

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

croco_selenium-0.1.6.post2.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

croco_selenium-0.1.6.post2-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file croco_selenium-0.1.6.post2.tar.gz.

File metadata

  • Download URL: croco_selenium-0.1.6.post2.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/24.0.0

File hashes

Hashes for croco_selenium-0.1.6.post2.tar.gz
Algorithm Hash digest
SHA256 1cbbdf6f45b555b213388db02fa18632550e59e7bbf5d0a0c370088c34697d28
MD5 94069d7f597b053605ec0f05eed11d89
BLAKE2b-256 a509e46c6c43324d00fbaf10db49a0f6300bf3ae3404ba34896c67b0f67a093c

See more details on using hashes here.

File details

Details for the file croco_selenium-0.1.6.post2-py3-none-any.whl.

File metadata

File hashes

Hashes for croco_selenium-0.1.6.post2-py3-none-any.whl
Algorithm Hash digest
SHA256 f6a64330e0964c019cc71a41f3676b3648e0dd3a083b38ca46c9b0cf4057e5fa
MD5 ecff108181c391a711b419a8e35c4aec
BLAKE2b-256 227f464ff3a72903b7b10627864ec4cfda980b7f44cb594fdd160c3fc9b47904

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