简体   繁体   中英

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!

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

Use the PyAutoGui module.

pip install PyAutoGUI

We can easily use HotKey combinations.

Pressing Ctrl+C

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM