Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue copying to clipboard using tkinter and pyautogui

I am working on an automation program to review/test content within a third party application. At the moment Im going with pyautogui to highlight and copy values(at least until we get access to query the applications database) and tkinter to retrieve data from the clipboard.

The script below has worked in copying content that can be highlighted on the screen (under the #get manager section in the script).

However, when I navigate to a section that has a text body (#QAR test 2), use pyautogui.hotkey("Ctrl","a") to highlight all and pyautogui.hotkey("Ctrl","c") to copy, it seems like the clipboard is not populated (due to the error message when trying to print out the variable it has been assigned to).

For reference, I am attaching a screen shot of the target text. Can text (specifically, paragraphs) not be copied over to the clipboard?

The error message raised after the #QAR Test 2 is:

Traceback (most recent call last):
  File "C:/Users/haudrxr/Downloads/PCA_5_5_18_QAR.py", line 92, in <module>
    background_tm= copy_clipboard()
  File "C:/Users/haudrxr/Downloads/PCA_5_5_18_QAR.py", line 10, in copy_clipboard
    clipboard = Tk().clipboard_get()
  File "C:\Users\haudrxr\AppData\Local\Continuum\anaconda3\lib\tkinter\__init__.py", line 804, in clipboard_get
    return self.tk.call(('clipboard', 'get') + self._options(kw))
_tkinter.TclError: CLIPBOARD selection doesn't exist or form "STRING" not defined

enter image description here

...
#Get Manager Value
x=115
y=450
for i in range (10):
    pyautogui.click(x, y)
    time.sleep(1)
    pyautogui.doubleClick(839, 567)
    pyautogui.hotkey("Ctrl","c")
    level=copy_clipboard()
    y += 23
    if level=="1":
        pyautogui.mouseDown(750, 437,button="left",duration=1)
        pyautogui.dragTo(1049, 437,1, button='left')
        pyautogui.hotkey("Ctrl", "c")
        staffname = copy_clipboard()
        if len(staffname)>1:
            team_tab.append(staffname)
            print(team_tab)
    else:
        continue

    team_tab = list(filter(None, team_tab))  # fastest
    print(len(team_tab))
if len(team_tab)>2:
    print("QAR Item 1: PASS")
else:
    print("QAR Item 1: FAIL")

#QAR Test 2
if windll.user32.OpenClipboard(None):
    windll.user32.EmptyClipboard()
    windll.user32.CloseClipboard()
pyautogui.click(262, 162) # navigates to tab with text box
pyautogui.click(614, 314) #clicks in text box
pyautogui.hotkey("Ctrl", "a")
pyautogui.hotkey("Ctrl", "c")
background_tm= copy_clipboard()
time.sleep(10)
print(background_tm)
print("test1")
like image 947
cookiemnstr247 Avatar asked Dec 28 '25 11:12

cookiemnstr247


1 Answers

As mentioned in my comment, the control-c shortcut does not act immediately:

Try adding a very small pause before trying to access the clipboard data as the keyboard shortcut is not instantaneous. This is noted in a code comment "ctrl-c is usually very fast but your program may execute faster" found in another SO thread)

In this situation, I would move the sleep to before you get the clipboard content (I don't know the context as to why it is there) and reduce it to just 0.1s. The changed code for QAR Test 2 can be seen below:

#QAR Test 2
if windll.user32.OpenClipboard(None):
    windll.user32.EmptyClipboard()
    windll.user32.CloseClipboard()
pyautogui.click(262, 162) # navigates to tab with text box
pyautogui.click(614, 314) #clicks in text box
pyautogui.hotkey("Ctrl", "a")
pyautogui.hotkey("Ctrl", "c")
time.sleep(0.1)
background_tm= copy_clipboard()
print(background_tm)
print("test1")

Note: If this still doesn't work it may be worth looking into a different method for getting the clipboard content as many people (far more experienced than myself!) have reported that it returns None instead of the actual content in some situations.

like image 135
Minion Jim Avatar answered Dec 31 '25 02:12

Minion Jim