I found the answer!
It wasn't selecting text for me as long as num lock was enabled.
Make sure num lock is disabled when using pyautogui
I found the answer!
It wasn't selecting text for me as long as num lock was enabled.
Make sure num lock is disabled when using pyautogui
You do not use click twice. Here is an example on how to do it from the documentation:
pyautogui.doubleClick()
PS: I tkink you wanted to do left click...
Can't select text
Cannot click-and-drag to select text
python 3.x - Move to searched text on active screen with pyautogui - Stack Overflow
python - Copy highlighted text to clipboard, then use the clipboard to append it to a list - Stack Overflow
I've tried with .hotkey('shift','end') and using keyDown + press combinations to do the same.
It does the 'end' and put the cursor at the end of the sentence but, does not hold the shift. Hence, no text is selected.
I've tried shift otherwise, like .hotkey('shift', 'p') and that does give me a capital p.
When you use Chrome or Chromium as a browser there is a much easier and much more stable approach using ONLY pyautogui:
Perform Crtl + F with pyautogui
Perform Ctrl + Enter to 'click' on search result / open the link related to the result
With other browsers you have to clarify if there keyboard shortcuts also exists.
Yes, you can do that, but you additionally need Tesseract (and the Python-module pytesseract) for text recognition and PIL for taking screenshots.
Then perform the following steps:
- Open the page
- Open and perform the search (ctrl+f with pyautogui) - the view changes to the first result
- Take a screenshot (with PIL)
- Convert the image to text and data (with Tesseract) and find the text and the position
- Use pyautogui to move the mouse and click on it
Here is the needed code for getting the image and the related data:
import time
from PIL import ImageGrab # screenshot
import pytesseract
from pytesseract import Output
pytesseract.pytesseract.tesseract_cmd = (r"C:\...\AppData\Local\Programs\Tesseract-OCR\tesseract") # needed for Windows as OS
screen = ImageGrab.grab() # screenshot
cap = screen.convert('L') # make grayscale
data=pytesseract.image_to_boxes(cap,output_type=Output.DICT)
print(data)
In data you find all required information you need to move the mouse and click on the text.
The downside of this approach is the ressource consuming OCR part which takes a few seconds on slower machines.
The keyboard combo Ctrl+C handles copying what is highlighted in most apps, and should work fine for you. This part is easy with pyautogui. For getting the clipboard contents programmatically, as others have mentioned, you could implement it using ctypes, pywin32, or other libraries. Here I've chosen pyperclip:
import pyautogui as pya
import pyperclip # handy cross-platform clipboard text handler
import time
def copy_clipboard():
pya.hotkey('ctrl', 'c')
time.sleep(.01) # ctrl-c is usually very fast but your program may execute faster
return pyperclip.paste()
# double clicks on a position of the cursor
pya.doubleClick(pya.position())
list = []
var = copy_clipboard()
list.append(var)
print(list)
Example using tkinter:
from tkinter import Tk
import pyautogui as pya
def copy_clipboard():
root = Tk() # Initialize tkinter
root.withdraw() # hide the tkinter window
pya.hotkey("ctrl", "c") # copy the text (simulating key strokes)
clipboard = root.clipboard_get() # get the text from the clipboard
return clipboard
copy_text = copy_clipboard()
print(copy_text)
Tk().clipboard_get() returns the current text in the clipboard.
Use the PyAutoGui module.
pip install PyAutoGUI
We can easily use HotKey combinations.
- See docs: https://pyautogui.readthedocs.io/en/latest/keyboard.html#the-hotkey-function
Pressing Ctrl+C
>>> import pyautogui
>>> pyautogui.hotkey('ctrl', 'c')
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
How can I select text and copy it to the clipboard?
The selection part actually works. Either with "ctrl+a" or a triple left mouseclick. But I can't "ctrl+c" it into the clipboard.
I tried it with pyautogui like a normal user would, but it doesn't work.
pyautogui.keyDown('ctrl')
pyautogui.keyDown('c')
pyautogui.keyUp('c')
pyautogui.keyUp('ctrl')
Pyperclip works to read out the content of the clipboard, but it seems like it can only copy strings to the clipbard, that need to be added manually like this:
Pyperclip.copy('test')
But I need to copy my text selection to the clipboard.
edit: if you know any other way to store selected text in a variable, that would be alright too