【发布时间】:2019-01-06 11:28:06
【问题描述】:
enter image description hereupdated我正在尝试使用 VBA 将我登录到安全网页,然后导航到需要在搜索数据库之前从下拉框中选择一个值的网页。
我无法让在下拉框中选择值的最后一部分起作用,我使用了以下代码。
下拉框名称为 = District,文本值为“South”,South 的组合值为 HTML 代码中的 A。有人可以帮忙(阅读其他几篇文章但不理解)。
Sub database()
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
'add worksheet
Sheets.Add After:=ActiveSheet
'destination
Set destsheet = ActiveSheet
'use internet explorer
Set IE = CreateObject("InternetExplorer.application")
' with internet open, make this visable and go to webpage x, enter username
and passwork
With IE
.Visible = True
.Navigate ("URL")
While .Busy Or .ReadyState <> 4: DoEvents: Wend
'.Document.getElementsbyname("User name").Focus
.Document.getElementsByName("username")(0).Value = "username"
.Document.getElementsByName("password")(0).Value = "Pword"
While .Busy Or .ReadyState <> 4: DoEvents: Wend
Set objCollection = IE.Document.getElementsByTagName("input")
'log in (submit)
i = 0
While i < objCollection.Length
If objCollection(i).Type = "submit" And _
objCollection(i).Name = "" Then
' "Search" button is found
Set objElement = objCollection(i)
End If
i = i + 1
Wend
'upon logging in naviage to webpage...
objElement.Click
.Navigate ("URL 2")
While .Busy Or .ReadyState <> 4: DoEvents: Wend
Debug.Print .LocationURL
End With
With IE
IE.doc.getElementsByName("district").Item(A).Selected = True
End With
End Sub
【问题讨论】:
-
你的html在哪里?还请明确指出您要选择哪个下拉选项。 Item(A).Selected = True 期待一个名为 A btw 的变量。
-
所以下拉框 objectname 的 HTML 代码是“District”,我要选择的选项值具有对应于文本值“South”的值“A” - 所以我想要这个值等于南。
-
道歉在最后一条消息中不小心按下了输入:')
-
我无法嵌入但链接到帖子顶部的图像
标签: html excel vba web web-scraping