본문 바로가기
외국어/몬티 파이튼의 비행 서커스

셀레니움 크롬 드라이버 자동 업데이트

by 사막 고양이 2023. 3. 10.

1번

from selenium import webdriver
import chromedriver_autoinstaller

# 크롬 드라이버 버전 체크 & 업데이트 진행
chrome_ver = chromedriver_autoinstaller.get_chrome_version().split(".")[0]
try:
    driver = webdriver.Chrome(f"./{chrome_ver}/chromedriver.exe")
    print(f"NOW intalled {chrome_ver}")
    driver.quit()
except:
    chromedriver_autoinstaller.install(True)
    driver = webdriver.Chrome(f"./{chrome_ver}/chromedriver.exe")
    print(f"now installing {chrome_ver}")
    driver.quit()
# 버전체크 종료


# 크롬 실행
driver = webdriver.Chrome(f"./{chrome_ver}/chromedriver.exe")
  • pip install chromedriver_autoinstaller
  • C:\Users\Sloane.cache\selenium\chromedriver\win32\
  • try except를 사용하는데, 다운로드 되는 위치에 파일이 없어도 except 부분의 print는 실행이 안되면서 다운로드는 됨...이거 왜이럼?

2번

from selenium import webdriver
import chromedriver_autoinstaller
import os

# Check if chrome driver is installed or not
chrome_ver = chromedriver_autoinstaller.get_chrome_version().split(".")[0]
driver_path = f"./chromedriver/{chrome_ver}/chromedriver.exe"
if os.path.exists(driver_path):
    print(f"chrom driver is insatlled: {driver_path}")
else:
    print(f"install the chrome driver(ver: {chrome_ver})")
    chromedriver_autoinstaller.install(True)

# Get driver and open url
driver = webdriver.Chrome(driver_path)
driver.get("https://google.com")
  • pip install chromedriver_autoinstaller
  • 내가 설정한 경로에 제대로 다운로드됨. 하지만 이걸로 실행하는지는 몰?루
  • 1번이 다운로드 되는 위치와, 내가 지정한 경로에 둘다 다운로드가 됨.
  • 1번이나 2번, 둘 중 한 곳에 파일이 없으면 다운로드를 함.
  • chromedriver_autoinstaller.install() 이 코드가 다운로드 되는 경로를 반환시켜준다고 한다.
    • 이 경로에 파일이 다운로드 되는 것을 확인함. 근데, 대체 왜?....여기에다가도 다운로드 하고 다른 곳에도 다운로드를 하는 것인가.
    • 1번의 경우엔 또 이 위치에 다운로드를 안한다. 대체 뭔가?...

3번

from selenium import webdriver  # 크롬 드라이버 자동 업데이트
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

# 브라우저 꺼짐 방지
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

# 불필요한 에러 메시지 없애기
chrome_options.add_experimental_option("excludeSwitches", ["enable-logging"])

# 내문서의 .wdm 폴더에 다운로드 되는듯 함.
service = Service(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)

driver.get("https://www.naver.com")
  • pip install webdriver_manager
  • 내 문서의 .wdm 폴더에 다운로드 되는듯 함.
    • C:\Users\Sloane.wdm\drivers\chromedriver\win32

4번

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time

def main():

    url = "http://www.naver.com"
    driver = webdriver.Chrome(ChromeDriverManager().install())
    driver.get(url)
    time.sleep(5)


if __name__ == '__main__':

    main()
  • 3번의 축약형

'외국어 > 몬티 파이튼의 비행 서커스' 카테고리의 다른 글

파이썬 셀레니움 업데이트 관련 오류  (0) 2023.08.01
PyInstaller  (0) 2023.05.02
파이썬 가상환경  (0) 2023.04.27
파이썬 datetime 변환식 코드  (0) 2023.03.10