【问题标题】:AHK Finding/Selecting Element with getElementByIDAHK 使用 getElementByID 查找/选择元素
【发布时间】:2018-09-08 08:47:18
【问题描述】:

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

这是该按钮的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 实例,但这是努力所能得到的。

但是!我刚刚在脚本查看窗口的视图下拉菜单中发现了“变量及其内容”选项,上面写着:

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 错误。 IEGet 返回您的 wb。我会把它放在一个答案中,让我们知道其余的是否好。 . .

标签: java html autohotkey


【解决方案1】:

根据我的评论,首先,这里是如何使用 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 出了什么问题

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

【讨论】:

  • 恐怕还是无处可去-_- 它永远不会超过第 8 行,返回 wb 我的新理论是我必须在调用 IEGet 时将选项卡标题弄错了。我看不出这是怎么可能的,但它似乎不想运行那部分代码,所以除非函数本身有问题,否则我看不出还有什么可能。我已经尝试了标签名称的各种迭代,但都没有成功,现在我开始怀疑有些犯规,某种 IT 安全以某种方式混淆了事情......
  • 删除程序名称,尝试一下:IEGet("Webtop") 并让我们知道确切的错误措辞。 . . (顺便说一句,你有没有告诉我们网站的网址,如果可以的话,我们也可以试试)?
  • 另外,检查我在 IEGet() 函数中的编辑,并告诉我们你是否得到任何东西(以及究竟是什么)。 . .
  • 嘿 PG,感谢您一直以来的支持 xD 好的,我已经尝试了修改后的代码,并且确实出现了 msgbox,因此 IEGet 函数必须工作。但是,在 Return wb 之后它仍然没有运行任何东西 - 查看运行脚本时我得到以下信息:010: Return,wb (12.26)
  • 进行了一些编辑,距离更近了一点,它正在运行,但仍然找不到这个按钮......上帝该死的dangit
猜你喜欢
  • 2018-09-18
  • 1970-01-01
  • 2016-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-26
  • 2021-01-25
  • 2011-03-06
相关资源
最近更新 更多