import Database

import selenium.webdriver as selWeb

import time

from bs4 import BeautifulSoup

import re

#=======================================

class Center(Database.DB):

    def __init__(self, url): # 생성자

        Database.DB.__init__(self)

        self.targetURL = url


        # selenium

        self.webDriver = selWeb.Chrome('C:\\Users\\sleep\\Desktop\\chrom_driver\\chromedriver.exe')


        # naver_voice_number

        self.naverVoiceNumber = ""


        # filter

        self.textFilter = re.compile("^새로운 기기\(브라우저\)에서 로그인 되었습니다.")

    # func 01

    def urlRequset_center(self):

        self.webDriver.get(self.targetURL)

        time.sleep(2) # 지연

        self.webDriver.find_element_by_xpath('//*[@id="account"]/div/a/i').click()

        time.sleep(2)  # 지연

        #ID / PW 값 넣기

        self.webDriver.find_element_by_id('id').send_keys(self.usrInfo['id'])

        self.webDriver.find_element_by_id('pw').send_keys(self.usrInfo['pw'])

        self.webDriver.find_element_by_xpath('//*[@id="frmNIDLogin"]/fieldset/input').click()

        time.sleep(2)  # 지연

        try:

            # capcha 부분 자동 로그인 방지

            self.webDriver.find_element_by_xpath('//*[@id="image_captcha"]/a[2]/span').click()

            while not self.naverVoiceNumber:

                self.naverVoiceNumber = input("number input :  ")


            self.webDriver.find_element_by_id('chptcha').send_keys(self.naverVoiceNumber)

            self.webDriver.find_element_by_id('pw').send_keys(self.usrInfo['pw'])

            self.webDriver.find_element_by_xpath('//*[@id="login_submit"]').click()

        except:

            # 등록/ 등록안함

            self.webDriver.find_element_by_xpath('//*[@id="frmNIDLogin"]/fieldset/span[2]/a').click()

        else:

            # 등록/ 등록안함

            self.webDriver.find_element_by_xpath('//*[@id="frmNIDLogin"]/fieldset/span[2]/a').click()

        time.sleep(2)

        # 메일 클릭

        #  selenium이 span 태그 제어 못함 그래서 find_element_by_link_text를 사용함

        self.webDriver.find_element_by_link_text("메일").click()

        # self.webDriver.find_elements_by_xpath('//*[@id="PM_ID_ct"]/div[1]/div[2]/div[1]/ul[1]/li[1]/a/span[2]').click()

        time.sleep(2)

        # 페이지 클릭


        #

        # //*[@id="normalPagingNav"]/span[1]/strong

        # for p in range(2, 11):

        #     tmpHtml = self.webDriver.page_source

        #     tmpObject = BeautifulSoup(tmpHtml, 'html.parser')

        #     t = tmpObject.find_all('ul',{"class":"mInfo"})


        for p in range(2, 11):

            self.webDriver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

            tmpHtml = self.webDriver.page_source

            tmpObject = BeautifulSoup(tmpHtml, "html.parser")

            # t = tmpObject.find_all('div', {'class':'mTitle'})

            t = tmpObject.find_all('strong', {'class':re.compile("^mail_title")})

            cunt = 0x0

            for i in t:

                if self.textFilter.search(i.text[6:]):

                    cunt += 1

                    print(cunt, " : ",i.text[6:], " : ", i.attrs['mailsn'])

                    self.webDriver.find_element_by_xpath('//*[@id="mailCheck_{}"]'.format(i.attrs['mailsn'])).click()

                    time.sleep(2)

                    # 이메일 삭제

                    self.webDriver.find_element_by_xpath('//*[@id="listBtnMenu"]/div[1]/span[2]/button[2]').click()

                    time.sleep(2)

                #     //*[@id="mailCheck_19085"]

                # tempEmail = ""

                # j = i.find("div", {'class':'name _ccr(lst.from) '})

                # tempEmail += j.text

                #

                # j = i.find("div", {'class':'subject'})

                # tempEmail += j.text

                # print (tempEmail)


                # if self.textFilter.findall(tempEmail):

                #     checkBox = tmpObject.find_all('ul', {"class": "mInfo"})

                #     tempV = [i.select_one('input') for i in checkBox]

                #     print ("=========> {}".format(tempV))


            self.webDriver.find_element_by_link_text("{}".format(p)).click()

            time.sleep(4)


def main():

    sNode = Center("https://www.naver.com/")

    sNode.urlRequset_center()

if __name__ == "__main__":

    main()




'언어 > python' 카테고리의 다른 글

pygame _ 연습2  (0) 2018.10.27
pygame _ 연습중  (0) 2018.10.27
python - selenium + urllib  (0) 2018.10.26
pygrame01  (0) 2018.10.24
selenium + 잡코리아 크롤링 + openpyxl  (0) 2018.10.24