簡體   English   中英

搜索HREF鏈接正在拉回不正確的鏈接

[英]Searching for HREF links is pulling back incorrect links

我正在嘗試使用Excel的VBA自動查找和單擊某個鏈接。 問題是此鏈接每天(或每個人)都在更改。 事實是,HREF屬性的開頭始終相同,並且是該頁面上唯一具有這種HREF的鏈接。

我正試圖不要讓它變得太復雜,但我想對此進行解釋,以便您了解我的代碼以及執行該操作的原因:該鏈接打開了一個新頁面,但是由於使用VBA似乎很難管理多個瀏覽器實例,我認為最好放一些代碼,使其導航到同一瀏覽器中的鏈接,然后根據需要使用objIE.back進行循環。

我搜索了一次,該鏈接是該文檔中的第59個鏈接,因此我嘗試了如下代碼:

If link59 <> "*ao/party/popuppartyinfo?party*" Then
hrefvalue = line59
'Gets rid of the JavascriptPopup unnecessary text
trimedhrefvalue = Right(link59, 49)
hrefurlvalue = "https://myjobswebsitehere.com" & trimedhrefvalue
objIE.navigate hrefurlvalue

代碼本身可以工作,但是根據帳戶的不同,此鏈接顯然並不總是頁面中的第59個鏈接。

所以現在我有以下內容:

Application.StatusBar = "Trying to find link..."
Application.Wait (Now() + TimeValue("00:00:05"))
'objIE.document.parentWindow.execScript "execute('RefreshList');"
Set ieLinks = objIE.document.getElementsByTagName("a")
Do Until Progress = True
For Each Links In ieLinks
If Links.innerText = "/ao/party/popuppartyinfo?partyId" Then
'Links.Click
Application.StatusBar = "Found link! Please wait"
Links.Value = hrefvalue
trimedhrefvalue = Right(Links, 49)
hrefurlvalue = "https://myjobswebsitehere.com" & trimedhrefvalue
Progress = True
Exit For
End If
Next Links
Loop

運行此代碼時,它將始終打開一個與我搜索的內容無關的頁面。 實際上,“ / ao / party / popuppartyinfo?partyId”中的任何單詞都不會出現在打開不正確的鏈接中。

我嘗試獲取的代碼的HTML如下:

<a href="javascript:fnOpenWindow('/ao/party/popuppartyinfo?partyId=11111&nav=off')">

除了更改的“ partyID”部分外,每個href的整個href保持不變。

我的問題是我在這里做錯了什么,導致此搜索撤回與我指定的innerText沒有任何關系的結果?

弄清楚了:鏈接的內在文本改變了每個頁面,因此無論出於何種原因,這都使我的代碼中出現了奇怪的“打開錯誤的鏈接”錯誤。 這是工作代碼:

Application.StatusBar = "Trying to find link..."
Application.Wait (Now() + TimeValue("00:00:05"))
'objIE.document.parentWindow.execScript "execute('RefreshList');"
Set ieLinks = objIE.document.getElementsByTagName("a")
Do Until Progress = True
For Each Links In ieLinks
If Links.outerHTML Like "<A href=""javascript:fnOpenWindow('/ao/party/popuppartyinfo?partyId=*" Then
'Links.Click
Application.StatusBar = "Found link! Please wait"
Links.Value = hrefvalue
trimedhrefvalue = Right(Links, 49)
hrefurlvalue = "https://myjobswebsitehere.com" & trimedhrefvalue
objIE.navigate hrefurlvalue
Progress = True
Exit For
End If
Next Links

暫無
暫無

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

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