Created
December 7, 2017 12:36
-
-
Save kowalcj0/6c5371fc641d13eb56da358ef63c0bd3 to your computer and use it in GitHub Desktop.
handy python enum for various browsers with some helpers that allow to toggle some hidden features
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from enum import Enum | |
from selenium.webdriver import ChromeOptions, FirefoxProfile | |
class Browser(Enum): | |
CHROME = "chrome" | |
FIREFOX = "firefox" | |
IE = "ie" | |
PHANTOMJS = "phantomjs" | |
EDGE = "edge" | |
SAFARI = "safari" | |
OPERA = "opera" | |
def __str__(self): | |
return self.name | |
def __eq__(self, y): | |
return self.value == y.value | |
def __hash__(self): | |
return id(self) | |
def chrome_disable_images(options: ChromeOptions): | |
prefs = {"profile.managed_default_content_settings.images": 2} | |
options.add_experimental_option("prefs", prefs) | |
return options | |
def firefox_disable_images(profile: FirefoxProfile) -> FirefoxProfile: | |
profile.set_preference('permissions.default.image', 2) | |
return profile | |
def firefox_disable_css(profile: FirefoxProfile) -> FirefoxProfile: | |
profile.set_preference('permissions.default.stylesheet', 2) | |
return profile | |
def firefox_disable_javascript(profile: FirefoxProfile) -> FirefoxProfile: | |
profile.set_preference('javascript.enabled', False) | |
return profile | |
def firefox_disable_flash(profile: FirefoxProfile) -> FirefoxProfile: | |
profile.set_preference( | |
'dom.ipc.plugins.enabled.libflashplayer.so', 'false') | |
return profile | |
def get_options( | |
browser: Browser, config: dict) -> ChromeOptions or FirefoxProfile: | |
if browser == Browser.CHROME: | |
options = ChromeOptions() | |
if config["environments"][0].get("disable_images", False): | |
chrome_disable_images(options) | |
return options | |
if browser == Browser.FIREFOX: | |
profile = FirefoxProfile() | |
if config["environments"][0].get("disable_images", False): | |
firefox_disable_images(profile) | |
return profile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment