크롬웹드라이버 테스트 케이스 작성

1.파이썬 설치

32비트용 : http://python.org/ftp/python/2.7.6/python-2.7.6.msi
64비트용 : http://python.org/ftp/python/2.7.6/python-2.7.6.amd64.msi


1.1 설치 프로그램을 완료

1.2 경로에 파이썬 추가
 win+r   cmd 실행

 setx PATH "C:\Python27;C:\Python27\Scripts;C:\Python27\Lib\site-packages"

 setx 가 안먹는다. 수동으로 path 에 추가 할려고 환경변수 열었더니 잡혀있음.
cmd 창 새로 열고 python 입력 하니 잘 동작.cmd 창을 재시작 해야 하는 거였음.


1.3 pip 설치


       > python get-pip.py


1.4 test.py 작성 ( 피터윤님 코드 : https://medium.com/@peteryun/python-selenium%EC%9D%84-%ED%99%9C%EC%9A%A9%ED%95%9C-%ED%81%AC%EB%A1%A4%EB%9F%AC-%EB%A7%8C%EB%93%A4%EA%B8%B0-b055cefd1195)

import time
from selenium import webdriver
import xlrd
from xlutils.copy import copy
from selenium.common.exceptions import NoSuchElementException,StaleElementReferenceException
import os
# driver로 wiki 접속
driver = webdriver.Chrome(executable_path="C:/Users\yunhwan\workspace\crazy\chromedriver.exe")
for i in range(1,101):
driver.get("https://google.com")
# 엑셀 파일 열기
wb = xlrd.open_workbook('test.xls')
sheet = wb.sheet_by_index(0)
copy_wb = copy(wb)
copy_sheet = copy_wb.get_sheet(0)
# 검색 키워드 셀에서 가져오기
song = sheet.cell(i, 1).value
artist = sheet.cell(i, 2).value
keyword = str(song) + ' ' + str(artist) #숫자인 경우가 있어서 str()
#구글 검색
elem = driver.find_element_by_id('lst-ib')
#공통
elem.send_keys(keyword)
elem.submit()
##### 구글 검색 결과 페이지 #####
try:
box = driver.find_element_by_xpath("//div[@id='rso']/div[2]/div")
list = box.find_elements_by_tag_name('h3')
for item in list :
#print(item.text)
if( 'Wikipedia' in item.text ):
p = item.find_element_by_tag_name('a')
p.click()
break
except NoSuchElementException:
box = driver.find_element_by_xpath("//div[@id='rso']/div/div")
list = box.find_elements_by_tag_name('h3')
for item in list:
# print(item.text)
if ('Wikipedia' in item.text):
p = item.find_element_by_tag_name('a')
p.click()
break
##### 위키 곡정보 페이지 #####
# writer,producer
print("//////////////////////////// " + str(i) + "행을 크롤링중입니다.")
print("//////////////////////////// " + "노래명, 가수 : " + keyword)
try:
table = driver.find_element_by_tag_name('table')
tbody = table.find_element_by_tag_name("tbody")
trs = tbody.find_elements_by_tag_name("tr")
except NoSuchElementException:
print(" [예외 발생] 표 없음 ")
continue
for tr in trs:
if "Writer(s)" in tr.text:
print(tr.text)
a = ""
if tr.find_elements_by_tag_name("li"):
lis = tr.find_elements_by_tag_name("li")
for li in lis:
a = a + "," + li.text
else:
o = tr.find_elements_by_tag_name("td")
a = a + "," + o[0].text
a = a[1:]
copy_sheet.write(i, 3, a)
if "Producer" in tr.text:
print(tr.text)
a = ""
if tr.find_elements_by_tag_name("li"):
lis = tr.find_elements_by_tag_name("li")
for li in lis:
a = a + "," + li.text
else:
o = tr.find_elements_by_tag_name("td")
a = a + "," + o[0].text
a = a[1:]
copy_sheet.write(i, 4, a)
#저장
copy_wb.save('test.xls')


1.5 실행

> python test.py

D:\testautomation>python test.py
  File "test.py", line 9
SyntaxError: Non-ASCII character '\xb7' in file test.py on line 9, but no encodi
ng declared; see http://www.python.org/peps/pep-0263.html for details

한글을 인식 못해서 나는 에러


맨위쪽에 다음 라인 추가
#-*- coding: utf-8 -*-

1.6 다시 실행
> python test.py

D:\testautomation>python test.py
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    from selenium import webdriver
ImportError: No module named selenium

모듈 없다고 에러남. 모듈 설치 어케 하는 거임? ( https://wikidocs.net/5763 )

> pip install selenium

1.7 또 다시 실행
python test.py
D:\testautomation>python test.py
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    import xlrd
ImportError: No module named xlrd

우씨 모듈들 다 설치 ... 해야 되넹

> pip install selenium xlrd xlutils

1.8 다시 실행

D:\testautomation>python test.py
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    driver = webdriver.Chrome(executable_path="D:/testautomation/chromedriver.ex
e")
  File "C:\Python27\lib\site-packages\selenium\webdriver\chrome\webdriver.py", l
ine 62, in __init__
    self.service.start()
  File "C:\Python27\lib\site-packages\selenium\webdriver\common\service.py", lin
e 81, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' execu
table needs to be in PATH. Please see https://sites.google.com/a/chromium.org/ch
romedriver/home

훔 크롬웹드라이버도 패스 추가 해야 됨 . 그냥 파이썬 설치 폴더에 복사 해버리자. ㅋㅋ


2. 자바로 작성 하기

https://examples.javacodegeeks.com/core-java/selenium-chromedriver-tutorial/

maven 프로젝트로 되어 있음

이클립스 메이븐 Maven dependency lib export 추출 방법
프로젝트 - 우클릭 - Run As -Maven Build... 
Goals 에 dependency:copy-dependencies 추가  
Run 실행
project- target - dependency folder 에 라이브러리 파일 떨어짐


2.1 스크린샷 하기

https://github.com/assertthat/selenium-shutterbug

위 두 소스 묶어서 배포 할것

Shutterbug.shootPage(driver, ScrollStrategy.BOTH_DIRECTIONS).save("C:\\testing\\screenshots\\");


3. 테스트 케이스 작성

4. 테스트


댓글