Autohotkey AHK-如何创建与网站交互的脚本';s按钮

Autohotkey AHK-如何创建与网站交互的脚本';s按钮,autohotkey,Autohotkey,我正在找人来帮助我创建一个AHK脚本,将点击一个网站上的一系列按钮 它必须在页面上单击的第一个按钮是: <button class="btn btn-primary pull-right" onclick="document.getElementById('btnCommentAdd').click()">Add comment</button> 添加注释 然后会弹出一个窗口,其中有一个下拉选择窗口,其中需要选择以下内容Har selv sign up,men Har

我正在找人来帮助我创建一个AHK脚本,将点击一个网站上的一系列按钮

它必须在页面上单击的第一个按钮是:

<button class="btn btn-primary pull-right" onclick="document.getElementById('btnCommentAdd').click()">Add comment</button>
添加注释
然后会弹出一个窗口,其中有一个下拉选择窗口,其中需要选择以下内容
Har selv sign up,men Har spg

完整代码:

<select id="inpCommentCategories" class="form-control">
                                    <option value="0">Category</option>
                                <option>Sign up</option>
                                <option>Har selv sign up, men har spg</option>
                                <option>Ikke sign up endnu, men har spg</option>
                                <option>Ombooking af tekniker dato</option>
                                <option>Udenfor området</option>
                                <option>Produkt/Internet spg</option>
                                <option>Gravearbejde</option>
                                <option>Telefonnummer ændring</option>
                                <option>Har ikke en mail adresse</option>
                                <option>Tekniker klage</option>
                                <option>Klage</option>
                                <option>Tekniker (udeblev, forsinket)</option>
                                <option>Tekniker (ønsker kontakt)</option>
                                <option>Nåede ikke sign up</option>
                                <option>Reetablering</option>
                                <option>Sign off</option>
                                <option>Oprettelse af KAP Installation</option>
                                <option>Oprettelse af Site Survey</option>
                                <option>Booket til KAP, fiber ikke skudt ind</option>
                                <option>Fejl</option>
                                <option>Øvrige</option>
                                </select>

类别
注册
加油,加油,加油
Ikke注册endnu,男人和spg
Ombooking af tekniker dato
området酒店
产品/互联网服务
墓穴
Telefonnummerændring
我的邮件地址是
泰克尼克克拉奇
克拉奇
Tekniker(udeblev,forsinket)
Tekniker(ønsker kontakt)
Nåede ikke注册
再生
签字
Oprettelse af KAP安装
Oprettelse af现场调查
布克-蒂尔-卡普,纤维IKE skudt ind
Fejl
Øvrige
然后,作为最后一步,必须单击提交按钮

<button id="btnCommentSubmit" class="btn btn-primary">Submit</button>
提交

我不知道从哪里开始。虽然我非常希望有人能创建一个脚本,我可以复制粘贴,但如果我将来需要做一些更改,它仍然需要一些解释

为了让AutoHotkey直接与网页的HTML交互,必须使用COM文档对象。这使用基本JavaScript语言与各种对象进行接口。但为了让您开始,这里有一个来自我的一个项目的基本工具,欢迎您使用。我重新打包这些方法的唯一原因是为了在某些事情失败时进行自定义事件处理

这将帮助您开始。我有一个使用下面详述的对象导航到网页的直接示例

;   Create a new window and navigate
obj_servnow := ComObjCreate("InternetExplorer.Application") ; Create the object
srv_now_hwnd := IE_Handler.GetHwnd(obj_servnow) ; Store the Window ID so you can do stuff if it closes
IE_Handler.NavToUrl(obj_servnow, navURL) ; Navigate to URL
IE_Handler.WaitNotBusy(obj_servnow) ; Wait for the window to load.
COM很挑剔,如果不小心,它会到处抛出异常

我在《自动热键2.0 Alpha》中写了这篇文章,如果您想使用不同的版本,您必须进行转换,因为大多数函数的语法完全不同

class IE_Handler
{
    ;   PURPOSE
    ;   *   To give a universal Handler method for all the IE windows used in this script.
    ;   *   To enable better/smoother exception handling
    ;   *   Get methods should always return a value.
    GetHwnd(ByRef pwb)
    {
        ;   pwb - OBJECT - Internet Explorer COM object
        Try
        {
            return pwb.hwnd
        }
        Catch
        {
            return false
        }
    }

    GetFieldById(ByRef pwb, s)
    {
        ;   pwb - OBJECT - Internet Explorer COM object
        ;   s - STRING - ID of field that has an appropriate value tag

        ;   No exception is thrown, instead return empty value.
        Try
        {
            return pwb.Document.GetElementById(s).value
        }
        Catch
        {
            return ""
        }
    }

