在非标准HTML文本框中输入值

时间:2015-08-14 16:24:50

标签: vba internet-explorer web-scraping

我尝试使用VBA编辑此文本框:

<td id="ctl00_PlaceHolderMain_ctl00_PlaceHolderMain_ProfileEditorValueAboutMe" class="ms-authoringcontrols" valign="top" style="padding-left:10px;padding-right:10px;"><span class="ms-profilevalue"><span id="ctl00_PlaceHolderMain_ProfileEditorEditAboutMe"><DIV id="ctl00_PlaceHolderMain_ProfileEditorEditAboutMe_editableRegion" class="ms-rtestate-write ms-inputuserfield ms-long" contentEditable="true" InputFieldId="ProfileEditorEditAboutMe_hiddenRTEField" style="height:125px;overflow:scroll;background-color:white;"><p>text box content here</p></DIV></span>
                        <div class="ms-profiledescription" style="width: 386px; white-space:normal;">Please enter some details in the textbox</div>
                        </span>
                        <table border="0">
                            <tr>
                                <td></td>
                            </tr>
                        </table>
                    </td>

这是VBA,它按预期执行,除了文本框没有改变(在屏幕上或通过点击事件保存表单时):

    Dim ie As InternetExplorerMedium 
Set ie = New InternetExplorerMedium
ie.Visible = True
ie.Navigate "URL"

While ie.Busy Or ie.ReadyState <> READYSTATE_COMPLETE
    DoEvents
Wend


Debug.Print ie.Document.getelementbyid("ProfileEditorEditAboutMe_hiddenRTEField").Value
ie.Document.getelementbyid("ProfileEditorEditAboutMe_hiddenRTEField").Value = "<p>123</p>"
Debug.Print ie.Document.getelementbyid("ProfileEditorEditAboutMe_hiddenRTEField").Value

ie.Document.all("Button1").Click

.value属性确实发生了变化,但文本框没有变化。

我会感激任何想法。你可以说,我对VBA webscraping相当新。

1 个答案:

答案 0 :(得分:0)

由于您自己操纵文本框的值,因此您需要触发onchange事件,以便文本框重新显示您的新值。这是通过创建一个事件对象,然后通过dispatchEvent()函数调用它来完成的。

Dim e
Set e = ie.Document.getelementbyid("ProfileEditorEditAboutMe_hiddenRTEField")

' Set the value...
e.Value = "123"

' Fire the "onchange" event...
Dim objEvent
Set objEvent = ie.document.createEvent("HTMLEvents")
objEvent.initEvent "change", False, True
e.dispatchEvent objEvent