无法使用IE从下拉选项中选择项目

时间:2017-12-05 20:09:32

标签: vba excel-vba internet-explorer web-scraping excel

我正在尝试点击下拉选项并选择其中的任何项目,但点击事件未被执行。只要浏览器点击包含elem.click的行,就会抛出错误object variable or with block variable not set。我怎样才能修补下面的努力才能让它成为现实?提前感谢任何解决方案。

到目前为止我所尝试的是:

Dim Hdoc as HTMLDocument, elem as object, s_item as object

Set Hdoc = IE.Document    ''here IE as New InternetExplorer
Set elem = Hdoc.getElementById("select")
elem.Click     ''error thrown here showing Object variable or with block ----

For Each s_item In Hdoc.getElementsByTagName("option")
    If InStr(1, s_item.innerText, "Asia Pacific Ex-Japan Fund", 1) > 0 Then s_item.Click: Exit For
Next s_item

嵌入了选择按钮和下拉选项的元素:

<html>
 <body>
  <select id="select" name="select" onchange="redirect(this.value);" style="width: 165px;">
   <option value="">
    Select a fund
   </option>
   <option value="/wps/myportal/mfs/non-us-advisor/products/meridian-funds/!ut/p/a1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOL9A40C_c09jAzcjYOdDYxcTPyNTUONDS1NjPW99KPSc_KTwEojHfOSjC3S9aOKUtNSi1KL9DLyi0v0I8rLy_Vy04r1kvNz9cOd9aMCEjMLMxz1C7KjkiodFRUBO3AG8w!!/dl5/d5/L2dJQSEvUUt3QS80SmlFL1o2X09RMlFPN0gyMEczU0MwMkQ0TzM1VTMxRk8y/?clearPortletSession=true&amp;productId=480&amp;shareId=33&amp;viewMode=profile">
    <!-- Do Nothing -->
    Absolute Return Fund
   </option>
   <option value="/wps/myportal/mfs/non-us-advisor/products/meridian-funds/!ut/p/a1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOL9A40C_c09jAzcjYOdDYxcTPyNTUONDS1NjPW99KPSc_KTwEojHfOSjC3S9aOKUtNSi1KL9DLyi0v0I8rLy_Vy04r1kvNz9cOd9aMCEjMLMxz1C7KjkiodFRUBO3AG8w!!/dl5/d5/L2dJQSEvUUt3QS80SmlFL1o2X09RMlFPN0gyMEczU0MwMkQ0TzM1VTMxRk8y/?clearPortletSession=true&amp;productId=1466&amp;shareId=33&amp;viewMode=profile">
    <!-- Do Nothing -->
    Asia Pacific Ex-Japan Fund
   </option>
   <option value="/wps/myportal/mfs/non-us-advisor/products/meridian-funds/!ut/p/a1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOL9A40C_c09jAzcjYOdDYxcTPyNTUONDS1NjPW99KPSc_KTwEojHfOSjC3S9aOKUtNSi1KL9DLyi0v0I8rLy_Vy04r1kvNz9cOd9aMCEjMLMxz1C7KjkiodFRUBO3AG8w!!/dl5/d5/L2dJQSEvUUt3QS80SmlFL1o2X09RMlFPN0gyMEczU0MwMkQ0TzM1VTMxRk8y/?clearPortletSession=true&amp;productId=139597&amp;shareId=33&amp;viewMode=profile">
    <!-- Do Nothing -->
    Blended Research European Equity Fund
   </option>
   <option value="/wps/myportal/mfs/non-us-advisor/products/meridian-funds/!ut/p/a1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOL9A40C_c09jAzcjYOdDYxcTPyNTUONDS1NjPW99KPSc_KTwEojHfOSjC3S9aOKUtNSi1KL9DLyi0v0I8rLy_Vy04r1kvNz9cOd9aMCEjMLMxz1C7KjkiodFRUBO3AG8w!!/dl5/d5/L2dJQSEvUUt3QS80SmlFL1o2X09RMlFPN0gyMEczU0MwMkQ0TzM1VTMxRk8y/?clearPortletSession=true&amp;productId=3014&amp;shareId=33&amp;viewMode=profile">
    <!-- Do Nothing -->
    Continental European Equity Fund
   </option>
   <option value="/wps/myportal/mfs/non-us-advisor/products/meridian-funds/!ut/p/a1/04_Sj9CPykssy0xPLMnMz0vMAfGjzOL9A40C_c09jAzcjYOdDYxcTPyNTUONDS1NjPW99KPSc_KTwEojHfOSjC3S9aOKUtNSi1KL9DLyi0v0I8rLy_Vy04r1kvNz9cOd9aMCEjMLMxz1C7KjkiodFRUBO3AG8w!!/dl5/d5/L2dJQSEvUUt3QS80SmlFL1o2X09RMlFPN0gyMEczU0MwMkQ0TzM1VTMxRk8y/?clearPortletSession=true&amp;productId=165855&amp;shareId=33&amp;viewMode=profile">
    <!-- Do Nothing -->
    Diversified Income Fund
   </option>
  </select>
 </body>
</html>

1 个答案:

答案 0 :(得分:0)

你能不能这样做 - 我认为这是因为Hdoc没有时间设置

    Dim Hdoc As HTMLDocument, elem As Object, s_item As Object

    Set Hdoc = IE.Document    ''here IE as New InternetExplorer
    Set elem = Hdoc.getElementById("select")
    Application.Wait(Now + TimeValue("00:00:02"))
    elem.Click     ''error thrown here showing Object variable or with block ----

    For Each s_item In Hdoc.getElementsByTagName("option")
        If InStr(1, s_item.innerText, "Asia Pacific Ex-Japan Fund", 1) > 0 Then s_item.Click: Exit For
    Next s_item