Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move to searched text on active screen with pyautogui

I am trying to make a program that searches for a text on a web-page, then places the mouse cursor on the highlighted text after it has been found. Is this possible using pyautogui? If so, how. If not, are there any other alternatives to do this?

Example code below:

import webbrowser
import pyautogui


var = 'Filtered Questions'
webbrowser.open('https://stackexchange.com/')
time.sleep(2)

pyautogui.hotkey('ctrl', 'f')
pyautogui.typewrite(var)
#code to place mouse cursor to the occurrence of var

I would prefer to not use the pyautogui.moveTo() or pyautogui.moveRel() because the text I am searching for on the website is not static. The position of the searched text varies when the web page loads. Any help would be highly appreciated.

like image 654
ElectroMotiveHorse Avatar asked Mar 04 '18 22:03

ElectroMotiveHorse


People also ask

How do you search text on screen in Python?

ImageGrab is a Python module that helps to capture the contents of the screen. PyTesseract is an Optical Character Recognition(OCR) tool for Python. Together they can be used to read the contents of a section of the screen.

How do you get to the center of the screen in PyAutoGUI?

You can directly find the center of the image using the built in locateCenterOnScreen method. Returns (x, y) coordinates of the center of the first found instance of the image on the screen.

Does PyAutoGUI work with multiple monitors?

Q: Does PyAutoGUI work on multi-monitor setups. A: No, right now PyAutoGUI only handles the primary monitor.

How do you press Alt F4 in PyAutoGUI?

Pressing Shortcuts A combination of keys can also be passed from keyboard using PYAUTOGUI module. To press 'Alt+F4', we first press and hold 'Alt' key then press 'F4' key. After releasing 'F4' key we release 'Alt' key.


1 Answers

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:

  1. Open the page
  2. Open and perform the search (ctrl+f with pyautogui) - the view changes to the first result
  3. Take a screenshot (with PIL)
  4. Convert the image to text and data (with Tesseract) and find the text and the position
  5. 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.

like image 171
Ulrich Avatar answered Sep 24 '22 00:09

Ulrich