功能:
1, 获得IE浏览器句柄
2, 使用句柄利用pythoncom 连接到IE浏览器,并且获得浏览器对象和Document对象
3, 通过IE浏览器对象控制浏览器,先导航到百度, 然后试着去寻找搜素input : ‘kw’
出错语句:dom.getElementById('kw')
TypeError: getElementById() takes 1 positional argument but 2 were given
代码 (运行前需要手动打开IE)
import traceback
import win32com.client
import pythoncom
import win32gui, win32api,win32con
import win32clipboard
import time,os
class MyWindow():
def __init__(self,h=None):
self.h=h
def __str__(self):
c=win32gui.GetClassName(self.h)
t=win32gui.GetWindowText(self.h)
return "hand={},{}, class={}, text={}".format(hex(self.h),self.h, c, t)
def print(self):
print(self.__str__())
return self
def enumFunc(self,h,para):
if win32gui.GetParent(h)==para[1]:
para[0].append(h)
def enumChilds(self,h=None,ind=0):
li = []
if h==None: h=self.h
if h != None:
c=win32gui.GetClassName(h)
t=win32gui.GetWindowText(h)
for i in range(1,ind):
print('| ',end='')
print("hand={}, class={}, text={}".format(hex(h), c, t))
win32gui.EnumChildWindows(h, self.enumFunc, (li, h))
else:
win32gui.EnumWindows(self.enumFunc, (li, h))
for x in li:
self.enumChilds(x,ind+2)
def setClickBoardText(self,text):
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32con.CF_UNICODETEXT,text)
win32clipboard.CloseClipboard()
def childCl(self, className=''):
li=[]
win32gui.EnumChildWindows(self.h,lambda h,para:para.append(h),li)
for x in li:
c = win32gui.GetClassName(x)
t = win32gui.GetWindowText(x)
if c==className:
return MyWindow(x)
return None
def parent(self):
return MyWindow(win32gui.GetParent(self.h))
def childTx(self,text=''):
li=[]
win32gui.EnumChildWindows(self.h,lambda h,para:para.append(h),li)
for x in li:
c = win32gui.GetClassName(x)
t = win32gui.GetWindowText(x)
if text in t:
return MyWindow(x)
return None
def childTxCl(self,text='', className=''):
li=[]
win32gui.EnumChildWindows(self.h,lambda h,para:para.append(h),li)
for x in li:
c = win32gui.GetClassName(x)
t = win32gui.GetWindowText(x)
if text in t and c==className:
return MyWindow(x)
return None
def waitTopWindow(text='',timeOut=30):
tryCount=timeOut
while True:
li=[]
win32gui.EnumWindows(lambda h,para:para.append(h),li)
for x in li:
c = win32gui.GetClassName(x)
t = win32gui.GetWindowText(x)
if text in t:
return MyWindow(x)
tryCount-=1
if tryCount==0:
return None
time.sleep(1)
def waitTopWindowTxCl(text='',className='',timeOut=30):
tryCount=timeOut
while True:
li=[]
win32gui.EnumWindows(lambda h,para:para.append(h),li)
for x in li:
c = win32gui.GetClassName(x)
t = win32gui.GetWindowText(x)
if text in t and className==c:
return MyWindow(x)
tryCount-=1
if tryCount==0:
return None
time.sleep(1)
# SID_STopLevelBrowser = IID('{4C96BE40-915C-11CF-99D3-00AA004AE837}')
SID_SWebBrowserApp = pythoncom.MakeIID('{0002DF05-0000-0000-C000-000000000046}')
SID_IXMLDocument2=pythoncom.MakeIID('{2B8DE2FE-8D2D-11d1-B2FC-00C04FD915A9}')
# IID_IWebBrowser2 = IID('{D30C1661-CDAF-11D0-8A3E-00C04FC9E26E}')
def getIWebBrowser2ByHwnd(hwnd):
'''
通过句柄获取IWebBrowser2对象
'''
try:
msg = win32gui.RegisterWindowMessage('WM_HTML_GETOBJECT')
ret, result = win32gui.SendMessageTimeout(hwnd, msg, 0, 0, win32con.SMTO_ABORTIFHUNG, 1000)
print('result:{},class:{}'.format(result, result.__class__))
ob = pythoncom.ObjectFromLresult(result, pythoncom.IID_IDispatch, 0)
print('ob:{},class:{}'.format(ob,ob.__class__))
iHtmlDocument2 = win32com.client.dynamic.Dispatch(ob)
print('iHtmlDocument2:{},class:{}'.format(iHtmlDocument2,iHtmlDocument2.__class__))
iServiceProvider = iHtmlDocument2.parentWindow._oleobj_.QueryInterface(pythoncom.IID_IServiceProvider)
print('iServiceProvider:{},class:{}'.format(iServiceProvider,iServiceProvider.__class__))
iWebBrowser2 = win32com.client.Dispatch(iServiceProvider.QueryService(SID_SWebBrowserApp, pythoncom.IID_IDispatch))
print('iWebBrowser2:{}, class:{}'.format(iWebBrowser2,iWebBrowser2.__class__))
return iWebBrowser2,iHtmlDocument2
except:
traceback.print_exc()
return None
topHand=MyWindow.waitTopWindowTxCl('Internet Explorer','IEFrame')
topHand.print()
ie,dom=getIWebBrowser2ByHwnd(topHand.childCl('Internet Explorer_Server').h)
ie.Navigate('http://www.baidu.com',None,None,None,None)
time.sleep(2)
x=dom.getElementById('kw')
print(x)
错误信息:
Traceback (most recent call last):
File "C:/Users/17854/PycharmProjects/pythonProject/句柄获取IE对象.py", line 145, in <module>
x=dom.getElementById('kw')
TypeError: getElementById() takes 1 positional argument but 2 were given
错误语句:
x=dom.getElementById('kw')
看起来python在调用getElementById时候把此方法当成类方法,默认传入self参数,但是此方法并不接受self,所以报错
这个问题如何解决,有没有大神来看看
1, 获得IE浏览器句柄
2, 使用句柄利用pythoncom 连接到IE浏览器,并且获得浏览器对象和Document对象
3, 通过IE浏览器对象控制浏览器,先导航到百度, 然后试着去寻找搜素input : ‘kw’
出错语句:dom.getElementById('kw')
TypeError: getElementById() takes 1 positional argument but 2 were given
代码 (运行前需要手动打开IE)
import traceback
import win32com.client
import pythoncom
import win32gui, win32api,win32con
import win32clipboard
import time,os
class MyWindow():
def __init__(self,h=None):
self.h=h
def __str__(self):
c=win32gui.GetClassName(self.h)
t=win32gui.GetWindowText(self.h)
return "hand={},{}, class={}, text={}".format(hex(self.h),self.h, c, t)
def print(self):
print(self.__str__())
return self
def enumFunc(self,h,para):
if win32gui.GetParent(h)==para[1]:
para[0].append(h)
def enumChilds(self,h=None,ind=0):
li = []
if h==None: h=self.h
if h != None:
c=win32gui.GetClassName(h)
t=win32gui.GetWindowText(h)
for i in range(1,ind):
print('| ',end='')
print("hand={}, class={}, text={}".format(hex(h), c, t))
win32gui.EnumChildWindows(h, self.enumFunc, (li, h))
else:
win32gui.EnumWindows(self.enumFunc, (li, h))
for x in li:
self.enumChilds(x,ind+2)
def setClickBoardText(self,text):
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32con.CF_UNICODETEXT,text)
win32clipboard.CloseClipboard()
def childCl(self, className=''):
li=[]
win32gui.EnumChildWindows(self.h,lambda h,para:para.append(h),li)
for x in li:
c = win32gui.GetClassName(x)
t = win32gui.GetWindowText(x)
if c==className:
return MyWindow(x)
return None
def parent(self):
return MyWindow(win32gui.GetParent(self.h))
def childTx(self,text=''):
li=[]
win32gui.EnumChildWindows(self.h,lambda h,para:para.append(h),li)
for x in li:
c = win32gui.GetClassName(x)
t = win32gui.GetWindowText(x)
if text in t:
return MyWindow(x)
return None
def childTxCl(self,text='', className=''):
li=[]
win32gui.EnumChildWindows(self.h,lambda h,para:para.append(h),li)
for x in li:
c = win32gui.GetClassName(x)
t = win32gui.GetWindowText(x)
if text in t and c==className:
return MyWindow(x)
return None
def waitTopWindow(text='',timeOut=30):
tryCount=timeOut
while True:
li=[]
win32gui.EnumWindows(lambda h,para:para.append(h),li)
for x in li:
c = win32gui.GetClassName(x)
t = win32gui.GetWindowText(x)
if text in t:
return MyWindow(x)
tryCount-=1
if tryCount==0:
return None
time.sleep(1)
def waitTopWindowTxCl(text='',className='',timeOut=30):
tryCount=timeOut
while True:
li=[]
win32gui.EnumWindows(lambda h,para:para.append(h),li)
for x in li:
c = win32gui.GetClassName(x)
t = win32gui.GetWindowText(x)
if text in t and className==c:
return MyWindow(x)
tryCount-=1
if tryCount==0:
return None
time.sleep(1)
# SID_STopLevelBrowser = IID('{4C96BE40-915C-11CF-99D3-00AA004AE837}')
SID_SWebBrowserApp = pythoncom.MakeIID('{0002DF05-0000-0000-C000-000000000046}')
SID_IXMLDocument2=pythoncom.MakeIID('{2B8DE2FE-8D2D-11d1-B2FC-00C04FD915A9}')
# IID_IWebBrowser2 = IID('{D30C1661-CDAF-11D0-8A3E-00C04FC9E26E}')
def getIWebBrowser2ByHwnd(hwnd):
'''
通过句柄获取IWebBrowser2对象
'''
try:
msg = win32gui.RegisterWindowMessage('WM_HTML_GETOBJECT')
ret, result = win32gui.SendMessageTimeout(hwnd, msg, 0, 0, win32con.SMTO_ABORTIFHUNG, 1000)
print('result:{},class:{}'.format(result, result.__class__))
ob = pythoncom.ObjectFromLresult(result, pythoncom.IID_IDispatch, 0)
print('ob:{},class:{}'.format(ob,ob.__class__))
iHtmlDocument2 = win32com.client.dynamic.Dispatch(ob)
print('iHtmlDocument2:{},class:{}'.format(iHtmlDocument2,iHtmlDocument2.__class__))
iServiceProvider = iHtmlDocument2.parentWindow._oleobj_.QueryInterface(pythoncom.IID_IServiceProvider)
print('iServiceProvider:{},class:{}'.format(iServiceProvider,iServiceProvider.__class__))
iWebBrowser2 = win32com.client.Dispatch(iServiceProvider.QueryService(SID_SWebBrowserApp, pythoncom.IID_IDispatch))
print('iWebBrowser2:{}, class:{}'.format(iWebBrowser2,iWebBrowser2.__class__))
return iWebBrowser2,iHtmlDocument2
except:
traceback.print_exc()
return None
topHand=MyWindow.waitTopWindowTxCl('Internet Explorer','IEFrame')
topHand.print()
ie,dom=getIWebBrowser2ByHwnd(topHand.childCl('Internet Explorer_Server').h)
ie.Navigate('http://www.baidu.com',None,None,None,None)
time.sleep(2)
x=dom.getElementById('kw')
print(x)
错误信息:
Traceback (most recent call last):
File "C:/Users/17854/PycharmProjects/pythonProject/句柄获取IE对象.py", line 145, in <module>
x=dom.getElementById('kw')
TypeError: getElementById() takes 1 positional argument but 2 were given
错误语句:
x=dom.getElementById('kw')
看起来python在调用getElementById时候把此方法当成类方法,默认传入self参数,但是此方法并不接受self,所以报错
这个问题如何解决,有没有大神来看看