【分享】自动解决浏览器驱动依赖

# -*- coding:utf-8 -*-
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager  # pip install webdriver-manager
from webdriver_manager.firefox import GeckoDriverManager
import platform
import os
def auto_drvier(driver_type='chrome'):
    platform_system = platform.system()
    print(f'platform system is {platform_system} ')
    if platform_system == 'Windows':
        result = os.popen('where chromedriver').read()
        is_install = False if '信息: 用提供的模式无法找到文件' in result else True
    elif platform_system == 'linux' or platform_system == 'Darwin':
        result = os.popen('which chromedriver').read()
        is_install = False if 'no chromedriver in' in result else True
    if driver_type == 'chrome':
        if not is_install:
            driver_path = ChromeDriverManager(
                url="https://npm.taobao.org/mirrors/chromedriver/",
                latest_release_url="https://npm.taobao.org/mirrors/chromedriver/LATEST_RELEASE").install()
        else:
            driver_path='chromedriver'
        driver=webdriver.Chrome(executable_path=driver_path)
    elif driver_type == 'firefox':
        if not is_install:
            driver_path = GeckoDriverManager().install()
        else:
            driver_path = 'geckodriver'
        driver = webdriver.Firefox( executable_path=driver_path)
    return driver

auto_drvier('firefox')
3 Likes


启动的driver好像不可用。。

想方便的可以直接用这种方法:

from webdriver_manager.chrome import ChromeDriverManager
import time

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

driver.get('https://www.baidu.com')

time.sleep(3)
driver.quit()

网址错了 https://www.baidu.com/

  • -好的我等下再试下。。