网页上的Excel VBA:不填写textarea
我很难过 我试图用vba来填写描述文本区域的文本“嗨”,但我得到错误。 我究竟做错了什么?
以下是网页: http : //toronto.kijiji.ca/c-PostAd?AdVideoUrl=&AddressCity=&AddressCounty=&AddressRegion=&AddressSelectedByUser=false&AddressStreet=&AddressStreetName=&AddressStreetNumber=&AddressZip=&Alley=&CPUrl=&CatId=212&ChangeCategory=&ChangePhoto=&ContactInfo=&Description= &DiscountPrice =&电子邮件=&EpsErrorCode =&EventEndDay = 18&EventEndHour = 23&EventEndMinute = 59&EventEndMonth = 5&EventEndSecond = 3&EventEndYear = 2014&EventStartDay = 18&EventStartHour = 0&EventStartMinute = 0&EventStartMonth = 5&EventStartSecond = 3&EventStartYear = 2014&GetNeighborhood =&的Guid =&JavaScriptNotEnabled =假泳道=&MapAddress =&总数=&PaypalEmailAddress =&PaypalMobileNumber =&电话=&照片= &PhotoDesc =&PostAs = POST_AS_OWNER&PreviewAd =&PreviewToEdit =&价格=&RequestRefererUrl%2C&RoadStreet =&SelectedLeafCat = SelectedLeafCat&ShowPhone =检查&ShowTerms =&子编号=&缩略图=&名称=&TransDescription =&TransTitle =&VinNumber =&WebsiteUrl =&action_cancel =&action_publish =&carproof =&cplink =&cpvin =&extraInfo =&machId =&numActiveAdsInCategory =&TEX t_loading =&TmID是否=&useBasicUpload =假utm_campaign =&utm_medium =&utm_source =
下面是元素: <textarea name="Description" id="hdnDescription" wrap="soft" cols="71" maxlength="100000" style="width:500; height:240;font-family:arial;" ondescriptiondest="0" rows="6" class="tipField"></textarea>
<textarea name="Description" id="hdnDescription" wrap="soft" cols="71" maxlength="100000" style="width:500; height:240;font-family:arial;" ondescriptiondest="0" rows="6" class="tipField"></textarea>
这是我尝试的代码:
Sub test() Dim objIE As Object Set objIE = CreateObject("InternetExplorer.Application") With objIE .Visible = True .navigate Website Do While .Busy Or .readyState <> 4 DoEvents Loop ' Description objIE.document.getElementByID("hdnDescription").Value = "hi" End With End Sub
我也尝试使用getElementsByName而不是由ID,但是也没有工作:
objIE.document.getElementsByName("Description").Item(0).Value = Description
帮助将不胜感激! 谢谢!
注意:也张贴在这里: http : //www.mrexcel.com/forum/excel-questions/785128-visual-basic-applications-webpage-not-filling-textarea.html#post3842132
如果我使用Chrome打开您的url,我会看到您所描述的textarea hdnDescription。
如果我用IE11打开你的URL,textarea hdnDescription就会呈现不可见状态,但是带有IFrame和自己的HTML内容的富文本编辑器是可见的。 为什么? 没有线索,但是像这样,代码行objIE.document.getElementByID("hdnDescription").Value = "hi"
将无法按预期工作。
所以我select了一个结构更简单的网站: https ://www.netvoip.ch/support textarea就在最底层。
然后,你的这个URL的片段工作得很好。 我看到在textarea出现的“hi”:
Dim objIE As Object Set objIE = CreateObject("InternetExplorer.Application") Dim webSite As String webSite = "https://www.netvoip.ch/support" With objIE .visible = True .navigate webSite Do While .Busy Or .ReadyState <> 4 DoEvents Loop ' Description objIE.document.getElementByID("description").value = "hi" End With
由于该机制工作正常,它可能最终是,你也必须使用它也为Kijiji虽然textarea hdnDescription呈现为隐形,因为表单提交将无论如何将采取它的价值。
当文档开始加载时,IE应用程序进入readyState 4状态。 您必须等待文档readyState,然后才能开始更改DOM。
这是我用来等待来自另一个项目的IE的子。
Private Sub WaitUntilLoaded(ie As Object) Dim i As Long Dim ready As Boolean ' wait for page to connect i = 0 Do Until ie.readyState = READYSTATE_COMPLETE Application.Wait Now + TimeValue("0:00:01") i = i + 1 If i > 300 Then Err.Raise 513, , "Timeout" End If Loop ' wait for document to load Do Until ie.document.readyState = "complete" Application.Wait Now + TimeValue("0:00:01") i = i + 1 If i > 300 Then Err.Raise 513, , "Timeout" End If Loop End Sub
另外,它是getElementsByTagName
,而不是getElementsByName
。
“说明”框位于iframe中。 通常,要与驻留在iframe中的元素进行交互,请从标记中提取src属性,然后导航到由该src和基础URL形成的URL。 然后,在新的网页,build设像
objIE.document.getElementByID(“rteDescription”)。Value =“hi”
应该pipe用。 但是,当我尝试将src添加到基础url时,在这种情况下,新页面没有描述框(或任何其他)与交互。 不明白为什么这种方法在这种情况下失败。
可以说,HTML textarea的标签是:
<textarea id="hdnDescription" ></textarea>
然后使用.innerHTML在textarea中提供文本,VBA代码提供文本“Hello World”:
ObjIE.Document.getElementById(“hdnDescription”).innerHTML= “Hello World”
尝试这个。
objIE.getElementsBytagname("textarea").Item.Value = "Your text."