【问题标题】:VBA select dropdown menu websiteVBA 选择下拉菜单网站
【发布时间】:2017-05-10 08:12:31
【问题描述】:

我目前正在尝试在vba 中写一些东西来访问网站并从表格中获取信息。到目前为止,我只设法登录并导航到表格,但我现在要做的是首先通过在下拉菜单上选择不同的选项来过滤表格。

这是我的代码:

Sub browse()
    Set objIE = CreateObject("InternetExplorer.Application")
    WebSite = "https://www.kicktipp.de/info/profil/login"
    With objIE
        .Visible = True
        .navigate WebSite
        Do While .Busy Or .ReadyState <> 4
            DoEvents
        Loop

        Set Element = .document.getElementsByName("kennung")
        Element.Item(0).Value = "myemail"
        Set Element = .document.getElementsByName("passwort")
        Element.Item(0).Value = "password"
        .document.forms(0).submit
        .navigate "https://www.kicktipp.de/los-bwlos/gesamtuebersicht"
        While ie.ReadyState <> 4 Or ie.Busy: DoEvents: Wend
        Set Element = .document.getElementById("tippspieltagVonIndex")
        Element.Item(0).Value = 4
        Element.FireEvent ("onchange")
        Application.SendKeys (Enter)


        End With

End Sub

问题是我在尝试选择下拉菜单时收到不同的错误消息,具体取决于我使用的是getElementById 还是getElementsbyName

这是该部分的 HTML:

<tr class="e">
<td class="nw">
<div><label for="tippspieltagVonIndex">Spieltage</label>
</div></td><td><b>von</b> 
<select name="tippspieltagVonIndex" id="tippspieltagVonIndex">
<option selected="selected" value="1">1. Spieltag</option>
<option value="2">2. Spieltag</option>
<option value="3">3. Spieltag</option>
<option value="4">4. Spieltag</option>
<option value="5">5. Spieltag</option>
<option value="6">Achtelfinale</option>
<option value="7">Viertelfinale</option>
<option value="8">Halbfinale</option>

【问题讨论】:

  • Element 定义在哪里?

标签: html excel vba web dropdown


【解决方案1】:

SelectedIndex 属性

而不是Element.Item(0).Value = 4
试试看:Element.Item(0).selectedIndex = 4

适用于扩展登录站点 HTML 的代码

我无法登录,所以我在登录站点添加了Select 标签。下面代码中的每个选项都对我有用。

Sub browse()
    Dim objIE As Object
    Dim Element As Object

    Set objIE = CreateObject("InternetExplorer.Application")
    With objIE
        .Visible = True
        .navigate "https://www.kicktipp.de/info/profil/login"
        Do While .Busy Or .ReadyState <> 4
            DoEvents
        Loop

        'element copied from your question, pasted into the login site HTML
        Set Element = .document.getElementById("tippspieltagVonIndex")
        'any option below did change the select element
        Element.Value = 1 '1. Spieltag
        Element.Value = "2" '2. Spieltag
        Element.selectedIndex= 2 '3. Spieltag because 0-based array

        .Quit
    End With 'objIE
End Sub

【讨论】:

  • 嗨 Branislav,我试过了,但我收到错误 91。知道什么可能导致问题吗?感谢您的时间和精力!
  • @JohannesEngelhardt 嗨。我已经测试了问题的选择部分并编辑了我的答案。如果导航到第二个 url 后仍然出现错误,请明确尝试Set Element = Nothing,然后再尝试Set Element = .document.getElementById("tippspieltagVonIndex")。另外,在您的代码中注意While ie.ReadyState &lt;&gt; 4 Or ie.Busy: DoEvents: Wend 上不存在的ie 对象,更改为objIE
猜你喜欢
  • 2022-01-13
  • 2012-10-01
  • 1970-01-01
  • 2018-04-24
  • 2020-01-25
  • 2019-08-08
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
相关资源
最近更新 更多