├── requirements.txt ├── pyproject.toml ├── autoselenium ├── __init__.py ├── download.py ├── utils.py ├── browsers.py └── driver.py ├── LICENSE ├── setup.py ├── README.md └── .gitignore /requirements.txt: -------------------------------------------------------------------------------- 1 | selenium 2 | # msedge-selenium-tools -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = [ 3 | "setuptools>=42", 4 | "wheel", 5 | "selenium>=3.141.0", 6 | "msedge-selenium-tools>=3.141.3" 7 | ] 8 | build-backend = "setuptools.build_meta" -------------------------------------------------------------------------------- /autoselenium/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from autoselenium.driver import Driver 4 | from autoselenium.download import download_driver, download_default_driver, get_version 5 | 6 | __author__ = 'saizk' 7 | 8 | __all__ = [ 9 | 'Driver', 10 | 'download_driver', 11 | 'download_default_driver', 12 | 'get_version' 13 | ] 14 | 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Sergio 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | with open("README.md", "r", encoding="utf-8") as fh: 4 | long_description = fh.read() 5 | 6 | setup( 7 | name="auto-selenium", 8 | version="1.0.1", 9 | author="saizk", 10 | author_email="sergioaizcorbe@hotmail.com", 11 | description="Python tool to automate the download of Selenium Web Drivers for all browsers", 12 | long_description=long_description, 13 | long_description_content_type="text/markdown", 14 | url="https://github.com/saizk/auto-selenium", 15 | project_urls={ 16 | "Bug Tracker": "https://github.com/saizk/auto-selenium/issues", 17 | }, 18 | classifiers=[ 19 | "Programming Language :: Python :: 3", 20 | 'Programming Language :: Python :: 3.6', 21 | 'Programming Language :: Python :: 3.7', 22 | 'Programming Language :: Python :: 3.8', 23 | 'Programming Language :: Python :: 3.9', 24 | "License :: OSI Approved :: MIT License", 25 | "Operating System :: OS Independent", 26 | ], 27 | install_requires=['selenium>=3.141.0', 'msedge-selenium-tools>=3.141.3', 'Jinja2>=3.0.2'], 28 | package_dir={".": ""}, 29 | packages=["autoselenium"], 30 | include_package_data=True, 31 | python_requires=">=3.6", 32 | ) 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # auto-selenium 2 | ![PyPI version](https://img.shields.io/pypi/v/auto-selenium) 3 | 4 | Auto-Selenium is a Python tool to automate the download of Selenium Web Drivers and store the profile sessions for the following browsers: 5 | * Google Chrome 6 | * Firefox 7 | * Opera 8 | * Brave 9 | * Edge (only for Windows) 10 | 11 | It utilizes Selenium and MSEdge tools 12 | 13 | ## Quickstart examples 14 | ### Installation 15 | ```Python 16 | pip install auto-selenium 17 | ``` 18 | 19 | ### Simple Usage 20 | ```Python 21 | from autoselenium import Driver 22 | 23 | driver = Driver('chrome') # downloads driver based on current version of the browser 24 | driver.get('https://www.google.com/') 25 | ``` 26 | 27 | ### Context Manager 28 | ```Python 29 | with Driver('brave', root='drivers') as driver: # equivalent to Selenium's WebDriver object 30 | driver.get('https://www.github.com/saizk') 31 | # Selenium Webdriver command examples 32 | driver.find_elements_by_tag_name('div') 33 | driver.refresh() 34 | ``` 35 | 36 | ### Download specific versions of each driver 37 | ```Python 38 | from autoselenium import download_driver, get_version 39 | 40 | download_driver('firefox', version='0.29.1', root='drivers') 41 | download_driver('opera', 'latest') 42 | 43 | cversion = get_version('brave', 'current') 44 | lversion = get_version('brave', 'latest') 45 | 46 | if cversion < lversion: 47 | print('You should update your browser!') 48 | ``` 49 | You can specify between 'current', 'latest' or input an specific version for a driver. 50 | 51 | ### Create your custom driver 52 | ```Python 53 | import autoselenium 54 | 55 | class TwitterBot(autoselenium.Driver): 56 | 57 | _URL = 'https://twitter.com' 58 | 59 | def __init__(self, *args, **kwargs): 60 | super().__init__(*args, **kwargs) 61 | self.logged = False 62 | 63 | def log(self): 64 | self.logged = True 65 | pass 66 | 67 | def scrape(self): 68 | pass 69 | ``` 70 | ## Contribute 71 | Would you like to contribute to this project? Here are a few starters: 72 | - Improve documentation 73 | - Add Testing examples 74 | - Bug hunts and refactor 75 | - Additional features/ More integrations 76 | - Phantom JS support 77 | - Implement default browser functions for Mac 78 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | .idea 131 | .pypirc 132 | 133 | drivers/ 134 | test.* 135 | 136 | -------------------------------------------------------------------------------- /autoselenium/download.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import os 4 | import shutil 5 | import urllib.request 6 | from urllib.error import HTTPError, URLError 7 | 8 | from autoselenium.utils import * 9 | 10 | __all__ = ['download_driver', 'download_default_driver', 'get_version'] 11 | 12 | 13 | def download_driver(driver, version='current', root='.'): 14 | if not os.path.exists(root): 15 | os.mkdir(root) 16 | 17 | driver_name = get_driver_name(driver) 18 | driver_path = root + f'/{driver_name}driver' + ('.exe' if platform == 'win32' else '') 19 | driver_path = Path(driver_path).absolute() 20 | 21 | if driver_path.exists(): 22 | return 23 | try: 24 | version = get_version(driver, version) 25 | url, filename = gen_url(driver, version) 26 | download_url(url, filename) 27 | extract(filename, root) 28 | fix_paths(driver, driver_path) 29 | assert driver_path.exists(), driver_path 30 | 31 | except (HTTPError, URLError): 32 | raise RuntimeError(f'Cannot download {driver} driver.\n' 33 | f'If the problem persist, download the driver manually.') 34 | 35 | return driver_path 36 | 37 | 38 | def download_default_driver(root='.'): 39 | return download_driver(get_os_default_browser(), root=root) 40 | 41 | 42 | def get_version(driver, version): 43 | if version == 'latest': 44 | return get_latest_version(driver) 45 | elif version == 'current': 46 | return get_current_version(driver) 47 | return parse_version(driver, version) 48 | 49 | 50 | def parse_version(driver, raw_version): 51 | # version = ''.join(filter(lambda char: char.isdigit() or char == '.', raw_version)) 52 | if driver == 'firefox': 53 | return 'v' + raw_version 54 | elif driver == 'opera': 55 | return 'v.' + raw_version 56 | return raw_version 57 | 58 | 59 | def download_url(url, filename): 60 | with urllib.request.urlopen(url) as response, open(filename, 'wb') as out_file: 61 | shutil.copyfileobj(response, out_file) 62 | 63 | 64 | def extract(filename, tgt_path): 65 | shutil.unpack_archive(filename, tgt_path) 66 | os.remove(filename) 67 | 68 | 69 | def fix_paths(driver, driver_path): 70 | if driver in ['chrome', 'brave']: 71 | os.remove(driver_path.parent / 'LICENSE.chromedriver') 72 | 73 | elif driver == 'opera': 74 | src_path = search_driver(browser=driver, root=driver_path.parent) 75 | shutil.move(src_path, driver_path) # moves driver and deletes extra dir 76 | shutil.rmtree(src_path.parent) 77 | 78 | elif driver == 'edge': 79 | shutil.rmtree(driver_path.parent / 'Driver_Notes') 80 | 81 | if platform in ['linux', 'darwin'] and driver in ['chrome', 'opera', 'brave']: 82 | os.chmod(driver_path, 755) # add permissions for linux/mac drivers 83 | -------------------------------------------------------------------------------- /autoselenium/utils.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from sys import platform 4 | from pathlib import Path 5 | 6 | import autoselenium.browsers as brw 7 | 8 | 9 | DRIVER_URLS = { 10 | 'chrome': 'https://chromedriver.storage.googleapis.com', 11 | 'firefox': 'https://github.com/mozilla/geckodriver/releases/download', 12 | 'opera': 'https://github.com/operasoftware/operachromiumdriver/releases/download', 13 | 'edge': 'https://msedgedriver.azureedge.net', 14 | 'brave': 'https://chromedriver.storage.googleapis.com', 15 | } 16 | 17 | 18 | def get_driver_name(driver): 19 | if driver == 'firefox': 20 | return 'gecko' 21 | elif driver == 'brave': 22 | return 'chrome' 23 | elif driver == 'edge': 24 | return 'msedge' 25 | return driver 26 | 27 | 28 | def gen_driver_url(driver, version, os, bits, format_): 29 | root = DRIVER_URLS[driver] 30 | if driver == 'chrome' or driver == 'brave': 31 | return f'{root}/{version}/chromedriver_{os}{bits}.{format_}' 32 | elif driver == 'firefox': 33 | return f'{root}/{version}/geckodriver-{version}-{os}{bits}.{format_}' 34 | elif driver == 'opera': 35 | return f'{root}/{version}/operadriver_{os}{bits}.{format_}' 36 | elif driver == 'edge': 37 | return f'{root}/{version}/edgedriver_{os}{bits}.{format_}' 38 | 39 | 40 | def gen_url(driver, version): 41 | bits = 64 42 | if platform == 'win32' and driver in ['chrome', 'brave']: 43 | bits = 32 44 | elif platform == 'darwin' and driver == 'firefox': 45 | bits = 'os' 46 | 47 | file_ext = 'zip' 48 | if platform in ['linux', 'darwin'] and driver == 'firefox': 49 | file_ext = 'tar.gz' 50 | 51 | url = gen_driver_url(driver, version, get_os(), bits, file_ext) 52 | filename = url.split('/')[-1] 53 | return url, filename 54 | 55 | 56 | def get_latest_version(browser): 57 | if browser in ['chrome', 'brave']: 58 | version = brw.get_chromium_latest_release(browser) 59 | elif browser in ['firefox', 'opera']: 60 | version = brw.get_github_latest_release(browser) 61 | elif browser == 'edge': 62 | version = brw.get_edge_latest_release() 63 | else: 64 | raise RuntimeError(f'Cannot get latest release for {browser} browser') 65 | return version 66 | 67 | 68 | def get_current_version(browser): 69 | if browser in ['chrome', 'brave']: 70 | version = brw.get_chromium_latest_release(driver=browser, current_version=True) 71 | elif browser in ['firefox', 'opera']: 72 | version = brw.get_github_latest_release(browser) 73 | elif browser == 'edge': 74 | version = brw.get_browser_version_windows(browser) 75 | else: 76 | raise RuntimeError(f'Cannot get the current version for {browser} browser') 77 | return version 78 | 79 | 80 | def search_driver(browser='*', root='.'): 81 | driver_name = get_driver_name(browser) 82 | ext = '.exe' if platform == 'win32' else '' 83 | for path in Path(root).rglob(f'{driver_name}driver{ext}'): 84 | return path.absolute() 85 | 86 | 87 | def get_os(): 88 | if platform == 'win32': 89 | return 'win' 90 | elif platform == 'darwin': 91 | return 'mac' 92 | return platform # linux 93 | 94 | 95 | def get_os_default_browser(): 96 | if platform == 'linux': 97 | return brw.get_linux_default_browser() 98 | elif platform == 'win32': 99 | return brw.get_windows_default_browser() 100 | elif platform == 'darwin': 101 | return brw.get_mac_default_browser() 102 | else: 103 | raise RuntimeError('Unknown operating system') 104 | -------------------------------------------------------------------------------- /autoselenium/browsers.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import json 4 | import urllib.request 5 | import subprocess 6 | from sys import platform 7 | 8 | 9 | LATEST_RELEASES = { 10 | 'chrome': 'https://chromedriver.storage.googleapis.com/LATEST_RELEASE', 11 | 'firefox': 'https://api.github.com/repos/mozilla/geckodriver/releases/latest', 12 | 'opera': 'https://api.github.com/repos/operasoftware/operachromiumdriver/releases/latest', 13 | 'edge': 'https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/' 14 | } 15 | LATEST_RELEASES['brave'] = LATEST_RELEASES['chrome'] 16 | 17 | BROWSER_REGISTRY_PATHS = { 18 | 'chrome': r'Software\Google\Chrome\BLBeacon', 19 | 'firefox': r'Software\Mozilla\Mozilla Firefox', 20 | 'edge': r'Software\Microsoft\Edge\BLBeacon', 21 | 'brave': r'Software\BraveSoftware\Brave-Browser\BLBeacon' 22 | } 23 | 24 | MAC_BROWSER_PATHS = { 25 | 'chrome': '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', 26 | 'firefox': '/Applications/Firefox.app/Contents/MacOS/firefox', 27 | 'opera': '/Applications/Opera.app/Contents/MacOS/Opera', 28 | 'brave': '/Applications/Brave Browser.app/Contents/MacOS/Brave Browser' 29 | } 30 | 31 | 32 | def get_chromium_latest_release(driver, current_version=False): 33 | api_path = LATEST_RELEASES[driver] 34 | 35 | if current_version: 36 | version = get_browser_version(driver) 37 | api_path += f'_{version.split(".")[0]}' if version else '' 38 | 39 | with urllib.request.urlopen(api_path) as response: 40 | version = response.readlines()[0].decode('utf-8') 41 | 42 | return version 43 | 44 | 45 | def get_browser_version(browser): 46 | if platform == 'linux': 47 | with subprocess.Popen(['chromium-browser', '--version'], stdout=subprocess.PIPE) as proc: 48 | process = proc.stdout.read().decode('utf-8') 49 | version = ''.join(filter(lambda char: char.isdigit() or char == '.', process)).strip() 50 | elif platform == 'darwin': 51 | process = subprocess.Popen([MAC_BROWSER_PATHS[browser], '--version'], 52 | stdout=subprocess.PIPE).communicate()[0].decode('UTF-8') 53 | version = ''.join(filter(lambda char: char.isdigit() or char == '.', process)).strip() 54 | else: # win32 55 | version = get_browser_version_windows(browser) 56 | return version 57 | 58 | 59 | def get_browser_version_windows(browser): 60 | assert platform == 'win32' 61 | import winreg 62 | try: 63 | key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, BROWSER_REGISTRY_PATHS[browser], 0, winreg.KEY_READ) 64 | name = 'version' if browser != 'firefox' else 'CurrentVersion' 65 | version = winreg.QueryValueEx(key, name)[0] 66 | except KeyError: 67 | raise RuntimeError(f'Cannot detect current version for {browser.title()} browser') 68 | return version 69 | 70 | 71 | def get_github_latest_release(browser): # for firefox and opera 72 | api_path = LATEST_RELEASES[browser] 73 | with urllib.request.urlopen(api_path) as response: 74 | version = json.loads(response.read())['tag_name'] 75 | return version 76 | 77 | 78 | def get_edge_latest_release(): 79 | api_path = LATEST_RELEASES['edge'] 80 | with urllib.request.urlopen(api_path) as response: 81 | html_str = response.readlines()[-1].decode('utf-8') 82 | 83 | versions_idx = html_str.find('Version: ') 84 | final_idx = html_str[versions_idx:].find(f'OS {platform}') + versions_idx 85 | init_idx = html_str[:final_idx].rfind('version') + 7 86 | 87 | version = html_str[init_idx: final_idx].strip() 88 | 89 | return version 90 | 91 | 92 | def get_linux_default_browser(): 93 | assert platform == 'linux' 94 | try: 95 | import re, subprocess 96 | def_list = subprocess.check_output(['cat', '/usr/share/applications/defaults.list']).decode('utf-8') 97 | default_browser = re.findall("http=([^\.]*)", def_list)[0].split('-')[0] 98 | 99 | if default_browser == 'google': 100 | default_browser = 'chrome' 101 | 102 | except Exception as e: 103 | return 'firefox' # linux default 104 | 105 | return default_browser 106 | 107 | 108 | def get_windows_default_browser(): 109 | assert platform == 'win32' 110 | try: 111 | import winreg 112 | key = winreg.OpenKey(winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER), 113 | r'Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice') 114 | prog_id, _ = winreg.QueryValueEx(key, 'ProgId') 115 | key = winreg.OpenKey(winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE), 116 | rf'SOFTWARE\Classes\{prog_id}\shell\open\command') 117 | launch_string, _ = winreg.QueryValueEx(key, '') # read the default value 118 | def_browser_path = launch_string.lower().rstrip() 119 | 120 | for key in ['chrome', 'firefox', 'opera', 'edge', 'brave']: 121 | if key in def_browser_path: 122 | return key 123 | 124 | raise RuntimeError(f'Default browser not found or not supported: {def_browser_path}') 125 | 126 | except FileNotFoundError: # Just when Opera default browser 127 | return 'opera' 128 | 129 | 130 | def get_mac_default_browser(): 131 | assert platform == 'darwin' 132 | return NotImplementedError('Mac default browser function not implemented yet') 133 | 134 | -------------------------------------------------------------------------------- /autoselenium/driver.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import time 4 | from sys import platform 5 | from pathlib import Path 6 | 7 | from selenium.webdriver import Chrome, Firefox, Edge, Remote#, Opera 8 | 9 | from selenium.common.exceptions import SessionNotCreatedException 10 | 11 | 12 | class Driver(object): 13 | 14 | _BRAVE_PATHS = { 15 | 'win32': 'C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe', 16 | 'linux': '/opt/brave.com/brave/brave-browser', 17 | 'darwin': '/Applications/Brave Browser.app/Contents/MacOS/Brave Browser' 18 | } 19 | XPATH_SELECTORS = {} 20 | CSS_SELECTORS = {} 21 | 22 | def __init__(self, 23 | browser: str, 24 | root: str = '.', 25 | driver_path=None, 26 | profile_path=None, 27 | driver_options=None, 28 | brave_path=None, 29 | headless=False, 30 | kiosk=False, 31 | proxy=None, 32 | command_executor=None): 33 | """ 34 | :param (str) browser: Driver browser (i.e. 'chrome', 'firefox', 'edge', 'brave', 'opera'). 35 | :param (str) root: Root path for the driver executable and profiles. 36 | :param (str) driver_path: Path for an existing driver. If empty it will search for the driver based on its name. 37 | :param (str) profile_path: Path for an existing profile. If empty it will create one new. 38 | :param (selenium.webdriver.browser.options) driver_options: Custom Selenium Options for the driver. 39 | :param (str) brave_path: Path for a custom installation path of Brave Browser. 40 | :param (bool) headless: Activate headless mode 41 | :param (bool) kiosk: Activate kiosk mode (does not work on Opera) 42 | :param (str) proxy: Set a proxy (format -> 'proxy:port') 43 | :param (str) command_executor: Command executor for remote Selenium driver. 44 | """ 45 | 46 | self.browser = browser.lower() 47 | self.options = driver_options 48 | self.headless = headless 49 | self.kiosk = kiosk 50 | 51 | self.proxy = proxy 52 | self.command_executor = command_executor 53 | 54 | if not driver_path: 55 | driver_path = self._search_driver(self.browser, root) 56 | if not driver_path: 57 | driver_path = self._download_driver(self.browser, root=root) 58 | self.driver_path = (Path(driver_path).absolute()) 59 | 60 | if not profile_path: 61 | profile_path = f'{self.driver_path.parent}/{self.browser}-profile' 62 | self.profile_path = Path(profile_path).absolute() 63 | 64 | if not brave_path: 65 | self.brave_path = self._BRAVE_PATHS[platform] 66 | else: 67 | self.brave_path = Path(brave_path).absolute() 68 | 69 | try: 70 | if driver_options is None: 71 | self.options = self._create_options() 72 | 73 | self.driver = self._create_selenium_driver(self.options) 74 | 75 | except OSError as e: 76 | raise RuntimeError(f'Incorrect executable path for {self.browser} driver: {e}') 77 | except SessionNotCreatedException as e: 78 | raise RuntimeError(f'{e} -> Cannot create {self.browser} process with the current driver.\n' 79 | f'Update your browser to the latest version or download the specific driver') 80 | 81 | @staticmethod 82 | def _download_driver(browser, root): 83 | from autoselenium.download import download_driver 84 | return download_driver(browser, root=root) 85 | 86 | @staticmethod 87 | def _search_driver(driver, root): 88 | from autoselenium.utils import search_driver 89 | return search_driver(driver, root=root) 90 | 91 | def __enter__(self): 92 | return self.driver 93 | 94 | def __exit__(self, *args): 95 | return self.quit() 96 | 97 | def _create_selenium_driver(self, options): 98 | 99 | if self.command_executor: # remote driver 100 | driver = Remote(command_executor=self.command_executor, options=options) 101 | 102 | elif self.browser in ['chrome', 'brave']: 103 | from selenium.webdriver.chrome.service import Service as ChromeService 104 | driver = Chrome(service=ChromeService(str(self.driver_path)), options=options) 105 | 106 | # elif self.browser == 'opera': 107 | # driver = Opera(executable_path=self.driver_path, options=options) 108 | 109 | elif self.browser == 'edge': 110 | from selenium.webdriver.edge.service import Service as EdgeService 111 | driver = Edge(service=EdgeService(str(self.driver_path)), options=options) 112 | 113 | elif self.browser == 'firefox': 114 | from selenium.webdriver.firefox.service import Service as FirefoxService 115 | driver = Firefox(service=FirefoxService(str(self.driver_path)), options=options) 116 | 117 | else: 118 | raise RuntimeError(f'Cannot load Selenium driver for {self.browser}') 119 | 120 | return driver 121 | 122 | def _create_options(self): 123 | 124 | if self.browser in ['chrome', 'brave']: 125 | from selenium.webdriver.chrome.options import Options as ChromeOptions 126 | options = ChromeOptions() 127 | if self.browser == 'brave': 128 | options.binary_location = self.brave_path 129 | 130 | elif self.browser == 'firefox': 131 | from selenium.webdriver.firefox.options import Options as FirefoxOptions 132 | options = FirefoxOptions() 133 | options.set_preference('dom.webnotifications.enabled', False) 134 | 135 | # elif self.browser == 'opera': 136 | # from selenium.webdriver.opera.options import Options as OperaOptions 137 | # options = OperaOptions() 138 | 139 | elif self.browser == 'edge': 140 | from msedge.selenium_tools import EdgeOptions 141 | options = EdgeOptions() 142 | options.use_chromium = True 143 | options._ignore_local_proxy = True 144 | 145 | else: 146 | raise RuntimeError(f'Unknown browser {self.browser}') 147 | 148 | options.headless = self.headless 149 | if self.kiosk: # not supported on opera driver 150 | if self.browser == 'opera': print('Opera does not work on kiosk mode') 151 | options.add_argument('--kiosk') 152 | 153 | if self.browser != 'firefox': 154 | options.add_argument(f'user-data-dir={self.profile_path}') 155 | options.add_argument('--disable-notifications') 156 | 157 | if self.proxy is not None: 158 | options = self._set_proxy(options) 159 | 160 | return options 161 | 162 | def _set_proxy(self, options): 163 | if self.browser != 'firefox': 164 | options.add_argument(f'--proxy-server={self.proxy}') 165 | return options 166 | return self._set_firefox_proxy(options) 167 | 168 | def _set_firefox_proxy(self, options): 169 | proxy_address, proxy_port = self.proxy.split(':') 170 | options.set_preference('network.proxy.type', 1) 171 | options.set_preference('network.proxy.http', proxy_address) 172 | options.set_preference('network.proxy.http_port', int(proxy_port)) 173 | options.set_preference('network.proxy.ssl', proxy_address) 174 | options.set_preference('network.proxy.ssl_port', int(proxy_port)) 175 | return options 176 | 177 | @staticmethod 178 | def retry(func, sleep=0.5, *args, **kwargs): 179 | time.sleep(sleep) 180 | func(*args, **kwargs) 181 | 182 | def get(self, url): 183 | return self.driver.get(url) 184 | 185 | def refresh(self): 186 | self.driver.refresh() 187 | 188 | def quit(self): 189 | self.driver.quit() 190 | --------------------------------------------------------------------------------