Skip to content

Instantly share code, notes, and snippets.

@Zalasyu
Created December 19, 2021 23:06
Show Gist options
  • Save Zalasyu/377a6f17357de8aa6d1eb2832ba43f1f to your computer and use it in GitHub Desktop.
Save Zalasyu/377a6f17357de8aa6d1eb2832ba43f1f to your computer and use it in GitHub Desktop.
Creates and configures a Chrome Webdriver.
# Author: Alec Moldovan
# Description: Creates and configures A Chrome webdriver.
# Import Standard packages
import pathlib
from loguru import logger
# Import third-party modules
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.proxy import Proxy, ProxyType
# Import Local Modules
import config # Contains login info and config parameters for driver
@logger.catch
def setup_driver():
""" "
Initializes and configs webdriver for Chrome
Parameters: None
Returns: Initialized, configured chrome web driver
"""
# Set up driver to do "performance logging" by adjsuting its desire_capabilities argument.
# Performance logging is not on by deafult -> Used to get "Timeline", "Network", and "Page" events.
capabilities = DesiredCapabilities.CHROME
# The version of Chome and chromedirver is IMPORTANT. There was a change in the logging feature around version 75.
capabilities["goog:loggingPrefs"] = {"performance": "ALL"} # chromedriver 75+
# Save login info to this file path for faster logging in next time.
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(
f"user-data-dir={pathlib.Path(__file__).parent.absolute().joinpath('chrome-profile')}"
)
# Setup an an unauthenicated proxy for Chrome Webdriver to operate from
chrome_options.add_argument('--proxy-server=%s' % config.HOSTNAME + ":" + config.PORT)
chrome_options.add_argument('ignore-certificate-errors')
# Initialize the Chrome Web Driver
logger.info("Creating a ChromeDriver object with the following arguements...")
driver = webdriver.Chrome(config.DRIVER_EXECUTABLE_PATH, options=chrome_options, desired_capabilities=capabilities)
# page loading time and wait time for page reload
driver.set_page_load_timeout(5)
driver.implicitly_wait(2)
return driver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment