java - AHK 使用 getElementByID 查找/选择元素

标签 java html autohotkey

我一直在试图让脚本自动按下按钮。我可以向你保证,对于一个完整的新手来说,这不是一件容易的事。

这是所述按钮的html代码:

<div class"rightButtonSection">
<button name="PierPropertiesContainer_componentnext_0" title="Next Page" class="button buttonLink" onclick"setKeys(event);__xee72onclick(this);" type="button">Next</button>
</div>

这深深地嵌入在代码中,在一个特定的框架内(我认为这是正确的术语)。

this link在另一个线程中提供的包含我需要的脚本,但在我的情况下,我无法实现它。

我的尝试做到了这一点:
+q::

IEGet(name="") {
   IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame     ;// Get active window if no parameter
   Name := (Name="New Tab - Windows Internet Explorer")? "about:Tabs":RegExReplace(Name, " - (Windows|Microsoft)? ?Internet Explorer$")
   for wb in ComObjCreate("Shell.Application").Windows()
      if wb.LocationName=Name and InStr(wb.FullName, "iexplore.exe")
         return wb
}

wb := IEGet("Webtop - Internet Explorer")
wb.Visible := true
wb.document.getElementByID("button").click() ;// is button the ID? try the name or a different selector

return

运行代码后,我在...脚本...运行...查看..窗口中得到了这个:
001: Return (19.94)
004: if name = 
004: WinGetTitle,name,ahk_class IEFrame
005: name := (Name="New Tab - Windows Internet Explorer")? "about:Tabs":RegExReplace(Name, " - (Windows|Microsoft)? ?Internet Explorer$")
006: For wb, in ComObjCreate("Shell.Application").Windows() (0.11)
007: if wb.LocationName=Name &&  InStr(wb.FullName, "iexplore.exe")  
007: if wb.LocationName=Name &&  InStr(wb.FullName, "iexplore.exe")  
009: } (3.21)

如果我正确理解这一点,我对此表示怀疑,它似乎已经捕获了我的两个正在运行的 IE 实例,但这就是努力所能得到的。

然而!我刚刚在脚本查看窗口的 View 下拉列表中发现了“变量及其内容”选项,它显示:
0[1 of 3]: 0
ErrorLevel[1 of 3]: 0
wb[0 of 0]:  

这有点令人担忧,有点暗示它根本没有找到任何可以放入 wb 的东西。

我不确定如何复制 windowspy 的内容,但最上面的标题窗口框显示:
Webtop - Internet Explorer
ahk_class IEFrame
ahk_exe iexplore.exe

我认为 elementID 实际上是“按钮”,但这也是标记名,我尝试过的按钮名称 =“PierPropertiesContainer_componentnext_0”,我尝试将其更改为 getElementByName,我尝试了许多排列。

我有一种感觉,因为我在工作中使用的系统的一些未知限制正在影响代码的第一部分中的一个步骤,但是到目前为止,我一直在寻找想法。至少可以说超出了我的深度!

我在一些线程中看到我可能不得不尝试创建一个循环来循环遍历元素以找到你想要的那个但是......

对下一步的任何帮助都会很棒!

干杯

编辑1:
有问题的站点是一个带有文档前端和基于 Web 的用户界面的 Oracle 数据库。它在工作时在 Intranet 上运行,并且在没有连接到网络的情况下似乎无法访问更广泛的 Internet。

编辑2:
我已经尝试过更多地弄乱代码。我认为我最初的问题是我把函数放在了错误的地方;一旦它点击“返回 wb”,脚本就会结束。前面有脚本的内容和脚本末尾的函数,它至少运行整个脚本!

“ReadyState =!4”段永远循环,所以我删除了它并将其保留为 while wb.busy,它似乎每次都在运行。

虽然仍然没有点击。所以现在我想知道我是否可以对这个 getElement 业务做些什么来测试它是否正常工作。我已经看到 focus() 被提及,并且我已经尝试过类似的负面结果。我猜这就像标签焦点?

我尝试了不同数字的 button[5] 方法,但遗憾的是这也无效。

我的直觉告诉我 wb.document.getElementByName 是要走的路,因为代码显示“

编辑3:
这是我的代码的当前状态(“重新加载”也派上用场了):
+q::

wb := IEGet("Webtop")  ;// call to IEGet function passing the IE tab name as a parameter to assign the tab to a wb object
wb.Visible := true
while wb.busy or wb.ReadyState != 4  ;// make sure page is fully loaded
   Sleep 150
document.getElementsByTagName("button")[1].focus()
;// is "button" the ID? No. Try the name, or use a different selector
return

