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