본문 바로가기
Python/selenium

selenium) 셀레니움 기본 사용법

by 유노파이 2022. 1. 17.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC



# 크롬옵션 생성
chrome_options = Options()

# 크롬드라이버 경로지정
chrome_driver_path = r'C:\chromedriver.exe'

# 크롬창 max 사용
chrome_options.add_argument("start-maximized")

# 크롬창 숨길때 사용
chrome_options.add_argument('--headless')

# gpu가속사용x
chrome_options.add_argument("disable-gpu")
# 사용자 정보 임의로 수정
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                 "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36")

# 2 = 사용안함
# 1 = 사용
prefs = {'profile.default_content_setting_values': {
            'images': 2, 'plugins' : 2, 
            'geolocation': 2, 'notifications' : 2, 'auto_select_certificate': 2,
            'fullscreen' : 2, 'mouselock' : 2, 'mixed_script': 2, 'media_stream' : 2,
            'media_stream_mic' : 2, 'media_stream_camera': 2, 'protocol_handlers' : 2,
            'ppapi_broker' : 2, 'automatic_downloads': 2, 'midi_sysex' : 2,
            'push_messaging' : 2, 'ssl_cert_decisions': 2, 'metro_switch_to_desktop' : 2,
            'protected_media_identifier': 2, 'app_banner': 2, 'site_engagement' : 2,
            'durable_storage' : 2
        } #'cookies' : 1, 'popups': 1,
}

chrome_options.add_experimental_option('prefs', prefs)

### 크롬 드라이버 생성 #chrome_options
driver = webdriver.Chrome ( chrome_driver_path, options = chrome_options )

# By.XPATH 찾을때까지 기다림
WebDriverWait(driver,5).until(
	EC.presence_of_element_located((By.XPATH, '찾을 xpath')))

댓글