Skip to content

Instantly share code, notes, and snippets.

@raphant
Last active February 16, 2021 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raphant/7f2481f42aef02bf7aa9031cd0083485 to your computer and use it in GitHub Desktop.
Save raphant/7f2481f42aef02bf7aa9031cd0083485 to your computer and use it in GitHub Desktop.
Selenium Snippets
# 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