When using selenium, even when you install the same chromedriver version as the Chrome browser, the browser shuts down immediately upon code execution.
Original code
# 1) pipenv install selenium
# 2) https://chromedriver.chromium.org/downloads
# Download a driver matching your Chrome version, and unzip it in any folder
from selenium import webdriver
browser = webdriver.Chrome("C:/Users/everp/Documents/env_p3/chromedriver.exe")
browser.get("https://www.naver.com")
Improved code
# 1) pipenv install selenium
# 2) https://chromedriver.chromium.org/downloads
# Download a driver matching your Chrome version, and unzip it in any folder
# 3) pipenv install webdriver-manager
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
# Auto-update Chrome driver
from webdriver_manager.chrome import ChromeDriverManager
# Prevent browser from closing
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
# Suppress unnecessary error messages
chrome_options.add_experimental_option("excludeSwitches", ["enable-logging"])
service = Service(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options,executable_path="C:/Users/everp/Documents/env_p3/chromedriver.exe")
# Navigate to the target webpage
driver.get("https://www.naver.com")
Code that keeps changing and improving. Complacency is the end. That's why the world of living-coding is so attractive and fair! hahaha
The most important official documentation
https://www.selenium.dev/selenium/docs/api/py/index.html
Selenium Client Driver — Selenium 4.8 documentation
www.selenium.dev
