end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

python v2.7 + selenium + firefox (geckodriver.exe) でfileの自動upload & download

多分、↓こう

#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import getopt
import os
import sys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import traceback
from time import sleep

CONF = {'mile_id':'ないしょ','mile_pw':'ないしょ'}

def main():
    # browser起動
    browser = init_browser()
    # login処理
    if login_group_ware(browser) == False:
        return False
    # file upload
    file_name = "browser_screen_shot.png"
    if upload_file(browser,file_name) == False:
        return False
    # file download
    download_file(browser,file_name)
    # 終了処理
    browser.quit()

    
def download_file(browser,file_name):

    print file_name
    
    browser.get("https://bunsho.mile.sexy.com/lite/folder/204939")

    tr_lists = browser.find_elements_by_class_name("listItemName")
    for tr_list in tr_lists:
        if tr_list.text == file_name:
            a_href = tr_list.find_element_by_tag_name("a")
            a_href.click()
            sleep(5)

            download_link = browser.find_element_by_id("BunshoVS_dlLink")
            download_link.click()
            browser.implicitly_wait(10) # seconds
            return True
    return False

        
def upload_file(browser, file_name):

    browser.get("https://bunsho.mile.sexy.com/lite/folder/204939")


    browser.get("https://bunsho.mile.sexy.com/folder/204939/file/create")
    browser.execute_script('javascript:switch_to_normal_mode()')

    elm_file = browser.find_element_by_id("fp0file")
    elm_file.send_keys("c:\\home\\end0tknr\\tmp\\SELENIUM\\" + file_name)
    
    browser.execute_script('javascript:Creator.create_commit(file_select_mode)')
    
    # seleniumにおけるwaitは以下を参照
    # refer to https://kurozumi.github.io/selenium-python/waits.html
    try:
        WebDriverWait(browser, 10).until(
            EC.title_contains(u"ほげほげ")
        )
    except Exception as e:
#        print(dir(e))
        print e.args
        return False

    return True

    
def init_browser():
    profile = webdriver.FirefoxProfile()

    # 0:desktop, 1:sys規定のfolder, 2:user def folder
    profile.set_preference("browser.download.folderList",2)
    profile.set_preference("browser.download.dir", os.getcwd())
    profile.set_preference("browser.download.manager.showWhenStarting",False)

    # chrome driverでは、download.directory_upgrade=Trueで上書き保存できましたが
    # firefox では、上書き方法不明
    # profile.set_preference("browser.download.directory_upgrade",True)

    # image/png の場合、強制的にdownload (=画面表示しない)
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk","image/png")
    browser = webdriver.Firefox(firefox_profile=profile)

    return browser

    
def login_group_ware(browser):

    browser.get("https://login.mile.sexy.com/login.pl")
    login_id_form = browser.find_element_by_name('credential_0')
    login_id_form.send_keys(CONF['mile_id'])
    login_pw_form = browser.find_element_by_name('credential_1')
    login_pw_form.send_keys(CONF['mile_pw'])

    login_btn = browser.find_element_by_name('Submit')
    login_btn.click()

    if( browser.current_url == "https://top.mile.sexy.com/"):
        return True
    return False


if __name__ == '__main__':
    main()