轉自:http://blog.chinaunix.net/u3/103146/showart_2058891.html
Python這種腳本語言的強大功能越來越被廣大的程序員所重視,這種之前在國內流行度不高的語言近來氣勢高漲。各種第三方模塊層出不窮。
本文介紹的便是一種能非常方便操作IE的第三方工具,
PAMIE,他能讓你如同寫JS一樣來操作IE瀏覽器。包括自動啓動,訪問鏈接,設置文本框值,獲取按鈕,執行點擊事件,甚至執行頁面JS方法等等。下面用一個實際的例子詳加說明:
以下簡短代碼便輕易實現,登錄本人ChinaUnix,並以此點擊日誌文章,發文章,設置標題,分類,和博客內容,最後執行確定,發佈成功。
# -*- coding: gb2312 -*- from PAM30 import PAMIE from string import split #=============================================================================== # 從文件讀取配置信息,登錄url,賬戶,密碼等 #=============================================================================== def getCfgFromFile(fileName='settings.txt'): file = open(fileName) dict = {} line = file.readline() while line != '': args = split(line, '=') dict[args[0]] = args[1].decode('utf-8').encode('gb2312') line = file.readline() return dict dict = getCfgFromFile() ie = PAMIE() #=============================================================================== # 打開登錄頁面,設置用戶/密碼 #=============================================================================== ie.navigate(dict['login-url']) ie.setTextBox('username', dict['username']) ie.setTextBox('password', dict['password']) #=============================================================================== # 獲取登錄按鈕 #=============================================================================== loginbtn = ie.findElement('input', 'type', 'image') ie.clickElement(loginbtn) #=============================================================================== # 點擊文章管理 #=============================================================================== ie.navigate(dict["article-url"]) #=============================================================================== # 點擊寫文章 #=============================================================================== mainFrame = ie.getFrame('main') pwindow = mainFrame.document.parentWindow pwindow.execScript('NewArticle()') #=============================================================================== # 設置文章標題,文章分類,系統分類,文章類型 #=============================================================================== mainFrame = ie.getFrame('main') doc = mainFrame.document #------------------------------------------------------------------------ 設置文章標題 doc.getElementById('blog_title').value = dict['title'] #------------------------------------------------------------------------ 文章分類-java doc.getElementById('frmid').value = '119124' #------------------------------------------------------------------------ 系統分類-java doc.getElementById('systemfrmid').value = '20' #----------------------------------------------------------------------- 文章類型-原創 doc.getElementById('arttype').value = dict['arttype'] #=============================================================================== # 填寫文章內容 #=============================================================================== pwindow = mainFrame.document.parentWindow pwindow.execScript('InsertHTML("Python+PAMIE")') pwindow.execScript('InsertHTML("如此強大的功能")') #=============================================================================== # 發表文章 #=============================================================================== pwindow.execScript('savearticle()')
|