搜索HREF链接会拉回错误的链接

时间:2015-06-09 15:50:04

标签: javascript html excel vba excel-vba

我尝试使用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

当此代码运行时,它始终会打开一个与我搜索的内容无关的页面。事实上,&#34; / ao / party / popuppartyinfo?partyId&#34;中没有一个词。甚至出现在错误打开的链接中。

我想要获取的代码的HTML如下:

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

除了&#34; partyID&#34;以外,每个客户的整个href保持不变。变化的部分。

我的问题是我在这里做错了什么,这个搜索是为了取回与我指定的innerText无关的结果?

1 个答案:

答案 0 :(得分:1)

想出来: 链接的innertext是一个改变每一页的东西,所以无论出于何种原因,这都会让人感到奇怪......打开错误的链接&#34;在我的代码中打嗝。 这是工作代码:

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