[英]How to find a button on webpage
我正在嘗試在網頁上執行多項操作以幫助用戶設置他們的偏好。 我正在使用 Excel VBA,因為它是公司自動執行任務的唯一方法。
首先,他們必須登錄,這是我能夠執行的操作。 然后,我瀏覽到相應的頁面。
問題發生在這里:使用 Internet Explorer,如果我右鍵單擊頁面並顯示源代碼,則只顯示頁面的一部分。 另外,根據我執行此操作的區域,源代碼不同,它不是一個整體。
如果我使用 DOM Explorer,事情就不一樣了。 顯示完整的頁面代碼,我可以指向要單擊的按鈕。
按鈕代碼:
<a href="javascript:somewords('somelink',var1,var2,false)">Click here !</a>
我的代碼:
Sub Connect_to_system()
Set IE = New InternetExplorerMedium
Dim IEDoc As HTMLDocument
Dim htmlTagCol As IHTMLElementCollection
Dim Generic As HTMLGenericElement
With IE
.Visible = True
.navigate "http://example.com/login?username=johndoe&password=123"
Do Until .readyState = 4
DoEvents
Loop
.navigate "http://example.com/somepage.jsp?reference123"
Do Until .readyState = 4
DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:10"))
Set tags = IE.document.getElementsByTagName("a")
For Each tagx In tags
If tagx.href = "javascript:somewords('somelink',var1,var2,false)" Then
tagx.Click
End If
Next
End With
End Sub
我的代碼循環了 3 次(所以我知道它檢測到標簽)但沒有一個符合我的條件。 所以它退出循環,什么也沒有發生。
我試圖查看標簽檢測到的內容,似乎只有頁面的頂部功能區被識別/加載。
即使使用 InnerText 函數,這些方法也不起作用。
使用您提供的最低限度的 html 代碼,這將不得不做。
您可以使用innerHTML
將元素上的字符串值與 a 的標記名稱進行a
。
Sub ieBusy(ie As InternetExplorerMedium)
Do While ie.Busy Or ie.readyState < 4
DoEvents
Loop
End Sub
Sub Connect_to_system()
Dim ie As New InternetExplorerMedium, aElemColl As Object, aElem As Object
With ie
.Visible = True
.navigate "http://example.com/login?username=johndoe&password=123"
ieBusy ie
.navigate "http://example.com/somepage.jsp?reference123"
ieBusy ie
Set aElemColl = .document.getElementsByTagName("a")
End With
For Each aElem In aElemColl
If aElem.innerHTML Like "*javascript:somewords('somelink',var1,var2,false)*" Then
aElem.Click
Exit For
End If
Next
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.