Python을 배워보자

파이썬 Selenium webdriver-manager: 웹 자동화의 필수 도구, 자세히 알아보기

_Blue_Sky_ 2024. 11. 15. 12:45
728x90
728x90

파이썬을 이용한 웹 자동화를 진행하다 보면 반드시 마주하게 되는 라이브러리 중 하나가 바로 Selenium입니다. Selenium은 웹 브라우저를 자동으로 제어하여 웹 페이지와 상호 작용하는 강력한 도구입니다. 하지만 Selenium을 효과적으로 사용하기 위해서는 웹 드라이버(Chrome Driver, Firefox Driver 등)가 필요하며, 이 드라이버의 버전 관리가 번거로운 문제로 작용할 수 있습니다. 이러한 문제를 해결하고 Selenium을 더욱 편리하게 사용할 수 있도록 도와주는 것이 바로 webdriver-manager입니다.

webdriver-manager는 파이썬 라이브러리로, Selenium에서 사용하는 웹 드라이버를 자동으로 설치하고 관리해주는 기능을 제공합니다. 즉, 개발자가 직접 웹 드라이버를 다운로드하고 설치하는 번거로운 작업을 생략하고, 코드 몇 줄만으로 원하는 웹 드라이버를 설치하고 사용할 수 있도록 지원합니다. 또한, webdriver-manager는 웹 브라우저 버전에 맞는 적절한 웹 드라이버를 자동으로 선택하여 설치해주므로, 버전 불일치로 인한 오류 발생 가능성을 줄여줍니다.

728x90

webdriver-manager의 주요 기능

  • 자동 설치: 코드 한 줄로 원하는 웹 드라이버를 자동으로 설치합니다.
  • 버전 관리: 웹 브라우저 버전에 맞는 적절한 웹 드라이버를 자동으로 선택하여 설치합니다.
  • 다양한 브라우저 지원: Chrome, Firefox, Edge 등 다양한 브라우저를 지원합니다.
  • 간편한 사용법: 간단한 API를 통해 쉽게 사용할 수 있습니다.

webdriver-manager 설치 및 사용 방법

pip install webdriver-manager
 
 

위 명령어를 실행하여 webdriver-manager를 설치한 후, 다음과 같이 코드를 작성하여 사용할 수 있습니다.

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

# 크롬 드라이버 자동 설치 및 WebDriver 객체 생성
driver = webdriver.Chrome(ChromeDriverManager().install())

위 코드는 Chrome 드라이버를 자동으로 설치하고, 설치된 드라이버를 이용하여 WebDriver 객체를 생성하는 예시입니다. Firefox 드라이버를 사용하려면 ChromeDriverManager 대신 GeckoDriverManager를 사용하면 됩니다.

webdriver-manager를 사용하는 이유

  • 편리성: 웹 드라이버 설치 및 관리에 대한 부담을 크게 줄여줍니다.
  • 효율성: 개발 시간을 단축하고 생산성을 향상시킵니다.
  • 안정성: 버전 불일치로 인한 오류 발생 가능성을 줄여줍니다.
  • 유지보수 편의성: 웹 드라이버 버전 관리에 대한 부담을 줄여줍니다.
728x90

webdriver-manager는 Selenium을 사용하는 개발자에게 필수적인 도구입니다. webdriver-manager를 사용하면 웹 드라이버 관리에 대한 번거로움을 해소하고, 더욱 효율적이고 안정적인 웹 자동화 작업을 수행할 수 있습니다.


The main idea is to simplify management of binary drivers for different browsers.

For now support:

Compatible with Selenium 4.x and below.

Before: You need to download the chromedriver binary, unzip it somewhere on your PC and set the path to this driver like this:

from selenium import webdriver
driver = webdriver.Chrome('/home/user/drivers/chromedriver')

It’s boring!!! Moreover, every time a new version of the driver is released, you need to repeat all these steps again and again.

With webdriver manager, you just need to do two simple steps:

Install manager:

pip install webdriver-manager

Use with Chrome

# selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

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

Use with Chromium

# selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType

driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install())
# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType

driver = webdriver.Chrome(service=ChromiumService(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()))

Use with Brave

# selenium 3
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType

driver = webdriver.Chrome(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install())
# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as BraveService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType

driver = webdriver.Chrome(service=BraveService(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install()))

Use with Edge

# selenium 3
from selenium import webdriver
from webdriver_manager.microsoft import EdgeChromiumDriverManager

driver = webdriver.Edge(EdgeChromiumDriverManager().install())
# selenium 4
from selenium import webdriver
from selenium.webdriver.edge.service import Service as EdgeService
from webdriver_manager.microsoft import EdgeChromiumDriverManager

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

Use with Firefox

# selenium 3
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
# selenium 4
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))

Use with IE

# selenium 3
from selenium import webdriver
from webdriver_manager.microsoft import IEDriverManager

driver = webdriver.Ie(IEDriverManager().install())
# selenium 4
from selenium import webdriver
from selenium.webdriver.ie.service import Service as IEService
from webdriver_manager.microsoft import IEDriverManager

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

Use with Opera

