首页 > 解决方案 > 使用 VBA 让 Internet Explorer 从列表中选择项目

问题描述

再会,

我已经搜索了在这个网站上证明的答案和解决方案似乎没有帮助,包括 selectedIndex 和遍历数组

我有以下 HTML 代码组成一个表格,我想从中选择第二个选项“Vorige week”

<table cellspacing="0" cellpadding="0" title=""  class="mstrListBlock" 
id="id_mstr51" style="display: table; width: auto;">
<tbody>
<tr>
<td class="mstrListBlockCell">
<span class="">
<div class="mstrListBlockCaption" style="display: none;"/>
<div class="mstrListBlockHeader" style="display: none;">
<div style="" class="mstrListBlockContents" 
id="ListBlockContents_id_mstr51">
<div oncontextmenu="return 
mstr.behaviors.Generic.oncontextmenu(arguments[0], self, 'id_mstr51');" 
onmouseup="try{mstr.$obj('id_mstr51').focus();}catch(localerr){}; return 
mstr.behaviors.Generic.clearBrowserHighlights(self)" onmousedown="var retVal 
= mstr.behaviors.ListView.onmousedown(arguments[0], self, 'id_mstr51'); 
try{mstr.$obj('id_mstr51').focus();}catch(localerr){}; return retVal" 
ondblclick="return mstr.behaviors.ListView.ondblclick(arguments[0], self, 
'id_mstr51')" class="mstrListBlockListContainer" id="id_mstr51ListContainer" 
style="display: block;">
<div class="mstrListBlockItem" title="Huidige Week">
<div class="mstrListBlockItemSelected" title="Vorige Week">
<div class="mstrBGIcon_fi mstrListBlockItemName" style="background-position: 
2px 50%; padding-left: 23px;">Vorige Week</div>
</div>
<div class="mstrListBlockItem" title="Afgesloten 4 Weken">
<div class="mstrListBlockItem" title="Afgesloten 8 Weken">
<div class="mstrListBlockItem" title="Huidige Periode">
<div class="mstrListBlockItem" title="Vorige Periode">
<div class="mstrListBlockItem" title="Afgesloten 2 Perioden">
<div class="mstrListBlockItem" title="Selectie Datum Hiërarchie. Aangepast 
wegens IServer crash icm. Metric prompts.">
<div class="mstrListBlockItem" title="Gisteren">

我认为我的问题在于决定我需要使用哪个元素来获得预期的结果

Sub JDWReport()

Dim objIE As InternetExplorer

Set objIE = New InternetExplorerMedium
objIE.Visible = True

objIE.navigate "URL"

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

objIE.document.getElementById("Uid").Value = "username"
objIE.document.getElementById("Pwd").Value = "password"
objIE.document.getElementById("3054").Click

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
objIE.navigate "URL2"

Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop
objIE.document.getElementsClassName("mstrBGIcon_fi 
mstrListBlockItemName")(0).Click

objIE.Quit
End Sub

请参阅上面我目前正在使用的代码。它卡在 objIE.document.getElementsClassName("mstrBGIcon_fi mstrListBlockItemName")(0) 行上。我尝试根据 HTML 代码将此行更改为不同的元素并使用 .click .selectedindex=2 但这些不起作用。

<div class="mstrListBlockItemSelected" title="Vorige Week">

目前它说 mstrListBlockItemSelected,但是,当第一次导航到该站点时,该类被定义为其余部分,mstrListBlockItem。

仅当您单击有问题的项目(从项目列表中)时,它才会变为选中状态。我的最终目标是让标题为“Vorige Week”的课程从 mstrListBlockItem 更改为 mstrListBlockItemSelected。

标签: htmlexcelvbainternet-explorerweb-scraping

解决方案


您可以尝试使用属性 = 值 CSS 选择器:

ie.document.querySelector("[title='Vorige Week']").Selected = True

或者

ie.document.querySelector("[title='Vorige Week']").Click

推荐阅读