getElementById() 恰好接受 1 个参数(给定 2 个)

getElementById() takes exactly 1 argument (2 given)

我想实现 Internet Explorer 的自动化。打开 Internet Explorer,导航到 login.live.com 并在电子邮件文本框中设置一个值。

这是简单的脚本:

import win32com.client
import time

IE = win32com.client.DispatchEx("InternetExplorer.Application")
IE.Visible = 1
IE.Navigate('login.live.com')

time.sleep(5)

DOC = IE.document
DOC.getElementById('i0116').value = 'test'

最后一行总是 returns 以下 TypeError:

getElementById() takes exactly 1 argument (2 given)

当我尝试通过 Internet Explorer 的控制台添加值时,它起作用了。

顺便说一句。 getElementsByTagName() 方法没有任何错误。

感谢您的帮助!

因为 this 答案表明您必须使用

DOC.Body.getElementById('i0116').value = 'test'

好的..我为此写了一个解决方法:

DOC = IE.Document
inputs = DOC.documentElement.getElementsByTagName('input')

for field in inputs:
    if field.id == 'i0116':
        email = field
        break
email.value = 'example@test.com'

对于浏览器自动化,我建议使用 Selenium 库。