Find and fill an input field with AutoHotKey

*爱你&永不变心* 提交于 2020-01-11 03:23:11

问题


A challenge to all you AutoHotKey masters:

Give us a function that will Find and Move the Cursor to an Input Field (E.g. LoginName) and, alternatively send input text. For the old, lazy hackers like myself just fiddling with AHK, it would look like this:

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

Where elementid is the HTML id for the field, e.g. USERNAME, where sendtext is the text to fill and where alt-text could be additional, specific text to help identify the fields.

Additional, optional parameters are always helpful to round out odd cases so let your imaginations run wild!

For old-timers like me, and for anyone, this would be a blessing in creating simple login macros.


回答1:


You can always use the {TAB} option. Open the website and hit the TAB key until you reach the input field and count how many times you hit it. Then do Send {TAB ##}. I used the below to put in First name, middle name, last name, and 2 other id into a web form. The variables were entered into a GUI form that was created.

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:


You can send text to an input field with the following code:

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

Where wb is the COM object, login-username is the ID of the input field, and myUserName is what you wish to input.

Insted of ID, you can also find the input field by name getElementsByName(...), tag name getElementsByTagName(...) or class name getElementsByClassName(...). I've found this tutorial to be useful. Use Chrome or Firefox to find out how to identify the input field (rightclick and press "inspect element").

If you want to move the cursor to the input field, use

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

Here is a full example, using IE and the Stack Overflow login page:

; 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"


来源:https://stackoverflow.com/questions/30102439/find-and-fill-an-input-field-with-autohotkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!