반응형
selenium 기본 예제
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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.common.action_chains import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
import pyperclip #
import time
# chromedriver.exe 파일을 다운로드하여 프로젝트 main.py 경로에 복사하자.
def clipboard_input(browser, xpath, input):
pyperclip.copy(input) # input을 클립보드로 복사
el = browser.find_element(By.XPATH, xpath)
el.click() # element focus 설정
time.sleep(0.5)
# ctrl + v 전달
ActionChains(browser).key_down(Keys.CONTROL).send_keys('v').key_up(Keys.CONTROL).perform()
try:
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
browser = webdriver.Chrome(options=options) # "./chromedriver.exe"
# browser = webdriver.Chrome(ChromeDriverManager().install())
print("Hello, webdriver!")
except Exception as e:
print("webdriver-loading ERROR.", e)
exit(1)
try:
except Exception as e:
print("naver.com connection ERROR.", e)
browser.quit()
exit(1)
time.sleep(1.5)
try:
el = browser.find_element(By.ID, "query")
el.send_keys("수알치")
btn = browser.find_element(By.ID, "search_btn")
el.send_keys(Keys.ENTER)
# XPATH 방법으로 클릭
# el = browser.find_element(By.XPATH, '//*[@id="search_btn"]')
# el.click()
# els = browser.find_elements(By.TAG_NAME, "a")
# for el in els:
# print(el)
except Exception as e:
print("element process ERROR.", e)
browser.quit()
exit(1)
# 네이버 로그인
try:
browser.get(login_url)
except Exception as e:
print("naver.com login-url connection ERROR.", e)
browser.quit()
exit(1)
time.sleep(2)
# 로그인은 자바스크립트 이용
try:
# 실패 ...
n_id = "my_id"
n_pw = "my_password"
clipboard_input(browser, '//*[@id="id"]', n_id)
clipboard_input(browser, '//*[@id="pw"]', n_pw)
el = browser.find_element(By.XPATH, '/html/body/div[1]/div[2]/div/div[1]/form/ul/li/div/div[7]/button')
el.click()
# 네이버 보안에서 자바 스크립트를 통해서 보안을 뚫습니다 <--- 실패
# browser.execute_script("document.getElementsByName('id')[0].value=\'"+naver_id+"\'")
# time.sleep(1)
# browser.execute_script("document.getElementsByName('pw')[0].value=\'"+naver_pw+"\'")
# time.sleep(1)
# browser.find_element(By.XPATH, '//*[@id="log.login"]').click()
# 일반적으로 사람이 작성하는 형태 => 네이버 보안에서 막힌다
# el = browser.find_element(By.ID, "id")
# el.click()
# pyperclip.copy('my_id')
# el.send_keys(Keys.CONTROL, 'v')
# # el.send_keys("my_id")
# time.sleep(2)
# el = browser.find_element(By.ID, "pw")
# el.click()
# pyperclip.copy('my_id')
# el.send_keys(Keys.CONTROL, 'v')
# # el.send_keys("my_password")
# time.sleep(2)
# el = browser.find_element(By.ID, "log.login")
# el.click()
# time.sleep(0.5)
# time.sleep(20)
# el.send_keys(Keys.ENTER)
except Exception as e:
print("naver.com login ERROR.", e)
browser.quit()
print("............???")
time.sleep(10)
browser.quit()
print("webdriver is closed.")
exit(0)
'''
driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')
ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"
location
location_once_scrolled_into_view
page_source
parent
rect
screenshot
screenshot_as_base64
screenshot_as_png
size
tag_name
text
value_of_css_property
submit()
close() # 탭 닫기
quit() # 브라우저 닫기
el.clear
el.click()
el.send_key("hello")
el.send_key(Keys.ENTER)
browser.back()
browser.forward()
browser.refresh()
# JavaScript scrollTop += 50
js_scroll = 'return document.getElementsByClassName("y-scroll")[1].scrollTop += 50'
driver.execute_script(js_scroll)
id = browser.execute_script('return info.id')
# JavaScript Click
js_click = 'return document.getElementsByClassName("sadfsdafsdaf")[0].click()'
driver.execute_script(js_click)
# get_property
div = driver.find_elements_by_id("div")
div[0].get_property("scrollTop")
# Tab 목록 보기
driver.window_handles
# Tab 화면 전환
last_tab = driver.window_handles[-1]
driver.switch_to.window(window_name=last_tab)
# 창 닫기
driver.close()
first_tab = driver.window_handles[0]
driver.switch_to.window(first_tab)
'''
반응형