【问题标题】:VBA selecting a value from dropdown box on a webpageVBA从网页上的下拉框中选择一个值
【发布时间】: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


【解决方案1】:

试试

.document.querySelector("Select[name=District] option[value=A]").Selected = True

Select[name=District] option[value=A] 是一个 CSS 选择器。它查找带有option 标签的元素,其属性为value,其值为= A,父元素的标签为Select,其属性为name,其值为District。 document 的 querySelector 方法应用了选择器。

【讨论】:

  • 啊太棒了!谢谢你的工作!只是出于兴趣,我以前从未使用过查询选择,这只是意味着从代码中的列表中选择?
  • 再一次。
猜你喜欢
  • 2017-06-01
  • 2021-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 1970-01-01
  • 2023-04-03
  • 2019-01-24
相关资源
最近更新 更多