[Python] pyautogui hotkey working on the first iteration and not after (Mac)

Discussão em 'Python' iniciado por Stack, Maio 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to automate a simple process using pyautogui: select an item with a link on an app window, open the link on my browser, copy the link from the address bar, move on to the next item, and continue. I can't access the links directly from the app without opening it with a browser.

    for idx in range(total_items):
    print(f"Processing item {idx+1}/{total_items}...")
    # Press enter to open the link
    pyautogui.press('enter')
    time.sleep(BROWSER_FOCUS_DELAY)

    # Focus address bar and copy URL
    pyautogui.moveTo(x2, y2, duration=0.2)
    pyautogui.click()
    time.sleep(0.2)
    pyautogui.hotkey('command', 'c', interval=0.1) # Copy selected text
    time.sleep(0.5)
    url = pyperclip.paste()
    print(f" -> {url}")
    links.append(url)

    # Close tab
    pyautogui.hotkey(*BROWSER_TAB_CLOSE_SHORTCUT, interval=0.1)
    time.sleep(0.3)

    # Refocus on app window by clicking at the stored position
    pyautogui.moveTo(x1, y1, duration=0.2)
    pyautogui.click()
    time.sleep(0.3) # Wait for window focus

    # Move down to next item
    pyautogui.press('down')
    time.sleep(0.2)


    The coordinates are preset according to my environment. When I run the code, it opens the first item's link on the browser, copies the address, and closes the tab. It successfully moves to the second item and opens the link as well, focuses on the address bar (I can see the whole address being selected), but fails to copy the address, I just get a beep sound that you get when you try to copy without selecting anything. It also fails to close the tab, just another beep. And it keeps on doing it for the remaining items. I would get it if it fails on the first item, but I just can't figure out why it works for the first iteration and not after. What am I missing here?

    Continue reading...

Compartilhe esta Página