我目前在嘗試將文本插入網站“www.skyvector.com”時遇到問題。我一直在嘗試在“路線”欄位中粘貼一些文本,該欄位顯示在左上角的灰色框中(通常在單擊“飛行計劃”之后)。
這是我到目前為止的代碼,它適用于其他網站,但奇怪的是不適用于 SkyVector:
Sub test1()
Dim IE As Object
Dim doc As HTMLDocument
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "http://www.skyvector.com/"
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = IE.document
doc.getElementsById("sv_planEditField").Value = "test"
End Sub
不幸的是,只要將此行設定為運行,就會出現錯誤:
doc.getElementsById("sv_planEditField").Value = "test"
錯誤是“運行時錯誤'438':物件不支持此屬性或方法”。
一直在為解決這個問題而絞盡腦汁,但我在這里也找不到任何解決方案,特別是對于像 SkyVector 這樣的網站。我不確定它與任何其他網站之間有什么區別。非常感謝您的寶貴時間!
uj5u.com熱心網友回復:
首先,方法名不是getElementsById(). 這個名字getElementById()沒有s復數形式。原因是,一個 ID 只能在 html 檔案中使用一次,它是唯一的。
但是,如果您使用正確的名稱,您將收到沒有物件的錯誤。這里的原因是,沒有 ID 名為 的元素sv_planEditField。
所以,你可以做什么?您可以使用另一種稱為的方法,getElementsByClassName()因為有問題的 html 行是
<input autocomplete="false" spellcheck="false" class="sv_search" autocorrect="off">
該方法getElementsByClassName()建立一個節點集合。因此它使用s復數形式。開發人員可以有盡可能多的具有相同類名的元素。您可以通過它的索引獲取特定元素,就像將它與陣列一起使用一樣。clss 名稱sv_search僅在檔案中使用一次。節點集合的第一個索引是 allways 0。因此,您必須使用以下 vba 代碼行,而不是您的:
doc.getElementsByClassName("sv_search")(0).Value = "test"
編輯
在再次閱讀您的問題并理解它之后;-) 并根據 Sam 的回答,這是您解決問題的方法。您需要的是一個新的文本節點和(我認為)觸發正確的事件以使輸入適用于頁面。用原始資料試試。
Sub test1()
Dim IE As Object
Dim textToEnter As Object
Dim nodeToAppendText As Object
Dim nodeText As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "http://www.skyvector.com/"
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
'Open overlay to enter data
IE.document.getElementsByClassName("sv_topbarlink")(0).Click
'Click textfield to hide helptext and place curser
IE.document.getElementsByClassName("svfpl_helpmessage")(0).Click
'Create a text node which belongs to the document
Set textToEnter = IE.document.createTextNode("Test")
'Get the node you want to append the new text node
Set nodeToAppendText = IE.document.getElementById("sv_planEditField")
'Append the new text node
Set nodeText = nodeToAppendText.appendChild(textToEnter)
'Not sure if it is necessary to trigger an event
'But there are two events in question:
' First one is input
' Second one is keypress
'You must try how it works
Call TriggerEvent(IE.document, nodeToAppendText, "input")
End Sub
如果需要,請使用此方法觸發任何事件:
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
Dim theEvent As Object
htmlElementWithEvent.Focus
Set theEvent = htmlDocument.createEvent("HTMLEvents")
theEvent.initEvent eventType, True, False
htmlElementWithEvent.dispatchEvent theEvent
End Sub
uj5u.com熱心網友回復:
該元素sv_planEditField不是普通的文本框。在瀏覽器中打開它并使用開發人員工具檢查它(按 F12)。在填充之前和之后都這樣做。您會注意到這與標準輸入完全不同。重新創建填充控制元件的 html 結構或重新創建表單提交。查看 createElement 和 appendChild 了解更多資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/419314.html
標籤:
