def chrome_driver(executable_path=None, run_headless=False,
load_images=True, use_proxy=None):
'''Function to initialize ``selenium.webdriver.Chrome`` with extended options
Args:
executable_path (str): path to the chromedriver binary. If set to ``None`` selenium will serach for ``chromedriver`` in ``$PATH``.
run_headless (bool): boolean flag that indicates if chromedriver has to be headless (without GUI).
load_images (bool): boolean flag that indicates if Chrome has to render images.
use_proxy (str): use http proxy in <host:port> format.
Returns:
selenium.webdriver.Chrome: created driver.
Note:
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.
Warning:
Headless Chrome is shipping in Chrome 59 and in Chrome 60 for Windows. Update your Chrome browser if you want to use ``headless`` option.
'''
chrome_options = webdriver.ChromeOptions()
if run_headless:
chrome_options.add_argument('headless')
if not load_images:
prefs = {'profile.managed_default_content_settings.images': 2}
chrome_options.add_experimental_option('prefs', prefs)
if use_proxy:
chrome_options.add_argument('proxy-server=' + use_proxy)
if executable_path:
driver = webdriver.Chrome(chrome_options=chrome_options,
executable_path=executable_path)
else:
driver = webdriver.Chrome(chrome_options=chrome_options)
return driver
评论列表
文章目录