按名称获取元素并设置其值


Sub FlashReportAutomation()

Dim web As Object
Dim doc As HTMLDocument

Set web = CreateObject("internetexplorer.application")

web.Visible = True
web.navigate "http://google.com"

Do While web.Busy
Application.Wait DateAdd("s", 2, Now)
Loop


Set doc = web.Document
web.Document.getElementsByName("q").Value = "Robert"
End Sub

如果没有提到元素ID,我就无法让它工作。有什么帮助吗?

我同意Tim Williams关于传递名称为"Q"的元素的索引号的建议。

我注意到的另一件事是,在下面的一行中,您设置了文档对象。

Set doc = web.Document

将值指定给元素时,您再次使用文档。它将生成一个错误。

web.Document.getElementsByName("q").Value = "Robert"

您的代码应该如下所示。

Set doc = web.document
doc.getElementsByName("q")(0).Value = "Robert"

你可以试着这样做,如果你有问题,请告诉我们。

最新更新