html - VBA 选择下拉菜单网站

标签 html excel vba web dropdown

我目前正在尝试用 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>

最佳答案

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

关于html - VBA 选择下拉菜单网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41321479/

相关文章:

javascript - Angular UI 网格 : Hover on expandable row

excel - VBA - 删除列范围时出现错误1004

excel - 复制visio页面并将其作为图像粘贴到excel中

javascript - 检查网页上的文本框以获取特定字符串

jquery - 移动到可滚动内容中的元素

Excel:从两列中提取按字母顺序排列的元素的唯一不同列表

excel - "path/file access error"错误的原因是什么

vba - 在 VBA 格式函数中指定变量长度

html - 如何在 asp.net mvc 中创建多行文本框?

java - 如何使用python编写excel注释?