enter image description here updated我正在尝试使用VBA将我登录到安全网页,然后导航到需要从下拉框中选择一个值的网页,然后再搜索数据库。
我无法在下拉框中选择要使用的值的最后一部分,我使用了以下代码。
在HTML代码中,下拉框的名称为= District,文本值为“ South”,South的组合值为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
答案 0 :(得分:1)
尝试
.document.querySelector("Select[name=District] option[value=A]").Selected = True
Select[name=District] option[value=A]
是CSS选择器。它寻找具有属性option
的标签value
的元素,其值= A
和具有元素Select
的父元素的标签,属性name
的属性District
1}}。文档的querySelector方法将应用选择器。