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

使用selenium时webdriver问题

通常使用selenium时候我们需要自己单独去下载对应各浏览器版本的webdriver,并在后期针对不同操作系统(mac、windows、linux)或者不同浏览器版本都要反复选择对应版本webdriver

为了解决以上问题,webdriver-manage三方库营运而生,它可以自动帮你识别当前运行环境下系统信息以及对应浏览器信息,并自动下载对应的webdriver,再也不用担心webdriver版本问题!!!

下载第三方库

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‘)

 

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

上一篇:CSS——CSS中可以和不可以继承的属性


下一篇:学习笔记——python导入模块(3)