VBA从网页的下拉框中选择一个值 [英] VBA selecting a value from dropdown box on a webpage

查看:113
本文介绍了VBA从网页的下拉框中选择一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此处输入图片描述

enter image description hereupdatedI'm trying to use VBA to log me into a secure webpage, then navigate to a webpage where I need to select a value from a drop down box before searching the database.

我无法在下拉框中选择要使用的值的最后一部分,我使用了以下代码.

I cannot get the last part where it selects the value in the drop down box to work, I have used the below code.

在HTML代码中,下拉框的名称为= District,文本值为"South",South的组合值为A.有人可以帮忙吗(阅读其他几篇文章,但听不懂).

The drop down box Name is = District, text value is "South" and combo value for South is A in HTML code. Can someone please help (read a couple of other posts but didn't understand them).

    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

推荐答案

尝试

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

Select [name = District]选项[value = A] 是CSS选择器.它查找带有 option 标签的元素,该标签的属性为 value ,其值 = A ,并且其父元素的标签为 Select 具有属性 name 和值为 District 的>.文档的querySelector方法将应用选择器.

The Select[name=District] option[value=A] is a CSS selector. It looks for elements with option tag with attribute value whose value = A and with a parent element whose tag is Select which has attribute name with value District. The querySelector method of document applies the selector.

这篇关于VBA从网页的下拉框中选择一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