AutoHotkey V2 – Click/focus input field and type in open chrome browser

  Kiến thức lập trình

Being new to AHK v2, we asked claude ai to generate some code, as it appears to be trained on AHK V2 based on some AHK expert videos released a couple of weeks ago.

We’re basically trying to click on google’s home page input field and type hello.

We’re trying to target input field based on its id to either click in it to type, or focus it then type, or find it by xpath and then type. The input field’s id is input.

Here is the code, but it errors out with “Error: Target control not found.” when running FocusInputById("input") and ClickInputById("input") when trying them separately (as seen in the last few lines of the script).

#Requires AutoHotkey v2.0

; Try to find the Chrome window
chromeTitle := "ahk_exe chrome.exe"
if (chromeWindow := WinExist(chromeTitle))
{
    MsgBox("Chrome window found. Attempting to activate...")
    WinActivate(chromeWindow)
    if (WinActive(chromeTitle))
    {
        MsgBox("Chrome window activated successfully.")
    }
    else
    {
        MsgBox("Failed to activate Chrome window.")
        ExitApp
    }
}
else
{
    MsgBox("Chrome window not found. Make sure Chrome is open with the correct profile.")
    ExitApp
}

; Function to focus on an input field by ID
FocusInputById(id) {
    try {
        ControlFocus("id" . id, "A")
        MsgBox("Successfully focused on input field with ID: " . id)
    } catch as err {
        MsgBox("Failed to focus on input field with ID: " . id . ". Error: " . err.Message)
    }
    Sleep 500  ; Wait a bit after focusing
}

; Function to focus on an input field by XPath
FocusInputByXPath(xpath) {
    try {
        ControlFocus("x" . xpath, "A")
        MsgBox("Successfully focused on input field with XPath: " . xpath)
    } catch as err {
        MsgBox("Failed to focus on input field with XPath: " . xpath . ". Error: " . err.Message)
    }
    Sleep 500  ; Wait a bit after focusing
}

; Function to click an input field by ID
ClickInputById(id) {
    try {
        ControlClick("id" . id, "A")
        MsgBox("Successfully clicked input field with ID: " . id)
    } catch as err {
        MsgBox("Failed to click input field with ID: " . id . ". Error: " . err.Message)
    }
    Sleep 500  ; Wait a bit after clicking
}

; Function to click an input field by XPath
ClickInputByXPath(xpath) {
    try {
        ControlClick("x" . xpath, "A")
        MsgBox("Successfully clicked input field with XPath: " . xpath)
    } catch as err {
        MsgBox("Failed to click input field with XPath: " . xpath . ". Error: " . err.Message)
    }
    Sleep 500  ; Wait a bit after clicking
}

; Tried these 2 separately
FocusInputById("input") ; Failed to click input field with ID: input. Error: Target control not found.
ClickInputById("input") ; Failed to click input field with ID: input. Error: Target control not found.

; Fill out form field
SendInput "Hello"

Any idea how to get it working?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT