【问题标题】:getElementById() takes exactly 1 argument (2 given)getElementById() 只需要 1 个参数(给定 2 个)
【发布时间】:2016-08-30 17:27:35
【问题描述】:

我想实现 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'

最后一行总是返回以下类型错误:

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

当我尝试通过 Internet Explorer 的控制台添加值时,它可以工作。

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

感谢您的帮助!

【问题讨论】:

标签: python win32com


【解决方案1】:

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

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 库。

【讨论】:

    【解决方案2】:

    正如this 的回答建议你必须使用

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

    【讨论】:

    • 我也试过这个,但它返回一个 AttributeError: AttributeError: .getElementById
    猜你喜欢
    • 1970-01-01
    • 2012-03-04
    • 2017-09-15
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    • 2012-03-07
    • 1970-01-01
    相关资源
    最近更新 更多