【问题标题】:Find and fill an input field with AutoHotKey使用 AutoHotKey 查找并填充输入字段
【发布时间】:2015-07-18 02:34:07
【问题描述】:
对所有 AutoHotKey 大师的挑战:
给我们一个函数,它将查找并移动光标到输入字段(例如登录名),或者发送输入文本。对于像我这样只是摆弄 AHK 的老而懒惰的黑客来说,它看起来像这样:
FindFillField(*elementid*,*sendtext*,*alt-text*)
elementid 是字段的 HTML id,例如用户名,
其中sendtext 是要填充的文本和
其中alt-text 可以是附加的特定文本,以帮助识别字段。
附加的可选参数总是有助于解决奇怪的情况,让您尽情发挥想象力吧!
对于像我这样的老前辈和任何人来说,这将是创建简单登录宏的福音。
【问题讨论】:
标签:
web-scraping
autohotkey
getelementbyid
【解决方案1】:
您始终可以使用 {TAB} 选项。打开网站并按 TAB 键,直到您到达输入字段并计算您点击它的次数。然后做
发送 {TAB ##}。我使用下面的方法将名字、中间名、姓氏和 2 个其他 id 放入 Web 表单中。变量被输入到创建的 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}
【解决方案2】:
您可以使用以下代码将文本发送到输入字段:
wb.document.getElementById("login-username").value := "myUserName"
wb 是 COM 对象,login-username 是输入字段的 ID,myUserName 是您要输入的内容。
ID的Insted,也可以通过名称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"