webdriver mange用法-告别手动下载driver

下载第三方库

https://pypi.org/project/webdriver-manager/

webdriver mange用法-告别手动下载driver

 

pip install webdriver-manager

Chrome用法

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

Firefox用法

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

IE用法

from selenium import webdriver
from webdriver_manager.microsoft import IEDriverManager

driver = webdriver.Ie(IEDriverManager().install())

Edge用法

from selenium import webdriver
from webdriver_manager.microsoft import EdgeChromiumDriverManager

driver = webdriver.Edge(EdgeChromiumDriverManager().install())

Opera用法

from selenium import webdriver
from webdriver_manager.opera import OperaDriverManager

driver = webdriver.Opera(executable_path=OperaDriverManager().install())

封装好的webdriver写法

#!/usr/config/env python
# -*- coding:utf-8 -*-

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from webdriver_manager.microsoft import IEDriverManager
from webdriver_manager.opera import OperaDriverManager


from lib.common import download_path


def browser(browser='chrome'):
    downloadpath = download_path()

    if browser == "firefox" or browser == "ff":
        profile = webdriver.FirefoxProfile()
        profile.set_preference('browser.download.dir', downloadpath)  # 相同类型的文件上一次的下载保存位置
        profile.set_preference('browser.download.folderList', 2) # 设置Firefox的默认下载文件夹。0是桌面;1是“我的下载”;2是自定义。这一选项你可以直接在“首选项-下载-将所有文件保存在此文件夹”中修改。但请注意,如果你在“首选项-下载-保存每个文件夹前询问存储位置“(也就是要求弹出下载对话框),那么本选项无效
        profile.set_preference('browser.download.manager.showWhenStarting', False) # 'application/octet-stream'对应下载的文件格式是rar
        profile.set_preference('browser.helperApps.neverAsk.saveToDisk','application/octet-stream')
        driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(),firefox_profile=profile)
        return driver
    elif browser == "ff_headless":
        profile = webdriver.FirefoxProfile()
        profile.set_preference('browser.download.dir', downloadpath)
        profile.set_preference('browser.download.folderList', 2)
        profile.set_preference('browser.download.manager.showWhenStarting', False)
        profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/octet-stream')
        from selenium.webdriver.firefox.options import Options
        firefox_options = Options()
        firefox_options.add_argument('--headless')
        driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(), firefox_profile=profile, options=firefox_options)
        return driver
    elif browser == "chrome":
        options = webdriver.ChromeOptions()
        prefs = {
            'profile.default_content_settings.popups': 0,
            'download.default_directory': downloadpath, "profile.default_content_setting_values.automatic_downloads": 1}
        options.add_experimental_option('prefs', prefs)  # 指定下载路径,并且允许下载多个文件
        driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options)
        return driver
    elif browser == "chrome_headless":
        options = webdriver.ChromeOptions()
        prefs = {
            'profile.default_content_settings.popups': 0,
            'download.default_directory': downloadpath, "profile.default_content_setting_values.automatic_downloads": 1}
        options.add_experimental_option('prefs', prefs)
        options.add_argument('-headless')
        driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(),options=options)
        return driver
    elif browser == "edge":
        driver = webdriver.Edge(executable_path=EdgeChromiumDriverManager().install())
        return driver
	elif browser == "IE":
        driver = webdriver.Ie(executable_path=IEDriverManager().install())
        return driver
    elif browser == "opera":
        driver = webdriver.Opera(executable_path=OperaDriverManager().install())
        return driver


if __name__ == '__main__':
    browser('chrome')

 

上一篇:【可视化爬虫实战】使用selenium爬取学校教务网并保存成绩截图


下一篇:Chrome扩展程序开发 - 更改活动选项卡的default_icon