1. 程式人生 > >Python操作IE的程式碼

Python操作IE的程式碼



# -*- coding:utf-8 -*-
import time
from win32com.client import DispatchEx

def ExistIE(url):
    ShellWindowsCLSID = '{9BA05972-F6A8-11CF-A442-00A0C90A8F39}'
    ies=DispatchEx(ShellWindowsCLSID)
    if len(ies)==0:
        return None
    for ie in ies:
        if ie.LocationURL==url:
            return ie
    return None

def NewIE(url):
    ie=DispatchEx("InternetExplorer.Application")
    ie.Navigate(url)
    return ie

def openIE(url):
    """
    >>> myie = ie.NewIE()
    """
    ie=ExistIE(url)
    if ie==None:
        ie=NewIE(url)
    return ie

def WaitIE(ie):
    while ie.Busy:
        time.sleep(1)

def Visible(ie):


    ie.Visible=1-ie.Visible

def GetBody(ie):
    WaitIE(ie)
    return ie.Document.body

def GetNodes(parentNode,tag):
    """
    >>> coldiv=GetNodes(body,"div")
    """
    childNodes=[]
    for childNode in parentNode.getElementsByTagName(tag):
        childNodes.append(childNode)
    return childNodes

def NodeByAttr(Nodes,nodeattr,nodeval):
    """
    >>> div_id_editor=NodeByAttr(coldiv,"id","editor_ifr")
    """
    for node in Nodes:
        if str(node.getAttribute(nodeattr))==nodeval:
            return node
    return None

def SetNode(node,val):
    node.innerHTML=val

if __name__=="__main__":
    url="about:blank"
    myie=NewIE(url)
    Visible(myie)
    mybody=GetBody(myie)

    SetNode(mybody,"<div>Genius</div>"*3)
    coldiv=GetNodes(mybody,"div")
    SetNode(coldiv[0],"<p>Hello</p>"*10)
    coldiv[0].SetAttribute("id","test")
    div_id_test=ie.NodeByAttr(coldiv,"id","test")
    div_p=GetNodes(div_id_test,"p")
    for div_per_p in div_p:
        print div_per_p.innerHTML

比如再去呼叫這個來登陸人人網:

# -*- coding:utf -*-
import getpass
import ie

myie=ie.NewIE(url)
ie.Visible(myie)
mybody=ie.GetBody(myie)
input_ids=ie.GetNodes(mybody,"input")
input_id_email=ie.NodeByAttr(input_ids,"id","email")
input_id_email.value="[email protected]"
input_id_psd=ie.NodeByAttr(input_ids,"id","password")
input_id_psd.value=getpass.getpass()
input_id_login=ie.NodeByAttr(input_ids,"id","login")
input_id_login.click()

相關推薦

Python操作IE程式碼

 # -*- coding:utf-8 -*-import timefrom win32com.client import DispatchEx def ExistIE(url):    ShellWindowsCLSID = '{9BA05972-F6A8-11CF-

python scrapy 基本操作演示程式碼

# -*- coding: utf-8 -*- import scrapy # from quotetutorial.items import QuoteItem from quotetutorial.items import QuotetutorialItem # 主要編輯專案資訊基本上都在在這裡完成

關於python操作excel的程式碼及一些感悟

首先我們來看看一些python操作excel的主流包: xlrd-----excelread      xlwt------excelwrite     xlutils    openpyxl 

Python操作JSON資料程式碼示例

#!/usr/bin/env python import json import os def json_test(): return_dic = {} json_data = { 'appid':'modify appid', 'key':'modify key', 'fromLang':'en'

python操作計算器的程式碼demo

操作計算器 先安裝pywin32模組 import win32api,win32gui, win32con import win32com.client shell = win32com.clie

leetcode 541 反轉字串 II Reverse String II python 最簡程式碼 字串的切片和反序不能同時操作

