Phython Selenium 사용법 정리해 봅니다.

 

셀레니움 설치 명령어 실행 cmd 창에서 해도 되고 vscode의 Ctrl + Shift + ` 를 눌러서 터미널에서 실행해도 됩니다.

pip install selenium

 

 

셀레니움을 사용하려면 자신이 사용할 브라우저의 드라이브가 필요하다.

크롬

https://sites.google.com/a/chromium.org/chromedriver/downloads

 

Downloads - ChromeDriver - WebDriver for Chrome

WebDriver for Chrome

sites.google.com

파이어폭스

https://github.com/mozilla/geckodriver/releases

 

 

Releases · mozilla/geckodriver

WebDriver for Firefox. Contribute to mozilla/geckodriver development by creating an account on GitHub.

github.com

엣지

https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

 

Microsoft Edge Driver - Microsoft Edge Developer

canary Channel Daily release of our latest features and fixes. Version: 99.0.1135.0: x86 | x64 | ARM64

developer.microsoft.com

사파리

https://webkit.org/blog/6900/webdriver-support-in-safari-10/

 

WebDriver Support in Safari 10

Starting with Safari 10 on OS X El Capitan and macOS Sierra, Safari comes bundled with a WebDriver implementation that's maintained by the Web Developer Experience team at Apple.

webkit.org

 

이주 한 브라우저를 선택하고 버전도 자신이 필요로 하는 버전을 선택하여 프로그램에 응용을 합니다.

여기서 예제로 사용한 드라이브는 파이어폭스 0.30.0을 선택 하여 예제를 작성했습니다.

아래 대상을 다운로드합니다.

geckodriver-v0.30.0-win64.zip

https://github.com/mozilla/geckodriver/releases/download/v0.30.0/geckodriver-v0.30.0-win64.zip

필요한 라이브러리 Import 처리합니다.

import selenium
from selenium import webdriver
from selenium.webdriver import ActionChains

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

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

 

다운로드한 드라이브와 파이썬 파일을 아래 디렉터리를 생성하여 저장합니다.

C:\dev\python

위에 Import 한 대상을 "naver_search_my_word_count.py" 로 저장합니다.

드라이브를 지정하고 원하는 페이지를 오픈하도록 코딩합니다.

import selenium
from selenium import webdriver
from selenium.webdriver import ActionChains

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

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


driver = webdriver.Firefox(executable_path='C:\dev\python\geckodriver.exe')
#driver = webdriver.Chrome(executable_path='C:\dev\python\chromedriver.exe')
driver.get(url=URL)
 

프로그램을 실행하면 네이버 페이지로 이동하는 것을 알 수 있습니다.

Posted by 블로그하는프로그래머
,