【发布时间】:2021-05-05 09:34:48
【问题描述】:
我登录一个网站并尝试选择列表中的所有项目。
登录有效,但在提交之前未突出显示或选择项目。
Sub BrowseToSite() 'Login to site
'set ie = internetexplorer for reference
Dim ie As New SHDocVw.InternetExplorer
Dim htmldoc As MSHTML.HTMLDocument
Dim htmlinput As MSHTML.IHTMLElement
Dim htmlbuttons As MSHTML.IHTMLElementCollection
Dim objIE As Object
'see window and navigate to website (HOURLY VOLUME ENERGY COMPOSITION - HVEC)
ie.Visible = True
ie.navigate "www.website.com"
'wait for brower to load
Do While ie.readyState <> READYSTATE_COMPLETE
Loop
'Enters username and password
Set htmldoc = ie.document
Set htmlinput = htmldoc.getElementById("USER")
htmlinput.Value = "LoginUsername"
Set htmlinput = htmldoc.getElementById("PASSWORD")
htmlinput.Value = "Password1"
'finds form > submit button (under class = "LoginSubmit")
htmldoc.forms(0).submit
'select all Locations
ie.document.getElementsByName("ctl00$contentPlaceHolder$ctl00$availableLocations").Value = "1297"
ie.document.getElementsByName("ctl00$contentPlaceHolder$ctl00$availableLocations").Value = "3216"
ie.document.getElementsByName("ctl00$contentPlaceHolder$ctl00$availableLocations").Value = "3135"
objIE.document.getElementsByName("ctl00$contentPlaceHolder$ctl00$availableLocations")(0).Click
End Sub
这是 HTML 代码:
<tr>
<td class="fieldLabel">Location:</td>
<td class="fieldData" colspan="3">
<select size="12" name="ctl00$contentPlaceHolder$ctl00$availableLocations" multiple="multiple" id="contentPlaceHolder_ctl00_availableLocations" tabindex="8" class="fullWidth" onFocus="resetList('operatorList');">
<option value="1297">1297 - Test1</option>
<option value="3216">3216 - Test2</option>
<option value="3135">3135 - Test3</option>
【问题讨论】:
-
一方面,您似乎在
3135之后缺少"。 -
一定是把它剪掉了。添加了反引号。
-
还有 ?
htmldoc.forms(0).submit21?您需要在登录后留出时间让页面重新加载。由于您的选择有一个 id,您应该使用getElementById它将返回一个元素,而不是像getElmentsByName这样的集合
标签: html vba webautomation