class Solution: def reverseStr(self, s, k): """ :type s: str :type k: int

Python對excel寫入資料操作例項程式碼(只供參考)

#coding=utf8 ''' 把buffer中的資訊,寫入到excel中。 並按照要求,構造表格樣式 ''' #匯入readCSV模組,用來獲取buffer資料 from readCSV import readCSV from readConfig import Con

python 操作mongoDB數據庫

查看 $set 一個 關於 pan date set 完整 mongodb 網上關於python 操作mongoDB的相關文章相對不是很多,並且質量也不是很高!下面給出一個完整的 增刪改查示例程序! #!/usr/bin/python # -*- coding

Python操作遠程數據庫

his exec 通配符 .get toc 大量 while 避免 登入 我的項目要往數據庫中插入create_time和update_time,那就勢必要引用現在的系統時間,經過大量的查找,終於發現往python是沒有對應時間datetime的相關通配符的,那麽我們要怎麽

#python# 操作文件和目錄

級別 編寫一個程序 路徑 pre 找文件 練習 itext os.path 當前 總結於廖雪峰老師的python教程。 操作文件和目錄的函數一部分放在os模塊中,一部分放在os.path模塊中。 總結如下: 1 os.path.abspath(‘.‘)

python 操作excel表格

雷達圖 tle sum utf-8 範圍 red lod 圖標 data Python 操作excel 表格 #coding=utf-8 import xlsxwriter #1.創建excel 對象 work = xlsxwr

python小工具:用python操作HP的Quality Center

over cti 步驟 response headers 服務器 登錄 chm format 背景是這樣的:這個組的測試人員每跑一個case都要上傳測試結果附件到QC。每個待測功能模塊可能包含幾十上百的case。於是手工上傳測試結果變成了繁重的體力勞動。令人驚訝的是我們的工

使用python操作word

data close def pip 行數 pri path http 行數據 有兩種方式: 使用win32com 使用docx 1.使用win32com擴展包 只對windows平臺有效 代碼: # coding=utf-8 import win32com fr

python操作excel

int lena cells src tar 程序 win32 div cep python操作exce的方式: 使用win32com 使用xlrd(讀excel)、xlwt(寫excel) 1.使用win32com方式 代碼: # coding=utf-8 from

Python操作MySQL

port sdf cnblogs ctc hal tro 多行 執行sql lai 本篇對於Python操作MySQL主要使用兩種方式: 原生模塊 pymsql ORM框架 SQLAchemy pymsql pymsql是Python中操作MySQL的模塊,其使用方法和

python操作數據庫-數據表

title 獲取 arch python操作 -- pymysql exe span 轉義 數據表: 數據類型: 幫助的三種形式: 在cmd中輸入: help 要幫助的主題詞,或 ? 要幫助的主題詞 或 \h 要幫助的主題詞 。 數據表的創建:

Python 操作 Azure Blob Storage

edi 安裝 一點 是你 接下來 ges list settings containe 筆者在《Azure 基礎:Blob Storage》一文中介紹了 Azure Blob Storage 的基本概念,並通過 C# 代碼展示了如何進行基本的操作。最近筆者需要在 Linux

python 操作RabbitMQ

ack 模糊匹配 host star cal top con 消失 nbsp pip install pika 使用API操作RabbitMQ 基於Queue實現生產者消費者模型 View Code 對於RabbitMQ來說,生產和消費不再針對內存裏的一個Queue

Python操作SQLAchemy

kref func ont == 更多 字典 drop ring constrain 實在不懂就看這個:http://www.cnblogs.com/jixuege-1/p/6272888.html 本篇對於Python操作MySQL主要使用兩種方式: 原生模塊 pyms

python 操作MongoDB

mongod apple find 沒有 mysq port 總結 logs 一個 安裝MongoDB 啟動數據庫:安裝完成指定數據庫存放路徑 mongod.exe --dbpath c:\data\db進入目錄後運行mongo.exe 成功 創建數據庫 > use