VBA互联网自动化:id不会带来价值

时间:2017-05-17 13:33:41

标签: vba getelementbyid microsoft-dynamics

我已经开始使用excel VBA在我的工作基于浏览器的CRM(Microsoft Dynamics)上学习业余Web自动化。 (我们不允许在我的工作计算机上使用powershell,python等)

从我的VBA代码中可以看出,我一直在使用HTMLdoc.getElementById(" elementname"),后跟.value或.click。

到目前为止,这是我的VBA代码:

Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.HTMLDocument
Dim HTMLinput As MSHTML.IHTMLElement
Dim HTMLspans As MSHTML.IHTMLElementCollection
Dim HTMLspan As MSHTML.IHTMLElement
Dim ele As Variant

 Dim HTMLimgs As MSHTML.IHTMLElementCollection
Dim HTMLimg As MSHTML.IHTMLElement

IE.Visible = True
IE.navigate "httpsWork.CRM.org.uk" '1) navigate to the CRM
'wait here a few seconds while the browser is busy
Do While IE.readyState <> READYSTATE_COMPLETE
Loop

Set HTMLDoc = IE.document
Set HTMLinput = HTMLDoc.getElementById("TabCS") 'press the dropdown arrow at the menu



 HTMLinput.Click
 Do While IE.readyState <> READYSTATE_COMPLETE
    Loop




     Set HTMLDoc = IE.document
  Set HTMLinput = HTMLDoc.getElementById("nav_cases") 'clicks "cases" button
  HTMLinput.Click
    Do While IE.readyState <> READYSTATE_COMPLETE
    Loop

不幸的是,我遇到了一个需要将值放入搜索框的墙,然后单击搜索按钮,无法使用此方法。我不知道为什么会这样,因为这个搜索框/元素看起来确实有id。

当我检查元素&#34;时,这是HTML代码的摘录。在搜索框中:

<input tabindex="0" title="Search for records" class="ms-crm-Dialog-Lookup-QuickFind" id="crmGrid_findCriteria" type="text" maxlength="100" value="" hintlabelid="crmGrid_findHintText">

我的理解是这个搜索框的ID是&#34; crmGrid_findCriteria&#34;

我的问题是为什么以下get elementid代码不起作用?:

Set HTMLDoc = IE.document

设置HTMLinput = HTMLDoc.getElementById(&#34; crmGrid_findCriteria&#34;)HTMLinput.value =&#34; casenumber&#34;     做IE.readyState&lt;&gt; READYSTATE_COMPLETE     环

感谢您的帮助!

此处编辑更多是上下文的HTML代码:

&#13;
&#13;
</SPAN><SPAN title="New&#13;&#13;Add info and activities." class="navTabButton navTabQuickCreateMargin" 
id="TabGQC"><A tabindex="0" title="New&#13;&#13;Add info and activities." class="navTabButtonLink" 
onkeypress="return true;" onclick="return false;" href="#" 
unselectable="on"><SPAN class="navTabButtonImageContainer" 
unselectable="on"><IMG class="globalCreateButtonImage" id="navTabGlobalCreateImage" 
alt="" src="/_imgs/NavBar/Invisible.gif" unselectable="on"></SPAN><SPAN class="navTabButtonLabel" 
unselectable="on"><SPAN class="navTabButtonText"></SPAN></SPAN></A></SPAN><SPAN 
title="Search CRM data" class="navTabButton searchIconOnly" id="TabSearch">
<TABLE width="100%" class="navSearchLayout" cellspacing="0" cellpadding="0">
  <TBODY>
  <TR>
    <TD valign="top" style="height: 100%; overflow: hidden;">
      <DIV style="position: relative;"><LABEL class="ms-crm-Dialog-Lookup-QuickFindHint ms-crm-TextAutoEllipsis navSearchLabelLayout" 
      id="findHintText" style="display: block;" for="search">Search CRM 
      data</LABEL></DIV>
      <DIV style="height: 100%;"><INPUT tabindex="0" class="ms-crm-Dialog-Lookup-QuickFind navBarSearchTextBox navTabHidden" id="search" style="border: currentColor; border-image: none; height: 100%;" onfocus="$('#findHintText').hide()" onblur="if($('#search').val().length  < 1) $('#findHintText').show();" type="text" maxlength="100" value=""></DIV></TD>
    <TD class="AppQuickFind_Render_td" id="meqfSearchButton" nowrap="" valign="top"><A 
      tabindex="0" title="Start search" class="ms-crm-FindButton navSearchButtonLayout" 
      id="findCriteriaButton" href="#" target="_self"><IMG title="Start search" 
      class="navImageFlipHorizontal" id="findCriteriaImg" style="padding-top: 4px; padding-left: 8px; vertical-align: middle;" 
      alt="Start search" 
src="/_imgs/NavBar/Invisible.gif"></A></TD></TR></TBODY></TABLE></SPAN><SPAN 
class="navTabQuickCreateSpacer" id="navTabSerachSpacer" style="width: 18px; display: inline;"></SPAN><SPAN 
title="Advanced Find&#13;&#13;Create advanced search queries." class="navTabButton navHomeButton navAdvSearch" 
id="AdvFindSearch"><A tabindex="0" title="Advanced Find&#13;&#13;Create advanced search queries." 
class="navTabButtonLink" onkeypress="return true;" onclick="return false;" href="#" 
unselectable="on"><SPAN class="navTabButtonImageContainer" 
unselectable="on"><IMG id="advancedFindImage" alt="Advanced Find&#13;&#13;Create advanced search queries." 
src="/_imgs/NavBar/Invisible.gif" unselectable="on"></SPAN></A></SPAN><SPAN 
title="Financial MyBusinessABC Service" class="navTabButton navTabButtonUserInfo navTabDisableHover navTabHidden" 
id="TabUserInfoId">				 <A class="navTabButtonLink" id="navTabButtonUserInfoLinkId" 
onkeypress="return true;" onclick="return false;" href="#" 
unselectable="on"><SPAN class="navTabButtonLabel navBarUserInfoAlign" id="navBarUserInfoTextId" 
unselectable="on"><SPAN title="John Smith" class="navTabButtonUserInfoText navTabButtonUserInfoWorker">John 
Smith</SPAN><SPAN title="Financial MyBusinessABC Service" class="navTabButtonUserInfoText navTabButtonUserInfoCompany">Financial 
MyBusinessABC Service</SPAN></SPAN>					 <SPAN class="navTabButtonImageContainer" 
id="navTabButtonChangeProfileImageLink" unselectable="on"><IMG class="navTabButtonUserInfoProfileImage" 
alt="John Smith" src="/_imgs/NavBar/EmptyUserImage.png?ver=-714152028" 
unselectable="on"></SPAN>				 </A>			
&#13;
&#13;
&#13;

0 个答案:

没有答案