python webdriver api-右鍵另存下載文件


右鍵另存下載文件

先編輯SciTE腳本:

 

;ControlFocus("title","text",controlID)

;表示將焦點切換到標題為title窗體中的controlID上

;Edit1表示第一個可以編輯的實例

;title表示彈出的Window窗口標題,不同瀏覽器的標題可能不一樣

ControlFocus("請輸入要保存的文件名...","","Edit1")

 

;等待10秒以便window窗口加載成功

WinWait("[CLASS:#32770]","",10)

 

;將焦點切換到Edit1輸入框中

ControlFocus("另存為","","Edit1")

 

;等待2秒

Sleep(2000)

 

;將要下載的文件名及路徑寫入Edit1編輯框中

ControlSetText("另存為","", "Edit1", "d:\iDownload\Firefox Setup 35.0b8.exe")

 

Sleep(2000)

 

;點擊窗體中的第一個按鈕,也就是保存按鈕

;ControlClick("另存為","","Button1")

Send("{ENTER}")

Send("{ENTER}")

;保存后將該文件編譯成exe文件,並存放到本地磁盤。

;保存后將該文件編譯成exe文件,並存放到本地磁盤

 

 

然后保存成SciScript,然后用convert工具轉成exe文件

執行python程序,修改相關目錄

# encoding=utf-8

from selenium import webdriver

import unittest, time, os

from selenium.webdriver.common.keys import Keys

from selenium.webdriver import ActionChains

import win32api

import win32con

 

VK_CODE ={'enter':0x0D, 'down_arrow':0x28}

 

#鍵盤鍵按下

def keyDown(keyName):

    win32api.keybd_event(VK_CODE[keyName], 0, 0, 0)

#鍵盤鍵抬起

def keyUp(keyName):

    win32api.keybd_event(VK_CODE[keyName], 0, win32con.KEYEVENTF_KEYUP, 0)

 

class TestDemo(unittest.TestCase):

    def setUp(self):

        #self.driver = webdriver.Ie(executable_path = "e:\\IEDriverServer")

        self.driver = webdriver.Chrome(executable_path="c:\\chromedriver")

    def test_dataPickerByRightKey(self):

        # 定義將要訪問的網址

        url = "http://ftp.mozilla.org/pub/mozilla.org//firefox/releases/35.0b8/win32/zh-CN/"

        self.driver.get(url)

        # 將窗口最大化

        self.driver.maximize_window()

        # 暫停5秒,目的防止頁面有一些多余的彈窗占據焦點

        time.sleep(5)

        # 找到文本內容為“Firefox Setup 35.0b8.exe”超鏈接元素

        a = self.driver.find_element_by_link_text("Firefox Setup 35.0b8.exe")

        time.sleep(2)

        # 在找到的鏈接元素上模擬點擊鼠標右鍵,

        # 以便調出選擇“另存為”選項的菜單

        ActionChains(self.driver).context_click(a).perform()

        # 暫停2秒,防止命令執行太快

        time.sleep(2)

        for i in range(4):

            # 循環按4次下箭頭,將焦點切換到“另存為”選項上

            # 不同瀏覽器此選項的位置可能不同

            #a.send_keys(Keys.DOWN)

            keyDown("down_arrow")

            keyUp("down_arrow")

            print i

            time.sleep(2)

        time.sleep(2)

        # 當焦點切換到“另存為”選項上后,模擬點擊回車鍵

        # 調出保存下載文件路徑的Windows窗體

        keyDown("enter")

        keyUp("enter")

        time.sleep(3)

        # 通過執行AutoIt編寫的操作彈窗的Windows文件保存窗體

        # 完成文件保存路徑的設置

        os.system("d:\\test\\rightClick.exe")

        # 等待文件下載完成,根據各自的網絡帶寬情況設定等待相應的時間

        time.sleep(5)

 

    #def tearDown(self):

        #self.driver.quit()

 

if __name__ == '__main__':

    unittest.main()

 

D:\test>python test.py

 

DevTools listening on ws://127.0.0.1:12860/devtools/browser/c68d0424-6626-421d-8268-3184f78ede57

0

1

2

3

.

----------------------------------------------------------------------

Ran 1 test in 46.308s

 

OK

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM