How to use 'selenium headless chrome python' in Python

Every line of 'selenium headless chrome python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
7def chrome_driver(executable_path=None, run_headless=False,
8 load_images=True, use_proxy=None):
9 '''Function to initialize ``selenium.webdriver.Chrome`` with extended options
10
11 Args:
12 executable_path (str): path to the chromedriver binary. If set to ``None`` selenium will serach for ``chromedriver`` in ``$PATH``.
13 run_headless (bool): boolean flag that indicates if chromedriver has to be headless (without GUI).
14 load_images (bool): boolean flag that indicates if Chrome has to render images.
15 use_proxy (str): use http proxy in <host:port> format.
16
17 Returns:
18 selenium.webdriver.Chrome: created driver.
19
20 Note:
21 In order to create Chrome driver Selenium requires `Chrome <https://www.google.com/chrome/browser/desktop/index.html>`_ to be installed and `chromedriver <https://sites.google.com/a/chromium.org/chromedriver/>`_ to be downloaded.
22
23 Warning:
24 Headless Chrome is shipping in Chrome 59 and in Chrome 60 for Windows. Update your Chrome browser if you want to use ``headless`` option.
25 '''
26 chrome_options = webdriver.ChromeOptions()
27 if run_headless:
28 chrome_options.add_argument('headless')
29 if not load_images:
30 prefs = {'profile.managed_default_content_settings.images': 2}
31 chrome_options.add_experimental_option('prefs', prefs)
32 if use_proxy:
33 chrome_options.add_argument('proxy-server=' + use_proxy)
34 if executable_path:
35 driver = webdriver.Chrome(chrome_options=chrome_options,
36 executable_path=executable_path)
37 else:
38 driver = webdriver.Chrome(chrome_options=chrome_options)
39 return driver
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code


Related snippets