def build_options(browser, browser_options: List[str], experimental_options: Optional[List[dict]], extension_paths: Optional[List[str]]): """ Build the Options object for Chrome or Firefox. Args: browser: The name of the browser. browser_options: The list of options/arguments to include. experimental_options: The list of experimental options to include. extension_paths: The list of extensions to add to the browser session. Examples: WebDriverFactory().build_options('chrome', ['headless', 'incognito'], [{'useAutomationExtension', False}]) """ browser = browser.lower() if browser == Browser.CHROME: options = webdriver.ChromeOptions() elif browser == Browser.FIREFOX: options = webdriver.FirefoxOptions() elif browser == Browser.IE: options = webdriver.IeOptions() elif browser == Browser.OPERA: options = webdriver.ChromeOptions() elif browser == Browser.EDGE: options = Options() else: raise ValueError( f'{browser} is not supported. https://elsnoman.gitbook.io/pylenium/configuration/driver' ) for option in browser_options: if option.startswith('--'): options.add_argument(option) else: options.add_argument(f'--{option}') if experimental_options: for exp_option in experimental_options: (name, value), = exp_option.items() options.add_experimental_option(name, value) if extension_paths: for path in extension_paths: options.add_extension(path) return options
def _get_driver(): driver = None # trying with google chrome if driver is None: try: chromedriver_autoinstaller.install() driver = webdriver.Chrome() except: pass # trying with edge if driver is None: try: edgedriver_autoinstaller.install() driver_options = EdgeOptions() driver_options.add_experimental_option("excludeSwitches", ["enable-logging"]) driver = webdriver.Edge(executable_path="msedgedriver.exe", options=driver_options) except: pass # trying with firefox if driver is None: try: # TODO: firefoxdriver_autoinstaller.install(True); driver = webdriver.Firefox(executable_path="msedgedriver.exe") except: pass if (driver): driver.implicitly_wait(5) return driver else: raise Exception("Impossible to connect to a browser, download google chrome.")
def open_browser(executable_path="msedgedriver", edge_args=None, desired_capabilities=None, **kwargs): """Open Edge browser instance and cache the driver. Parameters ---------- executable_path : str (Default "msedgedriver") path to the executable. If the default is used it assumes the executable is in the $PATH. port : int (Default 0) port you would like the service to run, if left as 0, a free port will be found. desired_capabilities : dict (Default None) Dictionary object with non-browser specific capabilities only, such as "proxy" or "loggingPref". chrome_args : Optional arguments to modify browser settings """ options = Options() options.use_chromium = True # Gets rid of Devtools listening .... printing # other non-sensical error messages options.add_experimental_option('excludeSwitches', ['enable-logging']) if platform.system().lower() == "windows": options.set_capability("platform", "WINDOWS") if platform.system().lower() == "linux": options.set_capability("platform", "LINUX") if platform.system().lower() == "darwin": options.set_capability("platform", "MAC") # If user wants to re-use existing browser session then # he/she has to set variable BROWSER_REUSE_ENABLED to True. # If enabled, then web driver connection details are written # to an argument file. This file enables re-use of the current # chrome session. # # When variables BROWSER_SESSION_ID and BROWSER_EXECUTOR_URL are # set from argument file, then OpenBrowser will use those # parameters instead of opening new chrome session. # New Remote Web Driver is created in headless mode. edge_path = kwargs.get( 'edge_path', None) or BuiltIn().get_variable_value('${EDGE_PATH}') if edge_path: options.binary_location = edge_path if user.is_root() or user.is_docker(): options.add_argument("no-sandbox") if edge_args: if any('--headless' in _.lower() for _ in edge_args): CONFIG.set_value('Headless', True) for item in edge_args: options.add_argument(item.lstrip()) options.add_argument("start-maximized") options.add_argument("--disable-notifications") if 'headless' in kwargs: CONFIG.set_value('Headless', True) options.add_argument("--headless") if 'prefs' in kwargs: if isinstance(kwargs.get('prefs'), dict): prefs = kwargs.get('prefs') else: prefs = util.prefs_to_dict(kwargs.get('prefs').strip()) options.add_experimental_option('prefs', prefs) logger.warn("prefs: {}".format(prefs)) driver = Edge(BuiltIn().get_variable_value('${EDGEDRIVER_PATH}') or executable_path, options=options, capabilities=desired_capabilities) browser.cache_browser(driver) return driver