Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 부트스트랩 #Bootstrap #웹개발첫걸음 #스파르타코딩클럽
- 스파르타코딩클럽 #크롤링 #스크래핑
- #내일배움단 #코딩프로젝트 #국비지원 #내일배움카드 #스파르타코딩클럽
- 스파르타코딩클럽 #코딩 #jQuery #Ajax
- 항해99솔직후기 #항해99 #부트캠프추천
Archives
- Today
- Total
이모저모
python - selenium, element 찾기, 클릭 (or 엔터키) 본문
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("window-size=1000,1000")
options.add_argument("no-sandbox")
# options.add_argument("headless") # 화면 띄우기 없음
chrome = webdriver.Chrome("./chromedriver", options=options)
chrome.get("https://shopping.naver.com")
# 요소 찾기
# elem = chrome.find_element_by_css_selector("input[name=query]")
# wait과 함께 요소 찾기 : (1)기다리기 (2)찾기
wait = WebDriverWait(chrome, 10)
# 이 아이의 반환값도 사실 elem 찾는 거라고 함
elem = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input[name=query]")))
# By.<??> 다른 방법들
# ID = "id"
# XPATH = "xpath"
# LINK_TEXT = "link text" # 이거 신기한 방법인 것 같아.
# PARTIAL_LINK_TEXT = "partial link text"
# NAME = "name" # name은 검색어 등 form data에서 꽤 중요한 역할을 하기 때문에, id/class아니어도 속성 중에 중요.
# TAG_NAME = "tag name"
# CLASS_NAME = "class name"
# CSS_SELECTOR = "css selector"
print(elem)
chrome.close()
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("window-size=1000,1000")
options.add_argument("no-sandbox")
# options.add_argument("headless") # 화면 띄우기 없음
chrome = webdriver.Chrome("./chromedriver", options=options)
chrome.get("https://shopping.naver.com")
# 요소 찾기
# elem = chrome.find_element_by_css_selector("input[name=query]")
# wait과 함께 요소 찾기 : (1)기다리기 (2)찾기
wait = WebDriverWait(chrome, 10)
def find_by_css_selector(wait, css_selector):
return wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, css_selector)))
def find_by_a_text(wait, a_text):
return wait.until(EC.presence_of_element_located((By.LINK_TEXT, a_text)))
# # 검색어 입력하고 검색하기
# # (1) 검색버튼 찾아 클릭
search = find_by_css_selector(wait, "input[name=query]")
search.send_keys("아이맥")
time.sleep(2)
search_btn = find_by_a_text(wait, "네이버 검색")
# search_btn = find_by_css_level(wait, "a.co_srh_btn")
search_btn.click()
time.sleep(3)
# (2) enter 버튼
search = find_by_css_selector(wait, "input[name=query]")
search.send_keys("아이맥\n") # 줄바꿈 기호를 넣으면 enter!
time.sleep(2)
chrome.close()
'coding > python' 카테고리의 다른 글
python - selenium, login (0) | 2022.01.03 |
---|---|
python - selenium_요소와 interaction위한 탐색시 주의사항(?!) (0) | 2022.01.03 |
python - selenium, 페이지 로딩 체크하기 (0) | 2022.01.03 |
python - requests, bs4 실습 (0) | 2022.01.01 |
python - 크롤링에서 정규식 사용예시 (0) | 2021.12.31 |
Comments