RPA/Python 48

[selenium] 페이지 로딩까지 대기

1. implicitly wait : 조건만큼만 대기하다 다음 동작 실행 from selenium import webdriver driver = webdriver.Chrome() driver.get('https://www.w3schools.com/html/') driver.maximize_window() # 10초 동안 페이지 로딩 대기 (로딩 되면 바로 다음 수행) driver.implicitly_wait(10) elem = driver.find_element_by_xpath('//*[@id="leftmenuinnerinner"]/a[61]') 2. explicitly wait : 조건이 완료될때까지 대기 from selenium import webdriver from selenium.webdriver..

RPA/Python 2021.05.06

[selenium] 동적 페이지 스크롤

예를 들면 쇼핑몰 같은 페이지는 처음부터 모든 문서를 로딩하지 않고 스크롤을 내리면 이후 문서가 로딩되는 동적 페이지이다. 페이지 스크롤 및 동적 페이지 스크롤 방법은 아래와 같다. from selenium import webdriver # from selenium.webdriver.common.keys import Keys import time driver = webdriver.Chrome() driver.get('https://shopping.naver.com/') # 검색창에 검색어 입력 elem = driver.find_element_by_xpath('//*[@id="autocompleteWrapper"]/input[1]') elem.send_keys('선풍기') # 검색창에서 enter 눌러 검..

RPA/Python 2021.05.06

[selenium] iframe 내 Element 접근 (checkbox, radio, select 태그)

페이지에 iframe이 있다면 iframe 내 Element는 직접 접근 안된다. 먼저 접근하려는 Element가 있는 iframe으로 전환 후 Xpath로 접근하여 처리하고 다시 빠져 나온다. from selenium import webdriver driver = webdriver.Chrome() driver.get('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_checkbox') # iframe id값을 이용해 프레임 전환 driver.switch_to.frame('iframeResult') # Xpath로 해당 checkbox/radio button 접근 후 클릭 elem = driver.find_element_by_x..

RPA/Python 2021.05.06

[selenium] Xpath 등을 이용하여 Element 동작 처리하기

from selenium import webdriver from selenium.webdriver.common.keys import Keys # 드라이버 생성 driver = webdriver.Chrome() # 네이버 이동 driver.get('https://www.naver.com') # 1. 네이버 첫 페이지에서 '카페'를 찾아 클릭 # '카페' 글자가 텍스트여서 텍스트 찾기로 가능하다. # '카페'라는 이름의 첫번째 Element 찾기 # elem.get_attribute('href')와 같이 attribute 값 확인 가능 elem = driver.find_element_by_link_text('카페') # Element 클릭하기 elem.click() # 2. 네이버 검색창의 Xpath값으로 ..

RPA/Python 2021.05.05

[selenium] Headless로 처리하기

RPA 처리 시 실제 브라우저를 실행하지 않고 가상 브라우저로 처리하는 방법이 있다. 드라이버 생성 시 Headless 옵션을 주면 가능하다. 일부 사이트는 Headless를 통한 크롤링 접근을 차단하는 곳이 이에 대한 추가 옵션이 필요하다. from selenium import webdriver from selenium.webdriver.chrome.options import Options # 브라우저 실행하지 않고 처리하기 옵션 (Headless로 실행하기) # 참고 및 인용 : https://beomi.github.io/gb-crawling/posts/2017-09-28-HowToMakeWebCrawler-Headless-Chrome.html op = Options() # Headless 설정 op..

RPA/Python 2021.05.05

[selenium] 브라우저 및 Handle 컨트롤

from selenium import webdriver # chrome 드라이버가 workspace에 없고 다른 경로에 있을 경우 # driver = webdriver.Chrome(executable_path=r'C:\driver\chromedriver.exe') driver = webdriver.Chrome() # 네이버로 페이지 이동 driver.get('https://www.naver.com') driver.implicitly_wait(10) # 브라우저 뒤로, 앞으로 가기 driver.back() driver.implicitly_wait(10) driver.forward() driver.implicitly_wait(10) # 브라우저 새로고침 driver.refresh() driver.implic..

RPA/Python 2021.05.05

[OS] 파일 및 폴더 관리

import os import time import datetime # 현재 폴더 (C:\PythonWorkspace) print(os.getcwd()) # 폴더 변경 (C:\PythonWorkspace\RPA) os.chdir('RPA') print(os.getcwd()) # 상위 폴더 이동 (C:\PythonWorkspace) os.chdir('..') print(os.getcwd()) # 2단계 상위 폴더 이동 os.chdir('RPA') # C:\PythonWorkspace\RPA print(os.getcwd()) os.chdir('../..') # C:\ print(os.getcwd()) # 절대 경로 이동 os.chdir('C:/PythonWorkspace/RPA') print(os.getcw..

RPA/Python 2021.05.04

[pyautogui] 메세지 창

import pyautogui # 참고 # 5초 대기, sleep와 다른점은 5, 4, 3, 2, 1 카운트다운 표시됨 print('미사일 발사 카운트 다운...') pyautogui.countdown(5) print('Fire!!!!!') # 자동화 진행 도중 사람의 개입이 필요한 순간에 메시지 박스로 알려주거나 확인받아야 할 경우 # alert 창 pyautogui.alert('자동화 진행 도중 알 수 없는 에러로 실패하였습니다.', '실패 경고') # confirm 창 con = pyautogui.confirm('계속 진행하시겠습니까?', '확인') # 확인 : OK, 취소 : Cancel print(con) # 사용자 값 입력 대기 ip = pyautogui.prompt('다음 진행할 숫자를 입력..

RPA/Python 2021.05.04

[pyautogui] 키보드 입력

import pyautogui # 메모장을 찾아 글자 입력 # 메모장 정보 가져옴 win = pyautogui.getWindowsWithTitle('제목 없음')[0] # 창 활성화 win.activate() # 글자 입력 후 개행 pyautogui.write('Hello\n') # 숫자 입력 후 개행, 하나 입력마다 0.2초 간격 pyautogui.write('12345\n', interval=0.2) # 한글 입력 불가능함 # pyautogui.write('한글') # 한글 입력하기 위해서는 클립보드에 복사 후 붙여넣기 방법으로 해야 함 # 함수로 만들어서 사용하면 편함 # Terminal> pip install pyperclip import pyperclip # '한글 테스트'를 클립보드에 복사 p..

RPA/Python 2021.05.04
반응형