【问题标题】:How to select multiple items in a list box on website with VBA?如何使用 VBA 在网站上的列表框中选择多个项目?
【发布时间】: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(&#39;operatorList&#39;);">
<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


【解决方案1】:

当前代码:

通过尝试设置集合的值(这是getElementsByName 返回的值),您应该会收到一条错误消息,告诉您.value 不可用。它是集合中节点的属性,需要索引到您的集合中。由于您没有提及错误,我不得不怀疑在您的实际代码中您是否使用 On Error Resume Next 来掩盖错误?

@TimWilliams 在评论中指出了重要的一点,即在.Submit, .Click, .Refresh 等操作后留出足够的时间让网页更新。

另外,使用适当的页面加载等待:

While ie.Busy Or ie.ReadyState<>4:DoEvents:Wend

你可以更新循环语法(我老了!)


您可以尝试什么:

假设父 select 启用了多选,那么我将使用 querySelectorAll 收集所有选项的 nodeList 并在每个节点上循环该列表设置 .Selected = True

您可以使用父级selectidchild combinator 以及其中子级的type 选择器option 来收集节点列表

Dim nodes As Object, i As Long

Set nodes = htmlDoc.querySelectorAll("#contentPlaceHolder_ctl00_availableLocations > option")

For i  = 0 To nodes.Length - 1
    nodes.item(i).Selected = True
Next

这将允许您选择,而不必通过选择选项列表中的索引或使用 value 属性值等来指定每个节点。例如使用 value 属性单独设置如下所示:

htmlDoc.querySelector("[value='1297']").Selected =True

等等……


在此处了解 querySelectorAll 使用的 css 选择器:

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    相关资源
    最近更新 更多