본문 바로가기
연습/크롤링

[크롤링] 셀레니움_사용_1

by yejineee 2022. 4. 15.

난이도| ★☆☆☆☆

Google에서 파이썬 공식 홈페이지로 이동하기


# 모듈 import 하기

import pandas as pd

import warnings
warnings.filterwarnings('ignore')

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time

# 경로, 드라이버, url 설정하기

# 경로 설정
path = 'C:\chromedriver_win32\chromedriver.exe'
url = 'https://www.google.co.kr/'

# 드라이버 설정
def drive_path(x, path):
    global driver # 전역변수 선언하기
    if x == 'chrome':
        driver = webdriver.Chrome(path)
    else:
        print('driver 새로 설정하세요')

# url 설정
def url_move(url):
    if isinstance(url, str):
        driver.get(url)
    else:
        driver.get(str(url))

# 실행
drive_path('chrome',path)
print('드라이버 설정 완료')
print('-----------------------')

url_move(url)
print('url로 창 열기 완료')
print('-----------------------')
driver.implicitly_wait(10) # seconds 기다리기

# 공식 홈페이지 검색하기

# 텍스트 입력하기
point = driver.find_element_by_xpath('/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')
point.send_keys('파이썬 공식 홈페이지')

# 입력 후 엔터
point.send_keys(Keys.ENTER)

# 파이썬 홈페이지로 이동하기

driver.find_element_by_xpath('//*[@id="rso"]/div[1]/div/div/div/div/div/div[1]/a/h3').click()

# 다운로드 칸 클릭하기

driver.find_element_by_xpath('//*[@id="downloads"]/a').click()

# 드라이버 끝내기

driver.quit()