IEGet(name="") {  ;//IEGet is a function. It returns a wb object
   IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame     ;// Get active window if no parameter
   Name := (Name="New Tab - Windows Internet Explorer")? "about:Tabs":RegExReplace(Name, " - (Windows|Microsoft)? ?Internet Explorer$")
   for wb in ComObjCreate("Shell.Application").Windows()
      if wb.LocationName=Name and InStr(wb.FullName, "iexplore.exe")
         {
         ;MsgBox % "Found it! (" . wb.FullName . ")"  ;  for debugging (comment out when done)
         return wb
         }
}

f6::
reload
return

编辑 4

在这一点上,我几乎放弃了选择我所追求的元素。似乎是因为该站点正在运行一些超出我理解能力的 JAVA 业务,因此该按钮在表面上不存在,因此 AHK 看不到它。

我得到的解决方案是专注于 AHK 可以看到的一个元素(标题),然后多次按 shift+tab,直到找到我想要按下的按钮。

这里的致命缺陷是,如果移动选项卡/按钮位置发生任何变化,它将不起作用。所以至少可以说有点喜怒无常。我徒劳地进行了像素颜色检查,希望像素位置在每个人的 UI 中保持一致,只是为了有某种故障保护措施来阻止自动化进行疯狂的杀戮。
f6::

wb := IEGet("Webtop")  ;// call to IEGet function passing the IE tab name as a parameter to assign the tab to a wb object
wb.Visible := true
while wb.busy ;or wb.ReadyState != 4  ;// make sure page is fully loaded
   Sleep 150

loop 4
{
    PixelGetColor, color, 145,145
    ;msgbox %color%
    if color <> 0x9D663B
        break

    wb.Document.all(1).focus()

    if A_index = 4 
        {
        send +{tab 7}
        send {enter}
        break
        }

    send +{tab 5}
    send {enter}
    while wb.busy
        sleep 150
}
return

IEGet(name="") {  ;//IEGet is a function. It returns a wb object
   IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame     ;// Get active window if no parameter
   Name := (Name="New Tab - Windows Internet Explorer")? "about:Tabs":RegExReplace(Name, " - (Windows|Microsoft)? ?Internet Explorer$")
   for wb in ComObjCreate("Shell.Application").Windows()
      if wb.LocationName=Name and InStr(wb.FullName, "iexplore.exe")
         {
         ;MsgBox % "Found it! (" . wb.FullName . ")"  ;for debugging (comment out when done)
         return wb
         }
}

最佳答案

根据我的评论,首先,这里是如何使用 IEGet:

+q::

IEGet(name="") {  ;//IEGet is a function. It returns a wb object
   IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame     ;// Get active window if no parameter
   Name := (Name="New Tab - Windows Internet Explorer")? "about:Tabs":RegExReplace(Name, " - (Windows|Microsoft)? ?Internet Explorer$")
   for wb in ComObjCreate("Shell.Application").Windows()
      if wb.LocationName=Name and InStr(wb.FullName, "iexplore.exe")
         {
         MsgBox % "Found it! (" . wb.FullName . ")"  ;  for debugging (comment out when done)
         return wb
         }
}

wb := IEGet("Webtop")  ;// call to IEGet function passing the IE tab name as a parameter to assign the tab to a wb object
wb.Visible := true
while wb.busy or wb.ReadyState != 4  ;// make sure page is fully loaded
   Sleep 150
MsgBox % wb.document.getElementByID("PierPropertiesContainer_componentnext_0").innerHTML ;// or innertext or outerHTML
wb.document.getElementByID("PierPropertiesContainer_componentnext_0").click() ;// is "button" the ID? No. Try the name, or use a different selector

return

请注意,我添加了一个小程序来确保 IE 页面已完成加载(它应该是,对,它在一个打开的选项卡中,但以防万一)。

如果您需要“按钮”,则需要“TagName”选择器和 [枚举]:
wb.document.getElementsByTagName("button")[3].click()  ;// what number button is it?

编辑:在 IEGet 中添加了一个 msgbox 以帮助诊断 OP 出了什么问题

请让我们知道您是如何相处的。 . .

关于java - AHK 使用 getElementByID 查找/选择元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49555300/

相关文章:

javascript - 按下按钮后隐藏弹出窗口。 react Js

javascript - jQuery - 即使单击列表也会触发,但不会触发嵌入其中的复选框

java - 为什么 Java 日历说该月的第一个星期四是第 5 周?

java - 如何保留实现带有注释的接口(interface)的类

html - 为 Internet Explorer 8 中的所有非多选标签设置 css 属性

macros - 如何在没有管理员权限的情况下安装/运行 autohotkey?

autohotkey - 傻瓜键盘 Hook

java - Android 向上导航和操作栏中的按钮

java - 从 InputStream 读取并写入 OutputStream

testing - 无法使用 Autohotkey 找到文本