1. 程式人生 > >selenium2學習:上傳文件、下載文件(待續)

selenium2學習:上傳文件、下載文件(待續)

utf-8 click ica AS fin code ear chrome pop

1.1 上傳文件

1.1.1 普通上傳:Send_keys實現上傳

找到上傳的input標簽,通過send_keys向其輸入一個文件地址實現上傳。

Driver.find_element_by_name(‘file’).send_keys(‘D:\01Test\1.txt’)

如qq郵箱:

技術分享圖片

#寫信鏈接
        driver.switch_to_default_content()
        driver.find_element_by_id("composebtn").click()
        #正文
        driver.switch_to_frame(
mainFrame) #driver.find_element_by_id("toAreaCtrl").click() driver.find_element_by_xpath("//*[@id=‘toAreaCtrl‘]/div[2]/input").send_keys("[email protected]") driver.find_element_by_id("subject").send_keys("主題test") driver.find_element_by_xpath("//*[@id=‘AttachFrame‘]/span/input
").send_keys(D:\\register.txt) time.sleep(6) #避免附件未上傳完畢,所以等待6秒鐘 driver.find_element_by_link_text("發送").click()

1.1.2 插件上傳:AutoIt實現上傳

AutoIt下載:https://www.autoitscript.com/site/autoit/downloads/

技術分享圖片

l AutoIt Windows Info:用於識別windows控件信息

l Compile Script to.exe:用於將AutoIt生成exe執行文件

l Run Script:用於執行AutoIt腳本

l SciTE Script Editor:用於編寫AutoIt腳本

1.1.1.1 識別元素

打開AutoIt Windows Info,拖動finder tool 至需要識別的元素,如文件上傳框中的文件名選擇框和打開按鈕。

技術分享圖片

技術分享圖片

1.1.1.2 編輯腳本

打開SciTE Script Editor編輯器,編寫AutoIt腳本。

;ControlFocus("title","text",controlID) Edit1=Edit instance 1
ControlFocus("文件上傳","","Edit1")

; Wait 10 secends for the Upload window to appear
WinWait("[CLASS:#32770]","",10)

;Set the File name text on the Edit field
ControlSetText("文件上傳","","Edit1", "D:\register.txt")
Sleep(2000)

; Click On the Open button
ControlClick("文件上傳","","Bu  t

ton1
");

技術分享圖片

1.1.1.1 生成exe

使用Compile Script to.exe,將其生成exe可執行文件。

技術分享圖片

註:生成的文件可能會360殺掉,直接從恢復區域恢復該文件並選擇不再查殺即可。

1.1.1.2 Py腳本

編輯py文件,調用該exe

        # 寫信鏈接
        driver.switch_to_default_content()
        driver.find_element_by_id("composebtn").click()
        # 正文
        driver.switch_to_frame(mainFrame)
        driver.find_element_by_xpath("//*[@id=‘toAreaCtrl‘]/div[2]/input").send_keys("[email protected]")
        driver.find_element_by_id("subject").send_keys("主題test")
        driver.find_element_by_id("sAddAtt1").click()#點擊添加附件
        #調用文件上傳程序
        os.system(D:\\upfile.exe)
        time.sleep(6)  # 避免附件未上傳完畢,所以等待6秒鐘
        driver.find_element_by_link_text("發送").click()

1.2 下載文件

1.2.1 Firefox文件下載

對於Firefox,需要設置其Profile:

l browser.download.dir:指定下載路徑

l browser.download.folderList:設置成 2 表示使用自定義下載路徑;設置成 0 表示下載到桌面;設置成 1 表示下載到默認路徑

l browser.download.manager.showWhenStarting:在開始下載時是否顯示下載管理器

l browser.helperApps.neverAsk.saveToDisk:對所給出文件類型不再彈出框進行詢問

腳本轉載:https://www.jianshu.com/p/b03ef6ffc4a5

# -*- coding: utf-8 -*-
from selenium import webdriver
from time import sleep

profile = webdriver.FirefoxProfile()
profile.set_preference(browser.download.dir, d:\\) # 指定下載的路徑
profile.set_preference(browser.download.folderList, 2) #0:默認下載路徑,2:知道路徑
profile.set_preference(browser.download.manager.showWhenStarting, False) #是否顯示開始,true:顯示,flase:不顯示
profile.set_preference(browser.helperApps.neverAsk.saveToDisk, application/zip) #下載類型

driver = webdriver.Firefox(firefox_profile=profile)

driver.get(http://sahitest.com/demo/saveAs.htm)
driver.find_element_by_xpath(//a[text()="testsaveas.zip"]).click()
sleep(3)
driver.quit()

1.2.2 Chrome 文件下載

Chrome瀏覽器類似,設置其options:

l download.default_directory:設置下載路徑

l profile.default_content_settings.popups:設置為 0 禁止彈出窗口

# -*- coding: utf-8 -*-

from selenium import webdriver
from time import sleep

options = webdriver.ChromeOptions()
prefs = {profile.default_content_settings.popups: 0, download.default_directory: d:\\} #0:禁止彈出窗口,設置下載路徑
options.add_experimental_option("prefs", prefs)

driver = webdriver.Chrome(chrome_options=options)
driver.get(http://sahitest.com/demo/saveAs.htm)
driver.find_element_by_xpath(//a[text()="testsaveas.zip"]).click()
sleep(3)
driver.quit()

selenium2學習:上傳文件、下載文件(待續)