gpt4 book ai didi

python - 有趣的 "getElementById() takes exactly 1 argument (2 given)",有时会发生。有人可以解释一下吗?

转载 作者:IT老高 更新时间:2023-10-28 21:13:58 24 4
gpt4 key购买 nike

#-*- coding:utf-8 -*-
import win32com.client, pythoncom
import time

ie = win32com.client.DispatchEx('InternetExplorer.Application.1')
ie.Visible = 1
ie.Navigate('http://ieeexplore.ieee.org/xpl/periodicals.jsp')
time.sleep( 5 )

ie.Document.getElementById("browse_keyword").value ="Computer"
ie.Document.getElementsByTagName("input")[24].click()

import win32com.client, pythoncom
import time

ie = win32com.client.DispatchEx('InternetExplorer.Application')
ie.Visible = 1
ie.Navigate('www.baidu.com')
time.sleep(5)

print 'browse_keword'
ie.Document.getElementById("kw").value ="Computer"
ie.Document.getElementById("su").click()
print 'Done!'

运行第一段代码时会弹出:

ie.Document.getElementById("browse_keyword").value ="Computer"
TypeError: getElementById() takes exactly 1 argument (2 given)

第二段代码运行正常。使结果不同有什么区别?

最佳答案

这两种情况的区别与您指定的 COM 名称无关:InternetExplorer.ApplicationInternetExplorer.Application.1 导致完全相同的 CLSID它为您提供了一个 IWebBrowser2 界面。运行时行为的差异完全取决于您检索到的 URL。

这里的区别可能是工作的页面是 HTML 而另一个是 XHTML;或者可能只是失败页面中的错误阻止了 DOM 正确初始化。无论它看起来是 IE9 解析器的“功能”。

请注意,如果您启用兼容模式,则不会发生这种情况(在下面的第二行之后,我单击了地址栏中的兼容模式图标):

(Pdb) ie.Document.DocumentMode
9.0
(Pdb) ie.Document.getElementById("browse_keyword").value
*** TypeError: getElementById() takes exactly 1 argument (2 given)
(Pdb) ie.Document.documentMode
7.0
(Pdb) ie.Document.getElementById("browse_keyword").value
u''

不幸的是,我不知道如何从脚本切换兼容模式(documentMode 属性不可设置)。也许其他人会这样做?

我认为错误的参数计数来自 COM:Python 传入了参数,而 COM 对象拒绝调用并出现误导性错误。

关于python - 有趣的 "getElementById() takes exactly 1 argument (2 given)",有时会发生。有人可以解释一下吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9816967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com