# selenium 3
from selenium import webdriver
from selenium.webdriver.chrome import service
from webdriver_manager.opera import OperaDriverManager

webdriver_service = service.Service(OperaDriverManager().install())
webdriver_service.start()

driver = webdriver.Remote(webdriver_service.service_url, webdriver.DesiredCapabilities.OPERA)
# selenium 4
from selenium import webdriver
from selenium.webdriver.chrome import service
from webdriver_manager.opera import OperaDriverManager

webdriver_service = service.Service(OperaDriverManager().install())
webdriver_service.start()

options = webdriver.ChromeOptions()
options.add_experimental_option('w3c', True)

driver = webdriver.Remote(webdriver_service.service_url, options=options)

If the Opera browser is installed in a location other than C:/Program Files or C:/Program Files (x86) on Windows and /usr/bin/opera for all unix variants and mac, then use the below code,

options = webdriver.ChromeOptions()
options.binary_location = "path/to/opera.exe"
driver = webdriver.Remote(webdriver_service.service_url, options=options)

Get browser version from path

To get the version of the browser from the executable of the browser itself:

from webdriver_manager.firefox import GeckoDriverManager

from webdriver_manager.core.utils import read_version_from_cmd 
from webdriver_manager.core.os_manager import PATTERN

version = read_version_from_cmd("/usr/bin/firefox-bin --version", PATTERN["firefox"])
driver_binary = GeckoDriverManager(version=version).install()

Custom Cache, File manager and OS Manager

from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.file_manager import FileManager
from webdriver_manager.core.driver_cache import DriverCacheManager
from webdriver_manager.core.os_manager import OperationSystemManager

cache_manager = DriverCacheManager(file_manager=FileManager(os_system_manager=OperationSystemManager()))
manager = ChromeDriverManager(cache_manager=cache_manager)
os_manager = OperationSystemManager(os_type="win64")

Configuration

webdriver_manager has several configuration variables you can be interested in. Any variable can be set using either .env file or via python directly

GH_TOKEN

webdriver_manager downloading some webdrivers from their official GitHub repositories but GitHub has limitations like 60 requests per hour for unauthenticated users. In case not to face an error related to GitHub credentials, you need to create GitHub token and place it into your environment: (*)

Example:

export GH_TOKEN = "asdasdasdasd"

(*) access_token required to work with GitHub API more info.

There is also possibility to set same variable via ENV VARIABLES, example:

import os

os.environ['GH_TOKEN'] = "asdasdasdasd"

WDM_LOG

Turn off webdriver-manager logs use:

import logging
import os

os.environ['WDM_LOG'] = str(logging.NOTSET)

WDM_LOCAL

By default, all driver binaries are saved to user.home/.wdm folder. You can override this setting and save binaries to project.root/.wdm.

import os

os.environ['WDM_LOCAL'] = '1'

WDM_SSL_VERIFY

SSL verification can be disabled for downloading webdriver binaries in case when you have troubles with SSL Certificates or SSL Certificate Chain. Just set the environment variable WDM_SSL_VERIFY to "0".

import os

os.environ['WDM_SSL_VERIFY'] = '0'

version

Specify the version of webdriver you need. And webdriver-manager will download it from sources for your os.

from webdriver_manager.chrome import ChromeDriverManager

ChromeDriverManager(driver_version="2.26").install()

cache_valid_range

Driver cache by default is valid for 1 day. You are able to change this value using constructor parameter:

from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.driver_cache import DriverCacheManager

ChromeDriverManager("2.26", cache_manager=DriverCacheManager(valid_range=1)).install()

os_type

For some reasons you may use custom OS/arch. You are able to change this value using constructor parameter:

from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import OperationSystemManager

ChromeDriverManager(os_system_manager=OperationSystemManager(os_type="linux-mips64")).install()

url

You may use any other repo with drivers and release URl. You are able to change this value using constructor parameters:

from webdriver_manager.chrome import ChromeDriverManager

ChromeDriverManager(url="https://custom-repo.url", latest_release_url="https://custom-repo.url/LATEST").install()

Custom Logger

If you need to use a custom logger, you can create a logger and set it with set_logger().

import logging
from webdriver_manager.core.logger import set_logger

logger = logging.getLogger("custom_logger")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
logger.addHandler(logging.FileHandler("custom.log"))

set_logger(logger)

Custom HTTP Client

If you need to add custom HTTP logic like session or proxy you can define your custom HttpClient implementation.

import os

import requests
from requests import Response

from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.download_manager import WDMDownloadManager
from webdriver_manager.core.http import HttpClient
from webdriver_manager.core.logger import log

class CustomHttpClient(HttpClient):

    def get(self, url, params=None, **kwargs) -> Response:
        """
        Add you own logic here like session or proxy etc.
        """
        log("The call will be done with custom HTTP client")
        return requests.get(url, params, **kwargs)


def test_can_get_chrome_driver_with_custom_http_client():
    http_client = CustomHttpClient()
    download_manager = WDMDownloadManager(http_client)
    path = ChromeDriverManager(download_manager=download_manager).install()
    assert os.path.exists(path)

This will make your test automation more elegant and robust!

 

728x90
728x90