Selenium setup to configure your webdriver, handling web browser options, exceptions and logging.
MIT License
This library provides a flexible and customizable setup for managing Selenium WebDriver instances. It supports multiple browsers (Chrome, Firefox, Edge, and Internet Explorer) and includes the ability to customize browser options, handle WebDriver errors, and retry failed operations.
Clone the repository:
git clone https://github.com/The-Deep-Hub/selenium-setup.git
Install the required dependencies:
pip install -r requirements.txt
Dependencies include:
config/logging.ini
file is properly configured for logging.The library provides a flexible interface for setting up and customizing Selenium WebDriver instances. It supports different browsers and custom options, such as proxy settings, headless mode, and window size.
*Example: Basic Chrome WebDriver Setup * You can create and configure a WebDriver for Chrome:
from selenium_drivers.driver_factory import get_driver
# Create a basic Chrome WebDriver with default options
driver = get_driver('chrome')
# Navigate to a website
driver.get('https://www.example.com')
You can use the following browsers by specifying the browser type in the get_driver() function:
You can pass additional WebDriver options, such as enabling headless mode, setting proxy, or window size, through the get_driver
function:
from selenium_drivers.driver_factory import get_driver
# Example: Setting custom options for Chrome WebDriver
driver = get_driver(
'chrome',
proxy="http://myproxy:8080",
headless=True,
window_size="1920,1080"
)
This enables the dynamic configuration of the WebDriver for each browser, giving you control over browser behaviors.
driver = get_driver(
'firefox',
proxy="http://anotherproxy:8080",
headless=False,
disable_extensions=True
)
The WebDriver can be customized with the following options:
Check the WebDriverSetup class in selenium_drivers.webdriver_setup where this list of options is defined.
The library includes centralized logging configured via logging.ini. By default, logs are written to a scraper.log file, but this can be customized via the config/logging.ini file.
The logging configuration can be easily customized:
To change the logging configuration, edit config/logging.ini:
[loggers]
keys=root
[handlers]
keys=fileHandler
[formatters]
keys=formatter
[logger_root]
level=INFO
handlers=fileHandler
[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=formatter
args=('scraper.log', 'a')
[formatter_formatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
The project follows a modular structure, allowing flexibility in the extension of browser-specific functionality.
selenium_drivers/
│
├── __init__.py # Package initialization
├── base_driver_setup.py # Abstract base class for WebDriver setups
├── driver_factory.py # Factory for initializing WebDriver instances
├── webdriver_setup.py # Core WebDriver setup class
│
└── config/
└── logging.ini # Logging configuration
If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request. Check contributing.md for more details.
This project is licensed under the Apache License, Version 2.0. See the LICENSE file for more details.