使用AutoHotKey查找并填充输入字段

时间:2015-05-07 13:25:09

标签: web-scraping autohotkey getelementbyid

对所有AutoHotKey主人的挑战:

为我们提供一个函数,它将查找并移动光标到输入字段(例如LoginName),或者发送输入文本。对于像我这样只是摆弄AHK的老懒惰的黑客,它看起来像这样:

FindFillField(*elementid*,*sendtext*,*alt-text*)

其中elementid是该字段的HTML ID,例如用户名, 其中sendtext是要填写的文本 其中alt-text可以是附加的特定文本,以帮助识别字段。

另外,可选参数总是有助于完善奇怪的案例,让你的想象力疯狂!

对于像我这样的老玩家,对于任何人来说,这对于创建简单的登录宏来说都是一种祝福。

2 个答案:

答案 0 :(得分:2)

您始终可以使用{TAB}选项。打开网站并按TAB键,直到您到达输入字段并计算您点击它的次数。然后做 发送{TAB ##}。我使用下面的内容将第一个名字,中间名,姓氏和另外两个id放入网络表单中。变量被输入到创建的GUI表单中。

Send {TAB 41}
Send %firstn%
Send {TAB}
Send %middle%
Send {TAB}
Send %lastn%
Send {TAB}
Send %deas%
Send {TAB}
Send %npis%
Send {TAB 3}
Send {N}
Send {TAB 2}
Send {ENTER}

答案 1 :(得分:2)

您可以使用以下代码将文本发送到输入字段:

wb.document.getElementById("login-username").value := "myUserName"

wb是COM对象,login-username是输入字段的ID,myUserName是您想要输入的内容。

Insted of ID,您还可以按名称getElementsByName(...),代码名称getElementsByTagName(...)或班级名称getElementsByClassName(...)查找输入字段。我发现this tutorial很有用。使用Chrome或Firefox查找如何识别输入字段(右键单击并按“检查元素”)。

如果要将光标移动到输入字段,请使用

wb.document.getElementById("login-username").focus()

这是一个完整的示例,使用IE和Stack Overflow登录页面:

; Create IE instance
wb := ComObjCreate("InternetExplorer.Application")
wb.Visible := True
wb.Navigate("https://stackoverflow.com/users/login")
; Wait for page to load:
While wb.Busy or wb.ReadyState != 4
    Sleep, 100
; Press "Log in using Stack Exchange"
wb.document.getElementById("se-signup-legend").click()
While wb.Busy or wb.ReadyState != 4
    Sleep, 100
; EITHER focus on the input field:
wb.document.getElementsByName("email")[0].focus()
; OR send text directly to the field:
wb.document.getElementsByName("email")[0].value := "my@email.com"