Last active
February 16, 2021 18:44
-
-
Save raphant/7f2481f42aef02bf7aa9031cd0083485 to your computer and use it in GitHub Desktop.
Selenium Snippets
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
# to make a headless browser | |
geckodriver_autoinstaller.install() | |
options = Options() | |
options.headless = True | |
driver = webdriver.Firefox(options=options) | |
def setup_driver(headless: bool): | |
""" | |
Only download the driver if not found on system as geckodriver_autoinstaller.install() will naively download | |
""" | |
gecko_path = shutil.which("geckodriver") or shutil.which("geckodriver.exe") | |
if not gecko_path: | |
try: | |
gecko_path = glob.glob('*/*geckodriver')[0] | |
except IndexError: | |
print('Installing geckodriver...') | |
gecko_path = geckodriver_autoinstaller.install(True) | |
print('Installed geckodriver to', gecko_path) | |
options = Options() | |
options.headless = headless | |
driver = webdriver.Firefox(options=options, executable_path=gecko_path) | |
return driver |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment