在网页上的搜索框中输入文字,然后点击搜索

时间:2019-04-23 07:04:36

标签: html excel vba web-scraping

我试图将数据从Excel工作簿输入到网页搜索字段,然后单击搜索按钮。许多不同的方法已经成功测试。当前解决方案给出424错误,需要对象。指向:

.Document.getElementById("crmGrid_findCriteria").Value = _
      Sheets("Other data").Range("A2").Value

用于搜索的HTML字段:

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

“ HTML搜索”按钮:

<img id="crmGrid_findCriteriaImg" src="/_imgs/imagestrips/transparent_spacer.gif" class="ms-crm-ImageStrip-search " imgbase="/_imgs/search" title="Start search" alt="Start search">

这是结构:

enter image description here

这是我的VBA(CRM系统的链接,因此它不是公开的):

Sub GoToOpport()

    'This will load a webpage in IE
    Dim ieappIE As InternetExplorerObject
    Dim HWNDSrc As Long
    Dim elementsSet AsappIE Object= CreateObject("internetexplorer.application")

    Set ie = Nothing With appIE
    Set ie = New InternetExplorerMedium      
    ie.Visible = True

    ie.Navigate "https://crm.dynamics.com/main.aspx?area=nav_oppts&etc=3&page=SFA&pageType=EntityList&web=true"

    With ie

    Do
    DoEvents
    Loop Until ie.ReadyState = READYSTATE_COMPLETE   
    End With

ie.Document.getElementById("contentIFrame0").contentDocument.getElementById("crmGrid_findCriteria").Value = "hello"

    'Unload IE
    Set ieappIE = Nothing
End Sub

错误:

enter image description here

2 个答案:

答案 0 :(得分:1)

它在iframe中,因此您需要访问它

ie.document.getElementById("contentIFrame0").contentDocument.getElementById("crmGrid_findCriteria")

您可以使用js尝试分配值

ie.document.parentWindow.execScript "document.getElementById('contentIFrame0').document.getElementById('scrmGrid_findCriteria').value = 'hello';" 

您还可以尝试导航到iframe的src

ie.navigate2 ie.document.querySelector("#crmContentPanel").src
 While ie.Busy Or ie.readyState < 4: DoEvents: Wend 
With ie.document.getElementById("crmGrid_findCriteria")
    .focus
    .value = "abc"
End With

答案 1 :(得分:0)

发送GET请求时,我的状态为301 Moved Permanently,然后重定向到这里:

  

https://dynamics.microsoft.com/en-us/crm/what-is-crm/

在HTML响应中找不到id="crmGrid_findCriteria",所以我想如果您尝试:

debug.print ie.Document.getElementById("crmGrid_findCriteria") Is Nothing

结果将得到TRUE

换句话说,您要查找的元素在您下载的HTML中不存在。