====== python selenium ======
[[software_development:python|Python]]
driver.current_url()
driver.getPageSource().contains("Example");
driver.findElement(By.id("Price")).getText()
driver.findElement(By.id("Price")).getAttribute("val");
driver.findElement(By.id("Price")).isEnabled()
==== 크롬, IE ====
from selenium import webdriver
driver_path=' '
driver.webdriver.Chrome(driver_path) #Chrome
driver.webdriver.Ie(driver_path) #IE
=== FireFox ===
https://github.com/mozilla/geckodriver/releases/
from selenium.webdriver.firefox.options import Options
firefox_options = Options()
user_agent = '--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 10_3 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) CriOS/56.0.2924.75 Mobile/14E5239e Safari/602.1'
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", user_agent)
firefox_driver = "c:\\geckodriver.exe"
driver = webdriver.Firefox(executable_path=firefox_driver, firefox_profile=profile)
==== xpath ====
self.driver.find_element_by_xpath('/html/body/div/div[4]/form/div[2]/div[1]/div[3]/center/input[1]')
a[@class='name']
====팝업, 새창 제어====
[[http://www.teachmeselenium.com/2018/04/10/python-selenium-handling-alert-dialog-popup-windows-and-frames-handling-popup-windows/|handling new window(pop-up)]]
current_window_handle = driver.current_window_handle
while len(driver.window_handles) == 1:
time.sleep(1)
window.handles = driver.window_handles
driver.switch_to.window(window_handles[1])
print(driver.title)
====iframe 안 참조하기 ====
driver.switch_to.frame( driver.find_element_by.....(프레임) )
driver.switch_to.default_content()
==== Select / option ===
from selenium.webdriver.support.ui import Select
unit_option = driver.find_element_by_id('selectbox'+str(idx if idx == 1 else idx+1)+'_input_0')
Select(unit_option).select_by_visible_text('백만원')
show_btn = driver.find_element_by_id('btn_anchor'+str(idx))
show_btn.click()
==== Clickable 핸들링하기 ====
로딩 전에 클릭이 안될 때가 있음
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ClassName, "imgover)))
==== alert, msgbox 핸들링 ====
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdrvier.support import expected_conditions as EC
WebDriver.Wait(driver,20).until( EC.alert_is_present() )
driver.switch_to.alert.accept()
# alert 클릭하면 창이 닫힐 때, 원래 창으로 돌아가기
driver.switch_to.window(driver.window_handles[0])
==== 로딩 기다리기 ====
while True:
try:
if 로딩조건
break
else:
time.sleep(0.5)
except ValueError:
time.sleep(0.5)
#TIP
driver.find_element_by_name('ProgressBar').value_of_css_property('display') == True
여러개 뜰 때, 뜰때까지 기다렸다가, 꺼질때까지
def wait_msgs(driver, num_msg):
is_first = 1
was_opened = False
driver.switch_to.frame(driver.find_element_by_name('FRAME_MATRIX'))
driver.switch_to.frame(driver.find_element_by_name('FRAME_MATRIX'))
while True:
progress_bar = driver.find_element_by_name('ProgressBar').value_of_css_property("display")
is_visible = progress_bar != 'none'
if is_visible:
if not was_opened:
print(str(is_first)+"-opened")
was_opened = True
else:
if is_first > num_msg:
break
if not was_opened: # 아직 안열렸으면
print("wait for "+str(is_first)+ " to open")
continue
else:
was_opened = False
print(str(is_first)+"-closed")
is_first += 1
driver.implicitly_wait(1)
driver.switch_to.default_content()
#driver.switch_to.default_content()
====clusterize.js 사용한 페이지 크롤링====
while (스크롤 높이와 scrollHeight와 비교) - if( tr 이 바뀔 때 ) - 스크롤( height*iter )
- 스크롤시 약간의 sleep을 줘야 함 ([[https://stackoverflow.com/questions/27003423/python-selenium-stale-element-fix|https://stackoverflow.com/questions/27003423/python-selenium-stale-element-fix]])
- exception 처리 [[http://allselenium.info/handle-stale-element-reference-exception-python-selenium/|http://allselenium.info/handle-stale-element-reference-exception-python-selenium/]]
driver.execute_script('return //clusterize1//.getRowsAmount()') #clusterize 는 페이지 내 객체명 #총 개수
driver.execute_script('return clusterize1.getScrollProgress()') # 얼마나 스크롤 했는지
driver.execute_script('document.getElementById(//"Container"//).scrollTop = 500') #스크롤하기
{{tag>software_development python selenium 파이썬 크롤링 스크래핑}}
~~DISCUSSION~~