|
| 1 | +import atexit |
| 2 | +import json |
| 3 | +import os |
| 4 | +import tempfile |
| 5 | +from typing import Dict |
| 6 | + |
| 7 | +from undetected_chromedriver import Chrome # noqa: F401, F403 |
| 8 | +from undetected_chromedriver.options import ChromeOptions |
| 9 | +from undetected_chromedriver import Service as ChromeService # noqa: F401, F403 |
| 10 | +from selenium.webdriver.common.desired_capabilities import DesiredCapabilities |
| 11 | +from ..util import cleanup_temp_dir |
| 12 | + |
| 13 | + |
| 14 | +def default_options(headless=False, download_folder_path=None, user_data_dir=None, |
| 15 | + page_load_strategy="normal") -> ChromeOptions: |
| 16 | + """Retrieve the default options for this browser curated by BotCity. |
| 17 | +
|
| 18 | + Args: |
| 19 | + headless (bool, optional): Whether or not to use the headless mode. Defaults to False. |
| 20 | + download_folder_path (str, optional): The default path in which to save files. |
| 21 | + If None, the current directory is used. Defaults to None. |
| 22 | + user_data_dir ([type], optional): The directory to use as user profile. |
| 23 | + If None, a new temporary directory is used. Defaults to None. |
| 24 | + page_load_strategy (str, optional): The page load strategy. Defaults to "normal". |
| 25 | +
|
| 26 | + Returns: |
| 27 | + ChromeOptions: The Chrome options. |
| 28 | + """ |
| 29 | + chrome_options = ChromeOptions() |
| 30 | + try: |
| 31 | + page_load_strategy = page_load_strategy.value |
| 32 | + except AttributeError: |
| 33 | + page_load_strategy = page_load_strategy |
| 34 | + chrome_options.page_load_strategy = page_load_strategy |
| 35 | + chrome_options.add_argument("--remote-debugging-port=0") |
| 36 | + chrome_options.add_argument("--no-first-run") |
| 37 | + chrome_options.add_argument("--no-default-browser-check") |
| 38 | + chrome_options.add_argument("--disable-background-networking") |
| 39 | + chrome_options.add_argument("--disable-background-timer-throttling") |
| 40 | + chrome_options.add_argument("--disable-client-side-phishing-detection") |
| 41 | + chrome_options.add_argument("--disable-default-apps") |
| 42 | + chrome_options.add_argument("--disable-hang-monitor") |
| 43 | + chrome_options.add_argument("--disable-popup-blocking") |
| 44 | + chrome_options.add_argument("--disable-prompt-on-repost") |
| 45 | + chrome_options.add_argument("--disable-syncdisable-translate") |
| 46 | + chrome_options.add_argument("--metrics-recording-only") |
| 47 | + chrome_options.add_argument("--safebrowsing-disable-auto-update") |
| 48 | + |
| 49 | + # Disable What's New banner for new chrome installs |
| 50 | + chrome_options.add_argument("--disable-features=ChromeWhatsNewUI") |
| 51 | + |
| 52 | + chrome_options.add_argument("--disable-blink-features=AutomationControlled") |
| 53 | + |
| 54 | + # Disable banner for Browser being remote-controlled |
| 55 | + # chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) |
| 56 | + # chrome_options.add_experimental_option('useAutomationExtension', False) |
| 57 | + |
| 58 | + if headless: |
| 59 | + chrome_options.add_argument("--headless") |
| 60 | + chrome_options.add_argument("--disable-gpu") |
| 61 | + chrome_options.add_argument("--hide-scrollbars") |
| 62 | + chrome_options.add_argument("--mute-audio") |
| 63 | + |
| 64 | + # Check if user is root |
| 65 | + try: |
| 66 | + # This is only valid with Unix |
| 67 | + if os.geteuid() == 0: |
| 68 | + chrome_options.add_argument("--no-sandbox") |
| 69 | + except AttributeError: |
| 70 | + pass |
| 71 | + |
| 72 | + if not user_data_dir: |
| 73 | + temp_dir = tempfile.TemporaryDirectory(prefix="botcity_") |
| 74 | + user_data_dir = temp_dir.name |
| 75 | + atexit.register(cleanup_temp_dir, temp_dir) |
| 76 | + |
| 77 | + chrome_options.add_argument(f"--user-data-dir={user_data_dir}") |
| 78 | + |
| 79 | + if not download_folder_path: |
| 80 | + download_folder_path = os.getcwd() |
| 81 | + |
| 82 | + app_state = { |
| 83 | + 'recentDestinations': [{ |
| 84 | + 'id': 'Save as PDF', |
| 85 | + 'origin': 'local', |
| 86 | + 'account': '' |
| 87 | + }], |
| 88 | + 'selectedDestinationId': 'Save as PDF', |
| 89 | + 'version': 2 |
| 90 | + } |
| 91 | + |
| 92 | + # Set the Downloads default folder |
| 93 | + prefs = { |
| 94 | + "printing.print_preview_sticky_settings.appState": json.dumps(app_state), |
| 95 | + "download.default_directory": download_folder_path, |
| 96 | + "savefile.default_directory": download_folder_path, |
| 97 | + "printing.default_destination_selection_rules": { |
| 98 | + "kind": "local", |
| 99 | + "namePattern": "Save as PDF", |
| 100 | + }, |
| 101 | + "safebrowsing.enabled": True, |
| 102 | + "credentials_enable_service": False, |
| 103 | + "profile.password_manager_enabled": False, |
| 104 | + "plugins.always_open_pdf_externally": True |
| 105 | + } |
| 106 | + |
| 107 | + chrome_options.add_experimental_option("prefs", prefs) |
| 108 | + chrome_options.add_argument( |
| 109 | + "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) " |
| 110 | + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" |
| 111 | + ) |
| 112 | + |
| 113 | + chrome_options.add_argument("--kiosk-printing") |
| 114 | + |
| 115 | + return chrome_options |
| 116 | + |
| 117 | + |
| 118 | +def default_capabilities() -> Dict: |
| 119 | + """Fetch the default capabilities for this browser. |
| 120 | +
|
| 121 | + Returns: |
| 122 | + Dict: Dictionary with the default capabilities defined. |
| 123 | + """ |
| 124 | + return DesiredCapabilities.CHROME.copy() |
| 125 | + |
| 126 | + |
| 127 | +def wait_for_downloads(driver): |
| 128 | + """Wait for all downloads to finish. |
| 129 | + *Important*: This method overwrites the current page with the downloads page. |
| 130 | + """ |
| 131 | + if not driver.current_url.startswith("chrome://downloads"): |
| 132 | + driver.get("chrome://downloads/") |
| 133 | + return driver.execute_script(""" |
| 134 | + var items = document.querySelector('downloads-manager') |
| 135 | + .shadowRoot.getElementById('downloadsList').items; |
| 136 | + if (items.every(e => e.state === "COMPLETE")) |
| 137 | + return items.map(e => e.fileUrl || e.file_url); |
| 138 | + """) |
0 commit comments