簡體   English   中英

Excel VBA瀏覽器自動化:網站搜索框沒有元素ID

[英]Excel VBA browser automation: website search box has no element id

我正在嘗試使用Excel VBA來實現瀏覽器自動化,但是該網站的( http://www.soccerstats.com/ )搜索框沒有元素ID,因此通常的技術將不起作用。 這是我計划的代碼:

    'dimension (declare or set aside memory for) our variables
Dim objIE As InternetExplorer 'special object variable representing the IE browser
Dim aEle As HTMLLinkElement 'special object variable for an <a> (link) element
Dim y As Integer 'integer variable we'll use as a counter
Dim result As String 'string variable that will hold our result link

'initiating a new instance of Internet Explorer and asigning it to objIE
Set objIE = New InternetExplorer

'make IE browser visible (False would allow IE to run in the background)
objIE.Visible = True

'navigate IE to this web page (a pretty neat search engine really)
objIE.navigate "http://www.soccerstats.com/"

'wait here a few seconds while the browser is busy
Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

'in the search box put cell "A2" value, the word "in" and cell "C1" value
objIE.document.getElementById("dsearch").Value = "arsenal"

搜索框的html代碼為:

<input name="searchstring" style="border: currentColor; border-image: none; width: 90px; height: 20px; padding-left: 5px; background-color: rgb(238, 238, 238); text-color: gray;" onfocus="if (this.value == 'Search team...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search team...';}" type="text" maxlength="20" value="Search team...">

誰能告訴我如何在此搜索框中輸入一個值,然后單擊搜索按鈕?

您可以遍歷輸入元素並找到一個名為searchstring ,如下所示:

Dim ele As Object

For each ele in objIE.document.getElementsByTagName("input")
    If ele.Name = "searchstring" Then
        ele.Value = "Example Text"
    End if
Next ele

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM