我正在尝试创建一个从网站上提取报告的宏,它有四个下拉列表来选择值。 我可以登录该页面并将自己定向到报告页面,但出于某种原因我遇到了下拉列表的问题。我尝试了几种解决方案'这是在线提供的,但我不断收到错误消息:
运行时错误'':对象不支持此属性或方法。
以下是可供选择的下拉列表之一:
<select name="LocationID">
<option value="0" selected="">All Location</option>
<option value="9">Atlanta</option>
<option value="7">Denver</option>
<option value="3">Las Vegas</option>
<option value="1">Los Angeles</option>
<option value="4">Miami</option>
<option value="6">New Jersey</option>
<option value="10">Phoenix</option>
<option value="2">San Francisco</option>
<option value="8">Seattle</option>
<option value="11">Vancouver</option>
</select>
以下是我目前在VBA中所拥有的内容:
Option Explicit
Const MyUserID As String = "test123"
Const MyPassword As String = "test123"
Const READYSTATE_COMPLETE As Integer = 4
Dim objIE As Object
Public Sub LoginScript()
Set objIE = CreateObject("InternetExplorer.Application")
With objIE
.Visible = True
.Silent = True
.navigate ("https://wwww.mywebsite.com")
Do Until .readyState = READYSTATE_COMPLETE
DoEvents
Loop
Application.Wait Now() + TimeValue("00:00:02")
.document.all.txtuserid.Value = MyUserID
.document.all.txtPassword.Value = MyPassword
objIE.document.getElementsByName("btnSubmit")(0).Click
Do Until .readyState = READYSTATE_COMPLETE
DoEvents
Loop
Application.Wait Now() + TimeValue("00:00:02")
.navigate("https://www.mywebsite.com/sample.html")
Do Until .readyState = READYSTATE_COMPLETE
DoEvents
Loop
.document.getElementByName("LocationID").Value = "7"
objIE.document.getElementsByName("view")(0).Click
End With
End Sub
答案 0 :(得分:0)
我假设您在此行收到错误消息?
.document.getElementByName("LocationID").Value = "7"
那是因为Name
是一个元素集合。与ID(getElementByID
)不同,集合上的元素是复数。
在这种情况下,正确的语法是:
.document.getElementsByName("LocationID")(0).Value = "7"
注意附加的(0)
?这是因为它又是一个集合,因此您还需要选择集合项。 (可能不一定是(0)
,但如果不查看整个HTML代码,我无法确定。但
答案 1 :(得分:0)
它应该是这样的......
Sub passValueToComboBox1()
Dim ie As Object
Dim oHTML_Element As IHTMLElement
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://www.your_web_site.com"
While ie.Busy Or ie.readyState <> 4: DoEvents: Wend
Set oHTML_Element = ie.document.getElementsByName("selectedReportClass")(0)
If Not oHTML_Element Is Nothing Then oHTML_Element.Value = "com.db.moap.report.FUBU"
For Each oHTML_Element In ie.document.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
End Sub