drivers.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:selenium_extensions 作者: pythad 项目源码 文件源码
def firefox_driver(executable_path=None, run_headless=False,
                   load_images=True, use_proxy=None):
    '''Function to initialize ``selenium.webdriver.Firefox`` with extended options

    Args:
        executable_path (str): path to the ``geckdriver`` binary. If set to ``None`` selenium will search for ``geckdriver`` in ``$PATH``.
        run_headless (bool): boolean flag that indicates if ``geckodriver`` has to be headless (without GUI). ``geckodriver`` doesn't support native headless mode, that's why ``pyvirtualdisplay`` is used.
        load_images (bool): boolean flag that indicates if Firefox has to render images.
        use_proxy (str): use http proxy in <host:port> format.

    Returns:
        selenium.webdriver.Firefox: created driver.

    Note:
        In order to create Firefox driver Selenium requires `Firefox <https://www.mozilla.org/en-US/firefox/new/>`_ to be installed and `geckodriver <https://github.com/mozilla/geckodriver/releases>`_ to be downloaded.

    Note:
        Firefox doesn't support native headless mode. We use ``pyvirtualdisplay`` to simulate it. In order ``pyvirtualdisplay`` to work you need to install ``Xvfb`` package: ``sudo apt install xvfb``.
    '''
    firefox_profile = webdriver.FirefoxProfile()
    if run_headless:
        display = Display(visible=0, size=(1024, 768))
        display.start()
    else:
        display = None
    if not load_images:
        firefox_profile.add_extension(os.path.dirname(
            os.path.realpath(__file__)) +
            '/browser_extensions/firefox/quickjava-2.1.2-fx.xpi')
        # Prevents loading the 'thank you for installing screen'
        firefox_profile.set_preference(
            'thatoneguydotnet.QuickJava.curVersion', '2.1.2.1')
        # Turns images off
        firefox_profile.set_preference(
            'thatoneguydotnet.QuickJava.startupStatus.Images', 2)
        # Turns animated images off
        firefox_profile.set_preference(
            'thatoneguydotnet.QuickJava.startupStatus.AnimatedImage', 2)
    if use_proxy:
        _ = use_proxy.split(':')
        firefox_profile.set_preference('network.proxy.type', 1)
        firefox_profile.set_preference('network.proxy.http', _[0])
        firefox_profile.set_preference('network.proxy.http_port', int(_[1]))
        firefox_profile.set_preference('network.proxy.ssl', _[0])
        firefox_profile.set_preference('network.proxy.ssl_port', int(_[1]))
    if executable_path:
        driver = webdriver.Firefox(
            firefox_profile, executable_path=executable_path)
    else:
        driver = webdriver.Firefox(firefox_profile)
    driver.display = display
    return driver
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号