selenium | chrome 브라우저 열기

2024. 9. 21. 09:47정보처리,전산/Python

반응형

 

모듈

from selenium import webdriver
import chromedriver_autoinstaller
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import Keys, ActionChains
from selenium.webdriver.common.by import By
import pyperclip
import pandas as pd
 
  1. selenium.webdriver: 브라우저를 제어하기 위한 모듈이다.
  2. chromedriver_autoinstaller: 크롬 브라우저 버전에 맞는 크롬드라이버를 자동으로 설치해주는 모듈이다.
  3. time: 페이지 로딩 등의 대기 시간을 설정할 때 사용된다.
  4. Options: 크롬 브라우저 옵션을 설정하는 객체이다.
  5. Keys, ActionChains: 키보드 이벤트 및 여러 동작을 체인 방식으로 수행하는 데 사용된다.
  6. By: 요소를 검색할 때 사용되는 방법(id, class, name 등)을 정의한다.
  7. pyperclip: 클립보드를 제어하기 위한 모듈이다 (복사 및 붙여넣기 기능).
  8. pandas: 데이터 처리 및 분석을 위한 라이브러리로, 여기서는 아직 사용되지 않았지만 나중에 데이터를 처리할 수 있다.

 

 

 

브라우저 옵션 설정 및 크롬드라이버 설치

chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chromedriver_autoinstaller.install()
driver = webdriver.Chrome(options=chrome_options)

 

  1. chrome_options = Options(): 크롬 브라우저 옵션을 정의하는 객체를 생성한다.
  2. add_experimental_option("detach", True): 브라우저가 스크립트 종료 후에도 자동으로 닫히지 않도록 설정한다.
  3. chromedriver_autoinstaller.install(): 현재 시스템에 설치된 크롬 브라우저의 버전에 맞는 크롬드라이버를 자동으로 설치한다.
  4. webdriver.Chrome(options=chrome_options): 설정된 옵션을 사용해 크롬 브라우저를 실행한다.

 

 

 

웹사이트 열기 및 대기

url = 'https://www.google.com'
driver.get(url)
time.sleep(3)
  1. url = 'https://www.google.com': 접속할 웹 페이지의 URL을 변수로 저장한다.
  2. driver.get(url): 해당 URL을 열고 크롬 브라우저에서 페이지를 로드한다.
  3. time.sleep(3): 페이지가 완전히 로딩되도록 3초간 대기한다.
 
 
 

 

 

반응형