我正在尝试在网页上执行多项操作以帮助用户设置他们的首选项。我正在使用 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
进行比较。
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
关于javascript - 如何在网页上找到按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48449435/