    GetByWindow(winId)
    {
        ;   winId - INT - AHK 2.0 returns int for win IDs.

        ;   Use WinExist or similar function to get the window handle of an IE window.
        ;   This allows the program to connect to a specific window.
        ;   Method is used during startup and ShellMessags
        if (!winId || !RegExMatch(winId, this.hwnd_regex))
        {
            Throw Exception("Unexpected Window ID Passed " . winId)
        }

        for pwb in ComObjCreate("Shell.Application").Windows
        {
            Try
            {
                if InStr(pwb.FullName, "iexplore.exe") && winId == pwb.HWND
                {
                    ;   Return literal object for use
                    return pwb
                }
            }
            Catch
            {
                ;   Catch is total failure to obtain the window.
                ;   Maybe it was closed while the loop was running.
                Throw Exception("COM Error in locating object by window ID " . winId)
            }
        }
        Throw Exception("Unable to locate provided window ID " . winId)
    }

    GetInnerHTMLByClassIndex(ByRef pwb, s, i)
    {
        ;   pwb - OBJECT - Internet Explorer COM object
        ;   s - STRING - Class name of tags to search
        ;   i - INT - Integer for the array given by GetElementsByClassName
        if (i < 0)
        {
            Throw Exception("Unexpected index less than zero: " . i)
        }
        Try
        {
            return pwb.Document.GetElementsByClassName(s)[i].innerHTML
        }
        Catch
        {
            return ""
        }
    }

    GetDOMById(ByRef pwb, s)
    {
        ;   pwb - OBJECT - Internet Explorer COM object
        ;   s - STRING - Class name of tags to search
        ;   Returns object hook for a web page element
        Try
        {
            return pwb.Document.GetElementById(s)
        }
        Catch
        {
            ;   This method is meant to be tested during variable assignment, so return false if unable to interface with the specified element.
            return false
        }
    }

    GetDOMByClassIndex(ByRef pwb, s, i)
    {
        ;   pwb - OBJECT - Internet Explorer COM object
        ;   s - STRING - Class name of tags to search
        ;   i - INT - Integer for the array given by GetElementsByClassName
        if (i < 0)
        {
            Throw Exception("Unexpected index less than zero: " . i)
        }
        Try
        {
            return pwb.Document.GetElementsByClassName(s)[i]
        }
        Catch
        {
            ;   This method is meant to be tested during variable assignment, so return false if unable to interface with the specified element.
            return false
        }
    }

    SetProperty(ByRef pwb, property, val)
    {
        ;   pwb - OBJECT - Internet Explorer COM object
        ;   property - STRING - Name of the property to change of the IE object. (toolbar, menubar, visible, statusbar, etc.)
        ;   val - INT - Range from -1 to 1 (0 is false) (Trinary operator)

        ;   Exclusive to 2.0, strlower and strupper
        ;   https://lexikos.github.io/v2/docs/commands/StrLower.htm
        property := StrLower(property)
        ;   Each of these would be a check for != so the ! can go like !(expression)
        ;   To invert the whole conditional phrase
        if !(property = "toolbar" || property = "visible" || property = "menubar")
        {
            Throw Exception("Unexpected property passed " . property)
        }
        if (val < -1 || val > 1)
        {
            Throw Exception("Unexpected value passed " . val)
        }
        Try
        {
            pwb.%property% := val
            return true
        }
        Catch
        {
            ;   Update over 2.2 - Used to throw exception here.
            ;   Just gonna return false
            return false
        }
    }

    SetInnerHTMLById(ByRef pwb, s, v)
    {
        ;   pwb - OBJECT - Internet Explorer COM object
        ;   s - STRING - Field ID of the desired tag
        ;   v - STRING - Value to set innerHTML

        ;   No exception is thrown, instead return true or false
        Try
        {
            pwb.document.getElementById(s).innerHTML := v
            return true
        }
        Catch
        {
            return false
        }
    }

    SetFieldById(ByRef pwb, s, v)
    {
        ;   pwb - OBJECT - Internet Explorer COM object
        ;   s - STRING - Field ID of the desired tag
        ;   v - STRING - Value to set value (usually html <input>)

        ;   This function can fail if the proper HTML field is not found
        ;   That is why it does not throw an exception
        Try
        {
            pwb.Document.GetElementById(s).value := v
            return true
        }
        Catch
        {
            return false
        }
    }

    CloseHiddenWindows(ignoreWin := false)
    {
        ;   Close invisible windows. Allow option to ignore a specific hwnd if so choosing to.
        Try
        {
            for pwb in ComObjCreate("Shell.Application").Windows
            {
                if (InStr(pwb.FullName, "iexplore.exe") && pwb.Visible = 0 && pwb.hwnd != ignoreWin)
                {
                    pwb.Quit()
                }
            }
        }
        ;   No catch desired here.
    }

