2017-10-12 8 views
1

我比较新,所以很抱歉,如果这是基本的。这里没有什么帮助我。我试图自动化一个函数,但无法解决这个问题。当我在网站上搜索某个东西时,它会返回一个表格,并且点击任何搜索选项都非常简单,因为功能菜单ID就像x1一样简单。然而,图表的顶部有一个“最佳赌注”行,这个总是我希望它选择的选项,这个ID根据变量I搜索而改变,所以我不能根据ID不断地选择它。我不想打开功能菜单,而只想点击最佳下注行中的按钮。任何的意见都将会有帮助。对于上下文GetElementByID(“x1017538”)。Click是发生问题的位置。我很难从IE网站的表格中点击最佳投注搜索结果

Sub Mirror_Mover() 

Dim IE As Object 
Set IE = CreateObject("InternetExplorer.Application") 

Dim MMD As Workbook 
Set MMD = ThisWorkbook 

Dim Mirror_Mover As Worksheet 
Set Mirror_Mover = Sheets("Mirror Mover") 

Dim inputRng As Range 
Dim lastRow As Integer 
Set inputRng = Mirror_Mover.Range("A:D") 
lastRow = Application.WorksheetFunction.Max(Range("A" & Rows.Count).End(xlUp).Row, Range("D" & Rows.Count).End(xlUp).Row) 

Dim Box As Range 
Dim LLT As Range 
Dim PRO As Range 
Dim FLD As Range 

Dim i As Integer 
For i = 1 To lastRow 
If inputRng(i, 1).Value <> vbNullString Then 
Set Box = inputRng(i, 1) 
Set LLT = inputRng(i, 2) 
Set PRO = inputRng(i, 3) 
Set FLD = inputRng(i, 4) 

