AHK使用getElementByID查找/选择元素

时间:2018-03-29 11:50:21

标签: 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实例,但这就是它的努力。

无论其!我刚刚在脚本查看窗口的视图下拉列表中发现了“变量及其内容”选项,它显示为:

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,我尝试了多种排列。

我有一种感觉,因为我在工作中使用的系统的一些未知限制正在影响代码的第一部分,但是到目前为止,我仍然坚持想法。至少可以说我的深度!

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

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

干杯

EDIT1: 有问题的站点是一个Oracle数据库,带有documentum前端和基于Web的用户界面。它正在内部网上运行,并且在没有连接到网络的情况下似乎无法访问更广泛的互联网。

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

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

仍然没有点击。所以现在我想知道是否还有其他任何我可以用这个getElement业务来测试它是否正常工作。我已经看到了提到的焦点(),我尝试了类似的负面结果。我猜这就像tab焦点一样?

我尝试使用不同数字的按钮[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
         }
}

1 个答案:

答案 0 :(得分: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页面已完成加载(它应该是,正确,它在打开的选项卡中,但以防万一)。

如果您需要&#34;按钮&#34;你需要&#34; TagName&#34;选择器和[枚举]:

wb.document.getElementsByTagName("button")[3].click()  ;// what number button is it?

编辑:在IEGet中添加了一个msgbox,以帮助诊断OP的错误

请告诉我们您是如何相处的。 。