반응형

[출처] 파이썬 셀레니움4 최신 버전 find_element 사용법 마스터|작성자 김플

 

파이썬 셀레니움4  find_element 사용법

 

selenium3에서 selenium4로 버전업되며 바뀐 find_element 문법에 대해 설명합니다.

 

[셀레니움3] html 요소 찾기

driver.find_element_by_class_name("")

driver.find_element_by_id("")

driver.find_element_by_css_selector("")

driver.find_element_by_name("")

driver.find_element_by_tag_name("")

driver.find_element_by_xpath("")

driver.find_element_by_link_text("")

driver.find_element_by_partial_link_text("")

(복수형 driver.find_elements_by~~)

 

[셀레니움4]

driver.find_element(By.CLASS_NAME, "")

driver.find_element(By.ID, "")

driver.find_element(By.CSS_SELECTOR, "")

driver.find_element(By.NAME, "")

driver.find_element(By.TAG_NAME, "")

driver.find_element(By.XPATH, "")

driver.find_element(By.LINK_TEXT, "")

driver.find_element(By.PARTIAL_LINK_TEXT, "")

(복수형 driver.find_elements(By.~~, "")

 

[소스코드]

from selenium import webdriver 
from selenium.webdriver.common.by import By 
from selenium.webdriver.chrome.options import Options 
from selenium.webdriver.chrome.service import Service 
from webdriver_manager.chrome import ChromeDriverManager 
import time 

options = Options() 
options.add_experimental_option("detach", True) 
service = Service(ChromeDriverManager().install()) 
driver = webdriver.Chrome(service=service, options=options) 

url = "https://naver.com" driver.get(url) 
time.sleep(1) 

""" 
<input id="query" name="query" type="text" title="검색어 입력" maxlength="255" class="input_text" tabindex="1" accesskey="s" style="ime-mode:active;" autocomplete="off" placeholder="검색어를 입력해 주세요." onclick="document.getElementById('fbm').value=1;" value="" data-atcmp-element=""> 
""" 

driver.find_element(By.CLASS_NAME, "input_text").send_keys("블랙핑크") 
time.sleep(1) 
driver.find_element(By.ID, "query").send_keys("뉴진스") 
time.sleep(1) 
driver.find_element(By.NAME, "query").send_keys("트와이스") 
time.sleep(1) 
driver.find_element(By.CSS_SELECTOR, "#query").send_keys("에스파") 
time.sleep(1) 
driver.find_element(By.XPATH, '//*[@name="query"]').send_keys("에스파") 
time.sleep(1) 
driver.find_element(By.LINK_TEXT, "쇼핑LIVE").click() 
driver.find_element(By.PARTIAL_LINK_TEXT, "핑LI").click() 
navs = driver.find_elements(By.CSS_SELECTOR, ".nav") 
for nav in navs: 
    print(nav.get_attribute("outerHTML")) 
    print()

 

https://www.youtube.com/watch?v=qhy8I4ChCuw 

 

반응형

+ Recent posts