๐ Slenium ํ์ ํ๊ธฐ
๐ฉ selenium link
https://www.selenium.dev/documentation/
The Selenium Browser Automation Project
Selenium automates browsers. That's it!
www.selenium.dev
https://selenium-python.readthedocs.io/index.html
Selenium with Python — Selenium Python Bindings 2 documentation
Note This is not an official documentation. If you would like to contribute to this documentation, you can fork this project in GitHub and send pull requests. You can also send your feedback to my email: baiju.m.mail AT gmail DOT com. So far 60+ community
selenium-python.readthedocs.io
๐ฉ selenium import
!pip install selenium # selenium ์ค์น
from selenium import webdriver # selenium web driver import
driver = webdriver.Chrome() # webdriver browser select
driver.get('https://www.naver.com') # open url
๐ฉ selenium ๊ด๋ จ ๋ช ๋ น์ด
- quit()
- maximize_window(), minimize_window(), set_window_size(width, height)
- refresh(), back(), forward()
driver.quit() # browser ๋๊ฐ๊ธฐ
# ํ๋ฉด ์ต๋ ํฌ๊ธฐ ์ค์ (ํ๋ฉด ์ต๋ํ)
driver.maximize_window()
# ํ๋ฉด ์ต์ ํฌ๊ธฐ ์ค์ (ํ๋ฉด ์ต์ํ)
driver.minimize_window()
# ํ๋ฉด ํฌ๊ธฐ ์ค์ (width, height)
driver.set_window_size(600,600)
# ์๋ก๊ณ ์นจ
driver.refresh()
# ๋ค๋ก ๊ฐ๊ธฐ
driver.back()
# ์์ผ๋ก ๊ฐ๊ธฐ
driver.forward()
๐ฉ selenium, find_element ๊ด๋ จ ๋ช ๋ น์ด
- By.CSS_SELECTOR
- ๊ฐ๋ฐ์ ๋๊ตฌ์์ ์ํ๋ ๋งํฌ ์ฃผ์๋ฅผ copyํ ์ ์๋ค.
- click()
# ํด๋ฆญ
from selenium.webdriver.common.by import By
first_content = driver.find_element(By.CSS_SELECTOR, '#content > div.cover-masonry > div > ul > li:nth-child(2)')
first_content.click()
- switch_to.window(driver.window_handles[2]
- (2๋ฒ์งธ๋ก ์ง์ ํ ํญ์ผ๋ก ์ด๋)
- close()
- tap์ ๊ธฐ์ค์ ์ก๊ณ ๊ทธ ํญ์ closeํด์ค๋ค (quit์ ์์ ์ข ๋ฃ)
# ์๋ก์ด ํญ ์์ฑํ๊ธฐ, (java ์คํฌ๋ฆฝํธ๋ฅผ ์ฐ๋ ๋ฌธ๋ฒ)
driver.execute_script('window.open("http://www.naver.com")')
# ํญ ์ด๋
driver.switch_to.window(driver.window_handles[2])
# quit๊ณผ ์ฐจ์ด์ - tap์ ๊ธฐ์ค์ ์ก๊ณ ๊ทธ ํญ๋ง close
driver.close()
๐ฉ selenium, scroll
- ์คํฌ๋กค ๊ฐ๋ฅํ ์ต๋ ๊ธธ์ด ๊ฒ์
# ์คํฌ๋กค ๊ฐ๋ฅํ ๋์ด (์ต๋ ๊ธธ์ด)
# ์๋ฐ ์คํฌ๋ฆฝํธ ์ฝ๋๋ก ์คํ
driver.execute_script('return document.body.scrollHeight')
# ํ๋ฉด ์คํฌ๋กค ํ๋จ ์ด๋ (์ ์ผ ์์์๋ถํฐ ๋งจ ๋ง์ง๋ง ๊น์ง)
driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')
# ํ๋ฉด ์คํฌ๋กค ์๋จ ์ด๋ (๋งจ ์๊น์ง ๊น์ง)
driver.execute_script('window.scrollTo(0,0);')
- ๋ณด์ด๋ ํ๋ฉด ์คํฌ๋ฆฐ ์ท ์ ์ฅ
# ํ์ฌ ๋ณด์ด๋ ํ๋ฉด ์คํฌ๋ฆฐ ์ท ์ ์ฅ
driver.save_screenshot('./last_height.png')
๐ฉ ActionChains
- ํน์ ํ๊ทธ ์ง์ ๊น์ง ์คํฌ๋กค ์ด๋
# ํน์ ํ๊ทธ ์ง์ ๊น์ง ์คํฌ๋กค ์ด๋
from selenium.webdriver import ActionChains
some_tag = driver.find_element(By.CSS_SELECTOR, '#content > div:nth-child(2) > div > ul > li:nth-child(6)')
action = ActionChains(driver) # ์ฐ๋ฆฌ๊ฐ ์ฌ์ฉํ๊ณ ์๋ driver๋ฅผ ์ ์ดํ๊ฒ ๋ค.
action.move_to_element(some_tag).perform()
๐ฉ CSS.SELECTOR
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.naver.com')
keyword = driver.find_element(By.CSS_SELECTOR, '#query') # ๋ค์ด๋ฒ ๊ฒ์ ์ฐฝ html ์์ค๋ฅผ css ์
๋ ํฐ๋ก ์ง์
keyword.clear() # ํ ๋ฒ ๊ฒ์์ฐฝ์ ๋น์ด๋ค
keyword.send_keys('ํ์ด์ฌ') # ๊ฒ์์ด๋ฅผ ๋ณด๋ธ๋ค
search_btn = driver.find_element(By.CSS_SELECTOR, '#search-btn') # ๊ฒ์๋ฒํผ CSS ์
๋ ํฐ ํ์ฑํ
search_btn.click() # ํด๋ฆญ
'//': ์ต์์
'/' : ์์ ํ๊ทธ '*' : ์์ ํ๊ทธ //*[@id="main_pack"]/section[3]/div/div[2]/panel-list/div/ul/li[3]/div/div/a ๊ฐ๋ฐ์ ๋๊ตฌ์์ ๊ฒฝ๋ก๋ฅผ ํ์ธํ ์ ์๋ค. ![]() |
๐ฉ CSS.SELECTOR
- ๋์ ํ์ด์ง (๋๊ทธ๋ผ๋ฏธ ๋ถ๋ถ ํ์ธ!) ์ผ๋๋ ActionChains๋ฅผ ํ์ฉํ ์ ์๋ค.
# 1. ๋๋ณด๊ธฐ ๋ฒํผ ์ ํ
from selenium.webdriver import ActionChains
search_tag = driver.find_element(By.CSS_SELECTOR, '.search')
action = ActionChains(driver)
action.click(search_tag)
action.perform()
# 2. ๊ฒ์์ด ์
๋ ฅ
driver.find_element(By.CSS_SELECTOR, '#header > div.search > input[type=text]').send_keys('ํ์ด์ฌ')
# 3. ๊ฒ์ ๋ฒํผ ์
๋ ฅ
driver.find_element(By.CSS_SELECTOR, '#header > div.search.on > button').click()
'Study_note(zb_data) > EDA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์คํฐ๋ ๋ ธํธ (BeautifulSoup + selenium 2) (0) | 2023.08.14 |
---|---|
์คํฐ๋ ๋ ธํธ (BeautifulSoup + selenium) (1) | 2023.08.14 |
์คํฐ๋ ๋ ธํธ (BeautifulSoup Web data 2-3) (0) | 2023.08.12 |
์คํฐ๋ ๋ ธํธ (BeautifulSoup Web data 2-2) (0) | 2023.08.12 |
์คํฐ๋ ๋ ธํธ (BeautifulSoup Web data 2-1) (0) | 2023.08.11 |