Python pyautogui.locateOnScreen() Examples

The following are 5 code examples of pyautogui.locateOnScreen(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyautogui , or try the search function .
Example #1
Source File: gmail_generator.py    From gmail-generator with MIT License 7 votes vote down vote up
def open_firefox():

    # Printing basic message
    msg(1,'Opening Firefox...')

    # Location the start button
    _start_button_=pyautogui.locateOnScreen('images/start_button.png')
    _location_=pyautogui.center(_start_button_)

    # Clicking the start button
    if not  pyautogui.click(_location_):
        msg(1,'Opened start menu successfully!')
    else:
        msg(3,'Failed to open start menu!')
        ext()

    time.sleep(2)

    # Search for Firefox in the menu search
    pyautogui.typewrite('firefox')
    pyautogui.typewrite('\n')
    
    # Print message
    msg(1,'Firefox is now open and running.')


# Function used to locate GMail 
Example #2
Source File: gmail_generator.py    From gmail-generator with MIT License 6 votes vote down vote up
def locate_gmail():
    
    #Sleep for a while and wait for Firefox to open
    time.sleep(3)

    # Printing message
    msg(1,'Opening Gmail...')

    # Typing the website on the browser
    pyautogui.keyDown('ctrlleft');  pyautogui.typewrite('a'); pyautogui.keyUp('ctrlleft')
    pyautogui.typewrite('https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ltmpl=default')
    pyautogui.typewrite('\n')
    
    # Wait for a while until the website responds
    time.sleep(6)

    # Print a simple message
    msg(1,'Locating the form...')

    # Locate the form
    pyautogui.press('tab')
 
    time.sleep(2)

    _gmail_ = pyautogui.locateOnScreen('images/gmail_form.png')
    formx, formy = pyautogui.center(_gmail_)
    pyautogui.click(formx, formy)
    
    # Check and print message
    if not pyautogui.click(formx, formy):
        msg(1,'Located the form.')
    else:
        msg(3,'Failed to locate the form.')
        ext()


# Function used to randomize credentials 
Example #3
Source File: im_bot.py    From automate-the-boring-stuff-projects with MIT License 6 votes vote down vote up
def auto_message(name, message):
    """Searches for friend on Google Hangouts and messages them."""
    print("Make sure the Google Hangout 'Conversations' page is visible and "
          "your cursor is not currently on the page.")
    time.sleep(3)

    search_bar = pyautogui.locateOnScreen('search.png')
    pyautogui.click(search_bar)
    pyautogui.typewrite(name)
    time.sleep(1)

    online_select = pyautogui.locateOnScreen('online-friend.png')
    if online_select is None:
        print('Friend not found or currently offline.')
        return
    else:
        pyautogui.doubleClick(online_select)

    attempts = 3
    while attempts > 0:
        message_box = pyautogui.locateOnScreen('message.png')
        pyautogui.click(message_box)
        pyautogui.typewrite(message)

        # If it can no longer be found it is because the message was entered.
        if pyautogui.locateOnScreen('message.png') is None:
            pyautogui.press('enter')
            pyautogui.press('esc')
            print('Message sent to {}'.format(name))
            break
        else:
            if attempts == 1:
                print('Unable to send message to {}.'.format(name))
                pyautogui.press('esc')

            else:
                print('Sending message to {} failed. Another {} attempts will '
                      'be made before moving on.'.format(name, attempts))

            attempts -= 1 
Example #4
Source File: slack_messenger.py    From automate-the-boring-stuff-projects with MIT License 5 votes vote down vote up
def send_message(contact, message):
    """Sends message to an active slack contact
    Args:
        contact (str): contacts name on slack
        message (str): message to send to friend
    Returns:
        None
    """
    try:
        print('5 seconds to navigate to slack app..')
        time.sleep(5)

        # Use JumpTo slack feature
        pyautogui.hotkey('command', 'k')
        time.sleep(1)
        # Enter contact name in search box, click enter
        pyautogui.typewrite(contact)
        time.sleep(1)
        pyautogui.typewrite(['enter'])
        time.sleep(1)

        active = pyautogui.locateOnScreen('active_identifier.png')
        
        if not active:
            print(f'{contact} is not active, skipped contact')
            return
        
        print('Contact is active, sending message...')
        pyautogui.typewrite(['tab'])      
        pyautogui.typewrite(message)
        pyautogui.typewrite(['enter'])

    except KeyboardInterrupt:
        print('Process was cancelled..') 
Example #5
Source File: test_pyautogui.py    From pyautogui with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def test_locateFunctions(self):
        # TODO - for now, we only test that the "return None" and "raise pyautogui.ImageNotFoundException" is raised.

        pyautogui.useImageNotFoundException()
        with self.assertRaises(pyautogui.ImageNotFoundException):
            pyautogui.locate("100x100blueimage.png", "100x100redimage.png")
        # Commenting out the locateAll*() functions because they return generators, even if the image can't be found. Should they instead raise an exception? This is a question for pyscreeze's design.
        # with self.assertRaises(pyautogui.ImageNotFoundException):
        #    pyautogui.locateAll('100x100blueimage.png', '100x100redimage.png')

        # with self.assertRaises(pyautogui.ImageNotFoundException):
        #    pyautogui.locateAllOnScreen('100x100blueimage.png') # NOTE: This test fails if there is a blue square visible on the screen.
        with self.assertRaises(pyautogui.ImageNotFoundException):
            pyautogui.locateOnScreen(
                "100x100blueimage.png"
            )  # NOTE: This test fails if there is a blue square visible on the screen.
        with self.assertRaises(pyautogui.ImageNotFoundException):
            pyautogui.locateCenterOnScreen(
                "100x100blueimage.png"
            )  # NOTE: This test fails if there is a blue square visible on the screen.

        pyautogui.useImageNotFoundException(False)
        self.assertEqual(pyautogui.locate("100x100blueimage.png", "100x100redimage.png"), None)
        # self.assertEqual(pyautogui.locateAll('100x100blueimage.png', '100x100redimage.png'), None)
        # self.assertEqual(pyautogui.locateAllOnScreen('100x100blueimage.png'), None) # NOTE: This test fails if there is a blue square visible on the screen.
        self.assertEqual(
            pyautogui.locateOnScreen("100x100blueimage.png"), None
        )  # NOTE: This test fails if there is a blue square visible on the screen.
        self.assertEqual(
            pyautogui.locateCenterOnScreen("100x100blueimage.png"), None
        )  # NOTE: This test fails if there is a blue square visible on the screen.