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
- selenium.webdriver: 브라우저를 제어하기 위한 모듈이다.
- chromedriver_autoinstaller: 크롬 브라우저 버전에 맞는 크롬드라이버를 자동으로 설치해주는 모듈이다.
- time: 페이지 로딩 등의 대기 시간을 설정할 때 사용된다.
- Options: 크롬 브라우저 옵션을 설정하는 객체이다.
- Keys, ActionChains: 키보드 이벤트 및 여러 동작을 체인 방식으로 수행하는 데 사용된다.
- By: 요소를 검색할 때 사용되는 방법(id, class, name 등)을 정의한다.
- pyperclip: 클립보드를 제어하기 위한 모듈이다 (복사 및 붙여넣기 기능).
- pandas: 데이터 처리 및 분석을 위한 라이브러리로, 여기서는 아직 사용되지 않았지만 나중에 데이터를 처리할 수 있다.
브라우저 옵션 설정 및 크롬드라이버 설치
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chromedriver_autoinstaller.install()
driver = webdriver.Chrome(options=chrome_options)
- chrome_options = Options(): 크롬 브라우저 옵션을 정의하는 객체를 생성한다.
- add_experimental_option("detach", True): 브라우저가 스크립트 종료 후에도 자동으로 닫히지 않도록 설정한다.
- chromedriver_autoinstaller.install(): 현재 시스템에 설치된 크롬 브라우저의 버전에 맞는 크롬드라이버를 자동으로 설치한다.
- webdriver.Chrome(options=chrome_options): 설정된 옵션을 사용해 크롬 브라우저를 실행한다.
웹사이트 열기 및 대기
url = 'https://www.google.com'
driver.get(url)
time.sleep(3)
- url = 'https://www.google.com': 접속할 웹 페이지의 URL을 변수로 저장한다.
- driver.get(url): 해당 URL을 열고 크롬 브라우저에서 페이지를 로드한다.
- time.sleep(3): 페이지가 완전히 로딩되도록 3초간 대기한다.
반응형
'정보처리,전산 > Python' 카테고리의 다른 글
NumPy를 사용하여 1차원 배열을 2차원 배열로 변환하는 과정 (0) | 2024.10.22 |
---|---|
in 연산자 오류 (0) | 2024.09.26 |
시간에 따른 거리 (Distance), 속도 (Velocity), 가속도 (Acceleration)의 변화 (0) | 2024.09.15 |
리스트 특정 키에 대해 내림차순 정렬 (0) | 2024.09.04 |
폰번호 유효성 검사 if (0) | 2024.08.18 |