我正在尝试点击下拉选项并选择其中的任何项目,但点击事件未被执行。只要浏览器点击包含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&productId=480&shareId=33&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&productId=1466&shareId=33&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&productId=139597&shareId=33&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&productId=3014&shareId=33&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&productId=165855&shareId=33&viewMode=profile">
<!-- Do Nothing -->
Diversified Income Fund
</option>
</select>
</body>
</html>
答案 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