[파이썬] Selenium - 모든 쿠키 추출하기 Selenium으로 접속한 사이트의 모든 쿠키를 추출하는 기능입니다. from selenium import webdriver driver = webdriver.Chrome() driver.get("https://www.naver.com") # 모든 쿠기 추출 cookies = driver.get_cookies() print("All Cookies:", cookies) 해당 코드 실행 시 모든 쿠키를 출력할 수 있습니다. 플그래밍/파이써언 2024.04.24
[파이썬] Selenium - 자바스크립트로 스타일(css) 변경하기 Selenium으로 사이트를 연 후 특정 요소의 스타일을 변경하는 방법입니다. from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument("--window-size=800,800") driver = webdriver.Chrome(options=chrome_options) # 페이지 열기 driver.get("https://www.google.com") # 스타일을 변경할 요소 찾기 element = driver.find_element(By.. 플그래밍/파이써언 2024.04.24
[파이썬] Selenium - 여러 탭 열기 및 이동하는 방법 Selenium으로 세 개의 사이트를 연 후 탭 이동하는 방법입니다. from selenium import webdriver import time driver = webdriver.Chrome() # 첫 번째 탭 열기 driver.get("http://www.daum.net") time.sleep(1) # 1초 대기 # 두 번째 탭 열기 driver.execute_script("window.open('');") driver.switch_to.window(driver.window_handles[1]) driver.get("http://www.naver.com") time.sleep(1) # 1초 대기 # 세 번째 탭 열기 driver.execute_script("window.open('');") drive.. 플그래밍/파이써언 2024.04.24
[파이썬] Selenium - 헤드레스(Headless, 창 없는 백그라운드 실행) 모드 헤드레스 모드란 창을 띄우지 않고 Selenium을 실행하는 기능입니다. from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument('--headless') driver = webdriver.Chrome(options=options) driver.get("http://finance.naver.com") print(driver.title) 헤드레스 모드로 브라우저를 실행시키는 Selenium 코드입니다. 실행 시 아래와 같이 해당 페이지의 제목을 출력하는 것을 확인 할 수 있습니다. 플그래밍/파이써언 2024.04.24
[파이썬] Selenium - 마우스 우클릭 (ActionChains) from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome() driver.get("https://www.naver.com") element_to_right_click = driver.find_element(By.XPATH, '//*[@id="shortcutArea"]/ul/li[1]/a/span[1]') actions = ActionChains(driver) actions.contex.. 플그래밍/파이써언 2024.04.24
[파이썬] Selenium - 더블 클릭 (ActionChains) from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.get("https://www.rapidtables.com/tools/click-counter.html") element_to_double_click = driver.find_element(By.ID, "addbtn") actions = ActionChains(driver) actions.double_click(element_to_double_click).perform() Selenium Acti.. 플그래밍/파이써언 2024.04.24
[파이썬] Selenium - Hover Over 하는 방법 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.get("https://finance.naver.com/sise/") element_to_hover_over = driver.find_element(By.XPATH, '//*[@id="contentarea"]/div[1]/div[1]/ul/li[3]') actions = ActionChains(driver) actions.move_to_element(element_to_hover_over).per.. 플그래밍/파이써언 2024.04.24
[파이썬] Selenium - 드래그 앤 드롭 (Drag & Drop) 하는 방법 Selenium에서 요소를 선택 후 드래그 하는 방법입니다. from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome() driver.get("https://jqueryui.com/droppable/") driver.switch_to.frame(0) # 타.. 플그래밍/파이써언 2024.04.24
[파이썬] Selenium - AttributeError: 'WebDriver' object has no attribute 'find_element_by_id' AttributeError: 'WebDriver' object has no attribute 'find_element_by_id' 위와 같은 에러가 발생했을 경우 해결하는 방법입니다. # 변경 전 driver.find_element_by_id("draggable") # 변경 후 from selenium.webdriver.common.by import By driver.find_element(By.ID, "draggable") 위와 같이 변경하면 되는데요, from selenium.webdriver.common.by import By 해당 모듈을 반드시 추가해주셔야 합니다. 플그래밍/파이써언 2024.04.24
[파이썬] Selenium - WIKIMEDIA 메인페이지 이미지 다운로드 방법 이번에는 아래 메인 페이지에 있는 이미지의 src를 추출해서 직접 파일로 다운로드해볼께요 파이썬 코드 import requests from bs4 import BeautifulSoup from selenium import webdriver import undetected_chromedriver as uc from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.chrome.service import Service from web.. 플그래밍/파이써언 2024.03.15