'Open Internet Explorer and Navigate main page 
IE.Visible = True 
IE.Navigate "website" 
Application.Wait (Now + #12:00:05 AM#) 

'Locate the item to which folder will be added 
IE.Document.GetElementByID("fulltextwhere1").Value = PRO 
IE.Document.GetElementByID("fulltextsubmitButton").Click 
Application.Wait (Now + #12:00:05 AM#) 

'Open the top search result 
IE.Document.GetElementByID("x1017538").Click 
Do While IE.Busy: DoEvents: Loop 
Do While IE.ReadyState <> 4: DoEvents: Loop 
IE.Document.GetElementByID("funcMenu101753816.1").Click 
Do While IE.Busy: DoEvents: Loop 
Do While IE.ReadyState <> 4: DoEvents: Loop 

<TR CLASS="bestBetRow1" TITLE="Best Bet 1"> 
 

 
<TD ALIGN="CENTER" WIDTH="1%" VALIGN="TOP" STYLE="padding: 3px" NOWRAP>&nbsp;<IMG HEIGHT="9" ALT="" HSPACE="3" SRC="/img/spacer.gif" WIDTH="11" BORDER="0"></TD> 
 
<TD NOWRAP ALIGN="CENTER" TITLE="Score"> 
 

 
&nbsp; 
 
</TD> 
 

 
<TD NOWRAP ALIGN="CENTER" TITLE="MIME Type"> 
 

 

 

 

 

 

 

 
<A HREF="cs.exe?func=ll&amp;objId=1017538&amp;objAction=browse"><img class="objectIcon" src="/img/webdoc/folder.gif" alt="Folder" title="Folder"></A> 
 
</TD> 
 

 
<TD NOWRAP TITLE="TEXT"> 
 

 

 
<SPAN CLASS="important"><A HREF="cs.exe?func=ll&amp;objId=1017538&amp;objAction=browse">TEXT</A></SPAN> 
 
<A HREF="#" ONCLICK="setSectionName('');showFunctionMenu2('nextURL=%2Fotcs%2Fcs%2Eexe%3Ffunc%3Dsrch%2ESearchCache%26cacheId%3D697991735', '1017538', event, '', '');setSectionName('');return false"><IMG SRC="/img/actions.png" ID="x1017538" BORDER="0" ALT="Functions" TITLE="Functions" ONMOUSEOVER="this.src='/img/actions_hover.png'" ONMOUSEOUT="this.src='/img/actions.png'"></A> 
 
<DIV ID="z1017538" CLASS="functionMenuDiv" ></DIV> 
 

 
&nbsp;<SPAN CLASS="bestBetLabel">-&nbsp;Nickname&nbsp;(TEXT)</SPAN> 
 

 
</TD> 
 

 
<TD NOWRAP ALIGN="CENTER" TITLE="Date: 09/28/2017"> 
 
09/28/2017 
 

 
</TD> 
 

 
<TD NOWRAP ALIGN="CENTER" TITLE="Size: 21 Items"> 
 

 
21 Items 
 
</TD> 
 

 
<TD NOWRAP TITLE="Location: Enterprise:TEXT"> 
 

 
<DIV class="search-breadcrumb-trail"> 
 
<A HREF="cs.exe?func=ll&objId=2000&objAction=browse" CLASS="locationDisplayTrailNonterminalElement" TITLE="Go to Enterprise">Enterprise</A> 
 
<SPAN CLASS="separatorImage">&nbsp;</SPAN> 
 
<A HREF="cs.exe?func=ll&objId=611814&objAction=browse" CLASS="locationDisplayTrailNonterminalElement" TITLE="Go to Properties">Properties</A> 
 
<SPAN CLASS="separatorImage">&nbsp;</SPAN> 
 
<A HREF="cs.exe?func=ll&objId=611815&objAction=browse" CLASS="locationDisplayTrailNonterminalElement" TITLE="Go to TEXT">TEXT</A> 
 
<SPAN CLASS="separatorImage">&nbsp;</SPAN> 
 
<A HREF="cs.exe?func=ll&objId=611882&objAction=browse" CLASS="locationDisplayTrailNonterminalElement" TITLE="Go to TEXT">TEXT</A> 
 
<SPAN CLASS="separatorImage">&nbsp;</SPAN> 
 
<A HREF="cs.exe?func=ll&objId=611889&objAction=browse" CLASS="locationDisplayTrailTerminalElement" TITLE="Go to TEXT">TEXT</A> 
 
</DIV> 
 

 

 
</TD> 
 

 
<TD NOWRAP ALIGN="CENTER" TITLE="OTCreatedBy"> 
 

 

 

 
<A HREF="#" onClick="doUserDialog('608559')">Properties Folder Administrator (property_administrator)</A> 
 
<SCRIPT LANGUAGE="JavaScript"> 
 

 
function doUserDialog(userID) 
 
{ 
 
var \t \t w = window.open('/cs.exe?func=user.userdialog&userID=' + userID,'UserDialog','width=600,height=440,resizable=yes,scrollbars=yes,menubar=no'); 
 

 
if (w.focus) 
 
{ 
 
w.focus(); 
 
} 
 
} 
 

 
</SCRIPT> 
 

 
</TD> 
 

 
</TR>

+0

不知道我明白你得到了什么错误。 OnClick似乎按照您的预期运行,只是没有定义函数“setSectionName”(大概这将出现在整个页面中)。 – JohnRC

+0

唯一的另一个想法是元素'x1017538'没有'onClick'方法,你可能需要调用父锚对象的方法吗? – JohnRC

+0

对不起,我看到我不是很清楚。该x1017538工作正常,但该号码是唯一的第一个整数我正在寻找,当它搜索第二个或任何后来我得到一个错误,因为它将是x1017545(例如)和代码仍将尝试选择x1017538。 我想要它做的是能够选择任何最高结果,而不管x#是什么。没有固定的ID。 – bjankovs

回答

0

修订后的2017年10月27日,以考虑下面的评论。 2017年2月2日修正,以解决编码错误

您可以尝试类似下面的内容。

请注意此代码尚未编译或调试。

'// Find the best bet row <TR> element 
Dim bestBetRow As Object 
Set bestBetRow = IE.Document.getElementsByClassName("bestBetRow1")(0) 

'// Use the findRequiredLink function to find the element to be clicked 
Dim link As Object 
Set link = findRequiredLink(bestBetRow) 

'// Check that the element is found and click it if so 
If Not link Is Nothing Then 
    link.Click 
End If 

: 
: 

Function findRequiredLink(parent As Object) As Object 
'// This function uses recursion from a root node 
'// to find a specific IMG node that meets the required 
'// criteria, i.e. has the TITLE attribute set to "Functions" 

    Dim child As Object 
    Dim ix as integer 

    '// check each child node in turn 
    For ix = 0 to parent.ChildNodes.Length-1 

     '// get the child node 
     Set child = parent.ChildNodes.Item(ix) 

     '// does it meet the required criteria? 
     if child.tagName = "IMG" and _ 
      child.getAttribute("TITLE")="Functions" then 
      set findRequiredLink = child 
      exit function 
     end if 

     '// Recursively check the children of the child 
     Set child = findRequiredLink(child) 

     '// Check if the anchor was found amongst the children 
     if Not child Is Nothing then 
      Set findRequiredLink = child 
      exit Function 
     End If 

    Next ix 

    '// No matching node was found - return nothing 
    Set findRequiredLink = Nothing 
end function 
+0

忙碌了一周,还没有能够有效地测试,但这看起来完全一样,我正在寻找。谢谢 – bjankovs

+0

所以我把它全部编译出来,并且有几个问题: 1-设置bestBetRow = IE.Document.GetElementByID(“bestBetRow1”) 是否有你在ID中使用而不是类的原因,因为它没有看对我来说这个ID是否存在? 2-我在函数的开头得到一个错误,它突出显示bestbetrow1为未定义的行开始对于ix = – bjankovs

+0

是的你说得对,语句应该使用'getElementsByClassName'而不是ID。我猜这个类名只有一个元素,所以你可以认为它是集合中返回的唯一元素?如果不是,你可能不得不使用更多的逻辑来找到正确的。 – JohnRC

相关问题