본문 바로가기
프로그래밍/파이썬

selenium(셀레니움) 컨트롤

by 젤리씨 2021. 3. 25.
728x90

 

 

11번가 셀러오피스에서 송장 인쇄용 엑셀 다운로드

 

 

 

from selenium import webdriver

from urllib.parse import quote_plus

from bs4 import BeautifulSoup

import time

import os

import shutil

from selenium.webdriver.common.by import By

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.support.ui import WebDriverWait as wait

from selenium.webdriver.common.action_chains import ActionChains

from selenium.webdriver.common.keys import Keys

 

st_id = "11번가 아이디"

st_pw = "비밀번호"

 

def wait_for_download_and_rename(newFilename):

    # function to wait for all chrome downloads to finish

    def chrome_downloads(drv):

        if not "chrome://downloads" in drv.current_url: # if 'chrome downloads' is not current tab

            drv.execute_script("window.open('');"# open a new tab

            drv.switch_to.window(driver.window_handles[1]) # switch to the new tab

            drv.get("chrome://downloads/"# navigate to chrome downloads

        return drv.execute_script("""

            return document.querySelector('downloads-manager')

            .shadowRoot.querySelector('#downloadsList')

            .items.filter(e => e.state === 'COMPLETE')

            .map(e => e.filePath || e.file_path || e.fileUrl || e.file_url);

            """)

    # wait for all the downloads to be completed

    dld_file_paths = wait(driver, 1201).until(chrome_downloads) # returns list of downloaded file paths

    # Close the current tab (chrome downloads)

    if "chrome://downloads" in driver.current_url:

        driver.close()

    # Switch back to original tab

    driver.switch_to.window(driver.window_handles[0]) 

    # get latest downloaded file name and path

    dlFilename = dld_file_paths[0# latest downloaded file from the list

    # wait till downloaded file appears in download directory

    time_to_wait = 20 # adjust timeout as per your needs

    time_counter = 0

    while not os.path.isfile(dlFilename):

        time.sleep(1)

        time_counter += 1

        if time_counter > time_to_wait:

            break

    # rename the downloaded file

    shutil.move(dlFilename, os.path.join(download_dir,newFilename))

    return

download_dir = r'c:\11st'

chrome_options = webdriver.ChromeOptions()

chrome_options.add_experimental_option('prefs', {

    "download.default_directory": download_dir, # Set own Download path

    "download.prompt_for_download"False# Do not ask for download at runtime

    "download.directory_upgrade"False# Also needed to suppress download prompt

    #"plugins.plugins_disabled": ["Chrome PDF Viewer"], # Disable this plugin

    "plugins.always_open_pdf_externally"False# Enable this plugin

    })



driver = webdriver.Chrome("./chromedriver.exe")

 

#크롬창 최대화

driver.maximize_window()

 

#URL 접속

driver.get("https://login.11st.co.kr/auth/front/selleroffice/login.tmall"




#로그인

input_id = driver.find_element_by_id('user-id')

input_pw = driver.find_element_by_id('passWord')

input_id.send_keys(st_id)

input_pw.send_keys(st_pw)

driver.find_element_by_xpath('/html/body/div[1]/form[1]/fieldset/button').click()

 

time.sleep(5)

 

#배송관리 들어가기

driver.find_element_by_xpath('//*[@id="soContent"]/div[2]/div/div[2]/div[2]/div/div[2]/ul/li[3]/em/a').click()

time.sleep(5)




#아이프레임안에 들여다보기

driver.switch_to_frame("Content_ifrm_35930")

time.sleep(5)

 

#팝업닫기는 작동이 안되어 주석처리

#driver.find_element_by_xpath('//*[@id="ext-gen6"]/div/button').submit()

 

time.sleep(5)

driver.find_element_by_xpath('//*[@id="searchform"]/div/div[1]/div[6]/div/a[2]').send_keys(Keys.ENTER)

 

#팝업창 체크

print(driver.window_handles)

 

#팝업창으로 전환

driver.switch_to_window(driver.window_handles[1])

 

time.sleep(5)

 

#송장 출력용 엑셀다운로드 클릭

driver.find_element_by_xpath('/html/body/div/div[2]/div[4]/div/a[1]').send_keys(Keys.ENTER)

time.sleep(5)



driver_len = len(driver.window_handles)

 

if driver_len > 2:

    #엑셀 없음

    driver.quit()

else:

    #엑셀 있음 다운로드한 파일 경로및 파일명 변경

    wait_for_download_and_rename(st_id+'.xls')

    driver.quit()

 

 

참고 유툽 

https://www.youtube.com/watch?v=36mSjaEH-hw&t=281s

728x90

'프로그래밍 > 파이썬' 카테고리의 다른 글

파이썬 exe 파일 만들기  (0) 2021.03.25
window_handles 개수체크  (0) 2021.03.25
파이썬 time 함수  (0) 2020.03.17
파이썬 input 입력함수  (0) 2020.03.17
파이썬 웹브라우저 띄우기  (0) 2020.03.07

댓글