2

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")
cookiemnstr247
  • 121
  • 3
  • 14
  • 1
    You say "due to the error message", but failed to quote the error message - don't you think that might be an important detail? (I suspect you just need a bit of a delay between sending the Ctrl-C and checking the clipboard, to give the other program a chance to actually do anything.) – jasonharper Mar 13 '19 at 15:03
  • Great point, Im updating the entry to include the error message. Thanks! – cookiemnstr247 Mar 13 '19 at 15:57
  • 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: https://stackoverflow.com/a/51505977/7496549) – Minion Jim Mar 27 '20 at 17:57

3 Answers3

0

According to @TerryJanReedy ,The error says that There is nothing in the clipboard, not even an empty string.
So trying appending something into it first.
Try:

From time import sleep
from tkinter import Tk
try:
    r=Tk()
    r.clipboard_clear()
    r.clipboard_append('testing ')
    result = r.selection_get(selection="CLIPBOARD")
    sleep(1)
except:
    selection = None
Mr.Dark
  • 42
  • 1
  • 15
0

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.

Minion Jim
  • 1,239
  • 13
  • 30
0

Hey I guess the main reason is the typo you have done: You used 'Ctrl' i.e Control with capital C, which is sometimes accepted due to error handling mechanism but no always. Try this:

pyautogui.hotkey('ctrl','c')

Or use the backend method of hot keys which is

pyautogui.keyDown('ctrl')
pyautogui.press('c')
pyautogui.keyUp('ctrl')

And use some more sleep() time before calling this, this maybe because the program might be running faster (or slower for that matter) its better to be safe than sorry. Good luck

Hope this helps

Aroo
  • 55
  • 1
  • 10