    WaitNotBusy(ByRef pwb)
    {
        ;   pwb - OBJECT - Internet Explorer COM object

        ;   ReadyState is a more reliable determination as opposed to IE_Com.Busy property.
        ;   1 = Request sent
        ;   2 = Request received
        ;   3 = Request processing
        ;   4 = Page is done sending/receiving.

        ;   IE_Com.Busy property is either true or false.
        Try
        {
            Loop 100
            {
                Sleep(100)
                if (pwb.ReadyState = 4 || !pwb.Busy)
                {
                    ;   This is a normal exit
                    return true
                }
            }
            ;   Failed to wait all the way
            return false
        }
        Catch
        {
            ;   11/15/2019 - Returning false instead of throwing exception
            ;   Throw Exception("COM ERROR - Unable to check ReadyState for passed object")
            return false
        }
    }

    NavToUrl(ByRef pwb, url)
    {
        ;   pwb - OBJECT - Internet Explorer COM object
        ;   url - STRING - Actual URL, NOT regular expressions
        if !RegExMatch(url, "^https?://*")
        {
            Throw Exception("Invalid URL passed: " . url)
        }
        Try
        {
            pwb.Navigate(url)
        }
        Catch
        {
            Throw Exception("COM ERROR - Unable to call Navigate function")
        }
    }

    Kill(ByRef pwb)
    {
        ;   pwb - OBJECT - Internet Explorer COM object
        Try
        {
            ;   Make the window invisible, then kill it quietly.
            pwb.Visible := 0
            pwb.Quit()
            return true
        }
        Catch
        {
            return false
        }
    }

    Refresh(ByRef pwb)
    {
        ;   pwb - OBJECT - Internet Explorer COM object
        Try
        {
            pwb.Refresh()
            return true
        }
        Catch
        {
            return false
        }
    }
    ;   End of class IE Handler :D
}
类IE\u处理程序
{
;目的
;*为脚本中使用的所有IE窗口提供通用处理程序方法。
;*以实现更好/更平滑的异常处理
;*Get方法应始终返回一个值。
GetHwnd(ByRef pwb)
{
;pwb-对象-Internet Explorer COM对象
尝试
{
返回pwb.hwnd
}
抓住
{
返回错误
}
}
GetFieldById(ByRef pwb,s)
{
;pwb-对象-Internet Explorer COM对象
;s-字符串-具有适当值标记的字段ID
;不会引发异常,而是返回空值。
尝试
{
返回pwb.Document.GetElementById.value
}
抓住
{
返回“”
}
}
GetByWindow(winId)
{
;winId-INT-ahk2.0为win-id返回INT。
;使用WinExist或类似函数获取IE窗口的窗口句柄。
;这允许程序连接到特定窗口。
;方法在启动和关闭过程中使用
if(!winId | |!RegExMatch(winId,this.hwnd|u regex))
{
抛出异常(“传递了意外的窗口ID”.winId)
}
对于ComObjCreate(“Shell.Application”).Windows中的pwb
{
尝试
{
如果InStr(pwb.FullName,“iexplore.exe”)&&winId==pwb.HWND
{
;返回要使用的文本对象
返回pwb
}
}
抓住
{
;Catch是获取窗口的完全失败。
;可能在循环运行时它已关闭。
抛出异常(“按窗口ID.winId定位对象时出现COM错误”)
}
}
抛出异常(“无法找到提供的窗口ID”.winId)
}
GetInnerHtmlByCassindex(ByRef pwb,s,i)
{
;pwb-对象-Internet Explorer COM对象
;s-字符串-要搜索的标记的类名
;i-INT-GetElementsByClassName给定的数组的整数
if(i<0)
{
抛出异常(“意外索引小于零:”.i)
}
尝试
{
返回pwb.Document.GetElementsByCassName[i].innerHTML
}
抓住
{
返回“”
}
}
GetDomainById(ByRef pwb,s)
{
;pwb-对象-Internet Explorer COM对象
;s-字符串-要搜索的标记的类名
;返回网页元素的对象挂钩
尝试
{
返回pwb.Document.GetElementById
}
抓住
{
;此方法将在变量赋值期间进行测试,因此如果无法与指定元素接口,则返回false。
返回错误
}
}
GetDOMByClassIndex(ByRef pwb,s,i)
{
;pwb-对象-Internet Explorer COM对象
;s-字符串-要搜索的标记的类名
;i-INT-GetElementsByClassName给定的数组的整数
if(i<0)
{
抛出异常(“意外索引小于0