Skip to main content

No project description provided

Project description

EasySelenium

Table of Contents

  1. Overview
  2. Directory Structure
  3. Configuration
    1. Setting up the .env file
    2. Configuring a virutal environment
  4. Usage
    1. Web Driver Class
    2. Automatically Updating Chromedriver
  5. Contributing

Overview

The EasySelenium project is designed to make the Selenium module a little bit easier to use. It supports automatic chromedriver updates, and shorter syntax for finding_elements.

Modules

  • improved_webdriver.py: Contains the WebDriver class, which simplifies the use of Selenium WebDriver for web scraping. It includes methods for navigating web pages, finding elements with built-in EC delays, and passing Selenium content to BeautifulSoup.
  • update_drivers.py: Contains the update_chromedriver functions, which sends an HTML request, downloading the most recent version of chromedriver.exe in place, overwriting the existing chromedriver with the most recent version

Configuration

To easily download all repo code onto your local machine download the selenium-easy module from pip

pip install selenium-easy

Usage

WebDriver Class Usage

The WebDriver class is designed to simplify Selenium programming by reducing tedious syntax. Below are the usage instructions and examples.

Initialization

from improved_webdriver import WebDriver

# Initialize WebDriver with default settings
driver = WebDriver()

# Initialize WebDriver in headless mode
driver = WebDriver(headless=True)

# Initialize WebDriver with a custom memory structure
driver = WebDriver(memory_structure={})

#Initialize WebDriver, if unsuccesful try updating the chromedriver for windows and initialize again
driver = WebDriver(headless = True, platform = 'win64')

Parameters

  • headless: A boolean to determine whether the opened page will be visible. If True, the browser will run in headless mode (no GUI). Defaults to False.
  • memory_structure: A data structure to hold values for later use. Defaults to an empty list.
  • platform: In case of initialization failure, attempt to update chromedriver.exe by calling update_drivers.update_chromedriver(platform). Defaults to 'win64' The available platforms are:
    • linux64
    • mac-arm64
    • mac-x64
    • win32
    • win64

Methods

find_element_by_id

element = driver.find_element_by_id('element_id', wait=10, errors='raise')
  • id: The ID of the element to find.
  • wait: Time in seconds to wait for the element to be present. Defaults to None.
  • errors: Error handling strategy. If 'raise', an exception will be raised on error. If 'coerce', the error will be printed and the program will continue.

find_elements_by_id

elements = driver.find_elements_by_id('element_id', wait=10, errors='raise')
  • id: The ID of the elements to find.
  • wait: Time in seconds to wait for the elements to be present. Defaults to None.
  • errors: Error handling strategy. If 'raise', an exception will be raised on error. If 'coerce', the error will be printed and the program will continue.

EasySelenium can also be used to search by class, css selector, and by xpath with similar syntax, using:

  • find_element_by_class and find_elements_by_class to search by class
  • find_element_by_css and find_elements_by_cssto search by css selector
  • find_element_by_xpath and find_elements_by_xpath to search by xpath

get_soup

#Returns a BeautifulSoup html parser of the JS-rendered page. 
soup = driver.get_soup()

Example Usage

from improved_webdriver import WebDriver

with WebDriver(headless=True) as driver:
    driver.get('https://example.com')
    #This may not exist on the website, so in that case we'll just log that it wasn't found instead of halting the program
    element = driver.find_element_by_id('example_id', wait=10, errors='raise')
    print(element.text if element else "Element not found")
    #This is essential for the program to run, raise an error if not found
    elements = driver.find_elements_by_class('example_class', wait=10, errors='coerce')
    for elem in elements:
        print(elem.text)
    #Create a BeautifulSoup parser of the driver (post JS-rendering)
    soup = driver.get_soup()

Updating Chromedriver

The update_drivers.update_driver function allows you to download and update the chromedriver for different platforms.

Platforms

The available platforms are:

  • linux64
  • mac-arm64
  • mac-x64
  • win32
  • win64

Example Usage

from update_drivers import update_driver

# Update chromedriver for Windows 64-bit
update_driver('win64')

# Update chromedriver for macOS ARM64
update_driver('mac-arm64')

This will download the most recent chromedriver for the specified platform to the current working directory. It is an intended future update to customize the driver download/launch locations for each project.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.

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

seleniumeasy-0.1.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

seleniumeasy-0.1.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file seleniumeasy-0.1.0.tar.gz.

File metadata

  • Download URL: seleniumeasy-0.1.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for seleniumeasy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5a5785ccddc33e1b707dfe9e04ed17413bf73dd02dfd1464bd783aefe9380b60
MD5 2ade6830e4f6975561875172679305a1
BLAKE2b-256 1fc3d97ef32e7bc4a72801c523ec3cd60342c381055cd3b322abca4baa2713c2

See more details on using hashes here.

File details

Details for the file seleniumeasy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: seleniumeasy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for seleniumeasy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fd02b7cfe8a609f1cd5fb869abd9f584594d77018eaa50f2ec0bdc62db939be6
MD5 ce2c34ca104df3915419c51c9004bfdc
BLAKE2b-256 6cedd39f2854d28239851acf151a5cd0c47761e777027b066d0b61b020242ce7

See more details on using hashes here.

Supported by

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