如何使用 Access VBA 在 Internet Explorer 中定位特定元素
How to Target Specific Element in Internet Explorer using Access VBA
首先,如有错误请见谅,我是第一次发帖(虽然我是常客!
我正在研究一个项目来提高我工作组的工作效率。我正在尝试 link 一个 Access 数据库到一个非常复杂的内部 Web 应用程序(基于 Pega)。不幸的是,我的编码知识非常基础,而且我没有可用的资金或带宽来获得正式的帮助。虽然我很想使用加载项,但我不能,因为我的公司不允许这样做(我已经询问、恳求、恳求等)。
This 就是程序页面的样子。我首先需要单击以绿色阻塞的工作管理器选项卡 (WMT),然后我需要单击以红色阻塞的获取下一步按钮 (NGN)。
我认为需要采取的步骤
- 绑定正确的 IE window 会话
- 点击 WMT
- 点击 NGN
我已经能够使用Access绑定正确的window和IE的标签页(感谢为此提供基本代码的人!)但还不能破译如何定位页面上的正确元素。就我的知识而言,命名结构有点太多了。
This 是 WMT 的 HTML 的屏幕截图(它不会复制它,至少不清晰,即使在清理之后)。我希望能够使用特定元素的 ID,但是,它是动态的,并且会随着每次交互而变化,这就是为什么我一直试图以静态部分为目标,以绿色突出显示。
我没有对第二部分做太多工作,但是,This 是它的屏幕截图,按钮以黄色突出显示,如果有人也想帮忙的话。
谢谢!
WINDOW 绑定的代码(到目前为止一直有效!)
Sub C360WindowFind()
marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next
my_url = objShell.Windows(x).document.Location
my_title = objShell.Windows(x).document.Title
If my_title Like "Coverage User" & "*" Then
Set C360Window = objShell.Windows(x)
marker = 1
Exit For
Else
End If
Next
If marker = 0 Then
MsgBox ("C360 window is not found. Please ensure C360 is open in Internet Explorer and try again")
Else
'DO THE OTHER THINGS
End If
End Sub
CODE I'VE TRIED TO TARGET CORRECT TAB(添加以代替先前代码集中的 "DO THE OTHER THINGS")
首先尝试(做一些疯狂的事情,最终导致程序退出)
FindWorkManager1()
Dim C360doc As HTMLDocument
Dim Element
Dim ULTabName As String
Dim LIElement As String
Dim WMT_HREF As String
ULTabName = "yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList"
ULElement = "getElementsByClassName"
LIElement = "getElementbyID"
WMT_HREF = "getElementsByTagName"
Set C360doc = C360Window.document
With C360doc.all
If element = ULElement("ULTabName").LIElement("Work Manager").innerText.WMT_HREF("a")(0) Then
element.Click
Else
MsgBox ("Please ensure Work Manager is open, then try again")
End If
End With
End Sub
第二次尝试:到达带星标的行并退出(尝试了几个变体,结果相同)
FindWorkManager2()
Dim C360doc As HTMLDocument
Set C360doc = C360Window.document
TabTagCounter = 0
TabItemCounter = 0
With C360doc.all
Set Header = C360doc.getElementsByClassName("yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList")
'Find the correct TabTag within Header
For Each TabTag In Header
If TabTag.tagName Like "UL" Then
TabTagCounter = 1
'If we've identified the correct TabTag, then look at the TabItem
'*************Not currently working beyond this point :(
If TabTagCounter = 1 Then
For Each TabItem In TabTag
If TabItem.innerText Like "*Work Manager*" Then
TabItemCounter = 1
End If
MsgBox ("try again")
Next
Else
End If
End If
Next
End With
End Sub
我认为您应该尝试单击 <a>
标签,因为它是一个 link 导航至 #Tab2
。如果 class yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList
是唯一的,您可以先使用此 class 名称找到 ul
,然后使用 li
和 a
找到标签名称。
由于无法访问真正的link,所以我做了一个如下的测试页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div class="tStrCntr">
<ul class="yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList">
<li tabindex="-1"title="Work Manager"></li>
<li tabindex="0" title="Home" id="Tab2">
<a tabindex="-1" id="TABANCHOR" href="https://www.google.com">
<span style="display: inline-block">
<label>Work Manager</label>
</span>
</a>
</li>
</ul>
</div>
</body>
</html>
而测试页面中点击Work Manager的VB代码如下,大家可以测试一下:
更新版本:
Sub LOADIE()
Dim link As Object
Set ieA = CreateObject("InternetExplorer.Application")
ieA.Visible = True
ieA.navigate "http://the test web page"
Do Until ieA.readyState = 4
DoEvents
Loop
Set doc = ieA.Document
Set ElementCol = doc.getElementsByClassName("yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList")(0).getElementsByTagName("li")
For Each link In ElementCol
If link.getAttribute("title") = "Work Manager" Then
link.getElementsByTagName("a").Item(0).Click
End If
Next link
End Sub
首先,如有错误请见谅,我是第一次发帖(虽然我是常客!
我正在研究一个项目来提高我工作组的工作效率。我正在尝试 link 一个 Access 数据库到一个非常复杂的内部 Web 应用程序(基于 Pega)。不幸的是,我的编码知识非常基础,而且我没有可用的资金或带宽来获得正式的帮助。虽然我很想使用加载项,但我不能,因为我的公司不允许这样做(我已经询问、恳求、恳求等)。
This 就是程序页面的样子。我首先需要单击以绿色阻塞的工作管理器选项卡 (WMT),然后我需要单击以红色阻塞的获取下一步按钮 (NGN)。 我认为需要采取的步骤
- 绑定正确的 IE window 会话
- 点击 WMT
- 点击 NGN
我已经能够使用Access绑定正确的window和IE的标签页(感谢为此提供基本代码的人!)但还不能破译如何定位页面上的正确元素。就我的知识而言,命名结构有点太多了。
This 是 WMT 的 HTML 的屏幕截图(它不会复制它,至少不清晰,即使在清理之后)。我希望能够使用特定元素的 ID,但是,它是动态的,并且会随着每次交互而变化,这就是为什么我一直试图以静态部分为目标,以绿色突出显示。
我没有对第二部分做太多工作,但是,This 是它的屏幕截图,按钮以黄色突出显示,如果有人也想帮忙的话。
谢谢!
WINDOW 绑定的代码(到目前为止一直有效!)
Sub C360WindowFind()
marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next
my_url = objShell.Windows(x).document.Location
my_title = objShell.Windows(x).document.Title
If my_title Like "Coverage User" & "*" Then
Set C360Window = objShell.Windows(x)
marker = 1
Exit For
Else
End If
Next
If marker = 0 Then
MsgBox ("C360 window is not found. Please ensure C360 is open in Internet Explorer and try again")
Else
'DO THE OTHER THINGS
End If
End Sub
CODE I'VE TRIED TO TARGET CORRECT TAB(添加以代替先前代码集中的 "DO THE OTHER THINGS")
首先尝试(做一些疯狂的事情,最终导致程序退出)
FindWorkManager1()
Dim C360doc As HTMLDocument
Dim Element
Dim ULTabName As String
Dim LIElement As String
Dim WMT_HREF As String
ULTabName = "yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList"
ULElement = "getElementsByClassName"
LIElement = "getElementbyID"
WMT_HREF = "getElementsByTagName"
Set C360doc = C360Window.document
With C360doc.all
If element = ULElement("ULTabName").LIElement("Work Manager").innerText.WMT_HREF("a")(0) Then
element.Click
Else
MsgBox ("Please ensure Work Manager is open, then try again")
End If
End With
End Sub
第二次尝试:到达带星标的行并退出(尝试了几个变体,结果相同)
FindWorkManager2()
Dim C360doc As HTMLDocument
Set C360doc = C360Window.document
TabTagCounter = 0
TabItemCounter = 0
With C360doc.all
Set Header = C360doc.getElementsByClassName("yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList")
'Find the correct TabTag within Header
For Each TabTag In Header
If TabTag.tagName Like "UL" Then
TabTagCounter = 1
'If we've identified the correct TabTag, then look at the TabItem
'*************Not currently working beyond this point :(
If TabTagCounter = 1 Then
For Each TabItem In TabTag
If TabItem.innerText Like "*Work Manager*" Then
TabItemCounter = 1
End If
MsgBox ("try again")
Next
Else
End If
End If
Next
End With
End Sub
我认为您应该尝试单击 <a>
标签,因为它是一个 link 导航至 #Tab2
。如果 class yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList
是唯一的,您可以先使用此 class 名称找到 ul
,然后使用 li
和 a
找到标签名称。
由于无法访问真正的link,所以我做了一个如下的测试页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div class="tStrCntr">
<ul class="yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList">
<li tabindex="-1"title="Work Manager"></li>
<li tabindex="0" title="Home" id="Tab2">
<a tabindex="-1" id="TABANCHOR" href="https://www.google.com">
<span style="display: inline-block">
<label>Work Manager</label>
</span>
</a>
</li>
</ul>
</div>
</body>
</html>
而测试页面中点击Work Manager的VB代码如下,大家可以测试一下:
更新版本:
Sub LOADIE()
Dim link As Object
Set ieA = CreateObject("InternetExplorer.Application")
ieA.Visible = True
ieA.navigate "http://the test web page"
Do Until ieA.readyState = 4
DoEvents
Loop
Set doc = ieA.Document
Set ElementCol = doc.getElementsByClassName("yui-nav tab-ul tab-ul-t tab-ul-t-ns subTabsList")(0).getElementsByTagName("li")
For Each link In ElementCol
If link.getAttribute("title") = "Work Manager" Then
link.getElementsByTagName("a").Item(0).Click
End If
Next link
End Sub