4,475,465 th visitor since 2017.2.1 ( Today : 6190 )
Programming
No. 828
Name. swindler
Subject. Python Selenium
Main Cate. etc
Sub Cate.
Date. 2022-06-08 13:34
Hit. 9929 (121.134.199.74)
File.
from selenium import webdriver as wd
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ES
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys


-------- 초기화
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
driver = wd.Chrome(chrome_driver, chrome_options=chrome_options)
actions = ActionChains(driver)
http_base = requests.Session()


-------- 스크롤 관련

# PageDown
driver.find_element_by_tag_name("body").send_keys(Keys.PAGE_DOWN)

new_height = driver.execute_script("return document.documentElement.scrollTop")

driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')

321 픽셀만큼 scroll
driver.execute_script("window.scrollBy(0, -321);")



--------- element 찾기
elements = driver.find_elements_by_css_selector("li._aaei")

driver.find_element_by_css_selector("div._aa6e")

article.find_element_by_css_selector("div._aagv > img")

elements = driver.find_elements_by_xpath('//a[@href]')
for elem in elements:
urls = elem.get_attribute('href')

driver.find_elements_by_class_name("_9zm2")


# text 추출
elm.find_element_by_css_selector("div._aaep").find_element_by_css_selector("span._aacl").text


------- 액션
driver.execute_script("arguments[0].click();", next_btn[0])

특정좌표 클릭
actions.move_by_offset(10,10).click().perform()

특정 element의 특정 좌표 클릭
actions.move_to_element(close_btn).move_by_offset(1,1).click().perform()


------- html 소스
object의 경우 : elem.get_attribute("innerHTML")
페이지 전체 : driver.page_source

------ Wait 관련

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//svg[@class="x1lliihq"]')))



---- 현재 url 가져오기
driver.current_url


[바로가기 링크] : http://coolx.net/cboard/develop/828



Name
Password
Comment
Copyright © 1999-2017, swindler. All rights reserved. 367,611 visitor ( 1999.1.8-2004.5.26 ), 2,405,771 ( -2017.01.31)

  2HLAB   2HLAB_Blog   RedToolBox   Omil   Omil_Blog