Todd Lewden
Todd Lewden

Reputation: 309

Python, Pyautogui, and CTRL-C

I am attempting to complete a simple process of opening a web/browser based document, selecting a field within said document, and then copying it so that it goes into my operating system's clipboard. Here's the specs :

Windows 7 Google Chrome ( latest stable ) Python 3.5 pyautogui for keyboard/mouse control

Here is the field I am trying to work with ( http://screencast.com/t/jt0kTagb ). When that little arrow is clicked it pops open to reveal a calendar to pick a date. If you click directly in the field instead it highlights the field's contents. When I manually press CTRL+C in this situation the field's contents go right into the clipboard as expected.

I've tried two methods of getting the field to go into my clipboard. The first was leveraging pyautogui's keyDown/up and press functions which essentially looked like :

imageCoord = noClick("img/date.png")
x, y = pyautogui.center(imageCoord)
pyautogui.click(x, y + 20)
pyautogui.keyDown('ctrl')
pyautogui.press('c')
pyautogui.keyUp('ctrl')

I then attempted to just use the app menu that appears if you right click on something which looked like this:

imageCoord = noClick("img/date.png")
x, y = pyautogui.center(imageCoord)
pyautogui.click(x, y + 20, button='right')
pyautogui.press("down", presses=2)
time.sleep(1)
pyautogui.press('enter')

Lastly I tried the pyautogui.hotkey() function which looked like this :

imageCoord = noClick("img/date.png")
x, y = pyautogui.center(imageCoord)
pyautogui.click(x, y + 20, button='right')
pyautogui.hotKey('ctrl', 'c')

In all three events the field is indeed selected and as best as I can tell the keypresses are going through as all other presses/functions that happen prior go off without a hitch.

The problem that I am facing is that when I do this manually in the same fashion as both of those scripts above I am able to get the contents. When I use the scripts, the clipboard is never updated/populated with the field's contents. Is there something I am overlooking or not considering when working with Python and Window's clipboard?

In the end all I am trying to do is put that value into an excel sheet. Any advice would be appreciated!

Upvotes: 9

Views: 24105

Answers (5)

a struggler
a struggler

Reputation: 11

This seems like an old issue, but here is one option: I tried multiple commands to achieve Command+c in my program, but it kept making an error keyboard sound each time it got to the command+c action. Therefore, to solve it, I had to step by step compile and see what caused the problem. The problem turns out that each time the code has pyautogui.press('down') before command+c, it wouldn't work. Henceforth, I replaced the 'down' key with 'enter' as they are the same in my workflow, which has resulted in command+c no longer containing bugs.

Upvotes: 1

Moinul Islam Raj
Moinul Islam Raj

Reputation: 11

we can use the hotkey function to press ctrl + c

import pyautogui as pg

pg.hotKey("ctrl", "c")

Upvotes: 1

AlexRE
AlexRE

Reputation: 1

I found the solution!

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

In my script I had to use root.update() after.

Upvotes: 0

Ali Sajjad Rizavi
Ali Sajjad Rizavi

Reputation: 4480

Use the PyAutoGui module.

pip install PyAutoGUI

We can easily use HotKey combinations.

Pressing Ctrl+C

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

Upvotes: 3

awpelican
awpelican

Reputation: 98

I have also discovered this issue on a different automation script, and have been working on troubleshooting it for several days. I'm also on Python 3.5 and Windows 7. I can rule out that it has anything to do with Google Chrome, as my particular script is actually working with SAP.

The documentation for pyautogui on Read the Docs (https://pyautogui.readthedocs.io/en/latest/cheatsheet.html#keyboard-functions) gives a direct example of using Ctrl + C to copy text to the clipboard, so I can verify you're not actually doing something wrong. I believe you're just looking at a bug here.

I have opened an issue on the project's GitHub page: https://github.com/asweigart/pyautogui/issues/102

Upvotes: 2

Related Questions