0

So, I'm running a script from the Command Prompt with elevated permissions (Python3).

It runs perfectly for a time. But after about 10 minutes, it times out with this error:

Traceback (most recent call last):
  File "C:\Users\xxxxx\Desktop\clicker\ClickerImprovedV3.py", line 200, in <module>
    PyDeepClip1 = pyperclip.paste()
  File "C:\Users\thesu\AppData\Local\Programs\Python\Python39\lib\site-packages\pyperclip\__init__.py", line 490, in paste_windows
    with clipboard(None):
  File "C:\Users\thesu\AppData\Local\Programs\Python\Python39\lib\contextlib.py", line 117, in __enter__
    return next(self.gen)
  File "C:\Users\thesu\AppData\Local\Programs\Python\Python39\lib\site-packages\pyperclip\__init__.py", line 452, in clipboard
    raise PyperclipWindowsException("Error calling OpenClipboard")
pyperclip.PyperclipWindowsException: Error calling OpenClipboard ([WinError 5] Access is denied.)

So, basically, this is a recursive program to automate the boring stuff. I copy from 1 spot, paste to another online. It spits out its data. I grab that data, copy it, go back to the spreadsheet, then paste it into there.

As I said before, the program runs perfectly fine.

It seems to me that, after a certain time (I'm not exactly sure how much, because the program runs without me there :-) that somehow the clipboard locks... or empties?! I thought that running this with Admin privilages would solve this from looking at other answers.

A couple of things that may or may not have anything to do with this.

  1. When this program was at its simplest, this didn't happen. Still using pyautogui and pyperclip.
  2. I made a lot of changes in the code to catch errors as they've been popping up. This error seems to be ONLY after I started using pyperclip.paste() to catch a variable and make sure that the clipboard pasting INTO the website, and the clipboard pasting FROM the website were different.
  3. Before this error started showing, I was just using
pg.hotkey('ctrl', 'a')
pg.hotkey('ctrl', 'v')

and it would run for hours, ableit, with some problems. Which is why I changed it.

  1. Is there something that I need to be doing to flush the clipboard? Am I making too many calls to it somehow?

1 Answers1

0

So, after posting this, I changed a few things, but to no avail.

THEN! I believe that I have solved the problem now.

So, after changing and testing, I started thinging about how the problem basically came after I chose to "read from" the clipboard and make it a variable for comparison. Before I was just using LocateCenterOnScreen to find something and click on it. Then I would just use the hotkey for ctrl+c or ctrl+v and move on.

So I changed things back to what was originally posted here. What seems to have fixed the problem for me is that, after copying (whatever method you are choosing), you need to have about half a second waiting time included. Don't do anything. I think that, as system resources are being used for something else (checking your email program, checking for updates in the background... WHATEVER), the 'copy' to the clipboard takes just a tad longer than usual. The error comes because, as you are trying to look at the clipboard to set a variable, the system is actually busy with the 'copy' function still. And so, while the system is still doing this, you get the 'Access is denied' error. Make sense?

I will report back if this seems to be false eventually. But so far, I had the problem that, at least once within a 10-15 minutes time perĂ­od, this script would freak out and throw the error above. After adding a line to wait half a second in between the 'copy' and then reading the clipboard to set a variable, this script has been running for at least 25 minutes with no errors thrown.

I hope that this helps. It seems that there are quite a few questions around this error/topic. I hope that y'all find this helpful!