히공

트능 selenium으로 구현 본문

완성품

트능 selenium으로 구현

heegong 2020. 7. 14. 23:25
728x90

트능을 selenium, bs4를 이용해서 웹사이트에 들어가지 않고 콘솔창에서 테스트 할수있게 만들어봤습니다.

 

 

 

 

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from bs4 import BeautifulSoup
from selenium import webdriver
import os
 
 
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument("--disable-gpu")
options.add_argument("lang=ko_KR")
driver = webdriver.Chrome('C:\\chromedriver\\chromedriver.exe',chrome_options=options)
driver.implicitly_wait(1)
driver.get('https://www.trendtest.co.kr/Test')
driver.implicitly_wait(2)
 
os.system('cls')
for j in range(16):
    page_source = driver.page_source
    soup = BeautifulSoup(page_source,"html.parser")
    choose_4 = soup.find_all("ul",class_="flex_box flex_spc_btw answer_box")
    choose_4 = choose_4[j].find_all('li')
    problem = []
    for i in range(len(choose_4)):
        problem_choose = str(choose_4[i].find_all("span")[1]).replace('<span class="fw700 label">','').\
        replace('</span>','')
        problem.append(problem_choose)
    problem_discription = soup.find_all("h3",class_="fw400 tit")[j]
    problem_discription = str(problem_discription).replace('<h3 class="fw400 tit">','')
    problem_discription = problem_discription.replace('<span class="fc_lgrey">','')
    problem_discription = problem_discription.replace('</span>','')
    problem_discription = problem_discription.replace('</h3>','')
    problem_discription = problem_discription.strip()
    print(str(j+1)+"번   "+problem_discription+"\n")
    for i in range(len(problem)):
        print(f"{i+1} : {problem[i]}")
    choose_input = int(input("입력 : "))
    print("\n\n\n\n\n\n\n")
    driver.find_elements_by_class_name("box")[j*4+choose_input-1].click()
    driver.implicitly_wait(1)
    driver.find_element_by_xpath(f'//*[@id="form1"]/section/div[2]/div[{j+1}]/div[2]/a').click()
    if j==15:
        soup = BeautifulSoup(driver.page_source,'html.parser')
        point = soup.select('span.count_num')[0]
        point = str(point).replace('<span class="count_num" data-count=','')
        point = point[1:point.rfind('"')]
        
        level = soup.find_all('h3',class_="fw700 tit")[0]
        level = str(level)
        level = level.replace('<h3 class="fw700 tit"','')
        level = level.replace('<br/>','\n')
        level = level.replace('</h3>','')
        level = level[1:]
 
        final_message = soup.select('p.txt')[0]
        final_message = str(final_message)
        final_message = final_message.replace('"','')
        final_message = final_message.replace('<br/>','')
        final_message = final_message.replace('<br>','\n')
        final_message = final_message.replace('<p class=txt>','')
        final_message = final_message.replace('</p>','')
        print(f"점수 : {point}\n레벨 : {level}\n\n{final_message}")
 
driver.quit()
cs

 

이런식으로구현했습니다.

Comments