Simple selenium crawler application and timed desktop prompt diagram

Time:2021-11-30

Document establishment date: March 11, 2020

Last revision date:

2020 / 03 / 16 (code update – no message is displayed, and the update time with message becomes shorter)

2020 / 03 / 27 (code update – forgot what to change)

2020 / 04 / 14 (the picture uses the pysimplegui internal picture, and the code becomes shorter)

2020 / 04 / 15 (since the title of the window may be different, it will be prompted to modify by itself)

April 21, 2020 (new IE version)

2020 / 06 / 02 (prompt of inconsistency between Chrome version and chromedriver.exe)

2020/06/28 (SystemTray.notify removed, replaced by show_message)

Relevant software information

Win10 Python 3.8.3
PySimpleGUI 4.20.0.11 Selenium 3.141.0
pywin32 227 Google Chrome 83.0.4103.106
ChromeDriver 83.0.4103.39

Note: please quote or change this article at will. Just indicate the source and author. The author does not guarantee that the content is absolutely correct. Please be responsible for any consequences

Title: simple selenium crawler application and timed desktop prompt diagram

There are often some message notifications on learnku that can’t be seen without viewing, so I wrote a desktop notification program

requirement

  1. Using selenium
  2. To protect your account / password, enter login after the program starts
  3. After running, only one icon is left on the desktop. Right click start and end
  4. After logging in, click start to hide the Chrome web page and the chrome driver window
  5. Notification occurs every 5 minutes

Simple selenium crawler application and timed desktop prompt diagram

Code and description

  1. Library loading (if you use pysimpleguiwx instead of pysimplegui, the prompt icon will be incorporated into the work column)
import PySimpleGUI as sg
from PySimpleGUI import DEFAULT_BASE64_ICON as logo
import win32gui
from selenium import webdriver
  1. Message Notification

    The number of messages read is notified once every five minutes, and the number of messages is changed to once every 30 seconds
<span class="ui basic circular label notification" id="notification-count">
                0
            </span>
def notify():
    global timeout
    elements = driver.find_elements_by_id("notification-count")
    if elements != []:
        try:
            count = int(elements[0].text)
        except:
            count = 0
        if count > 0:
            timeout = 30000
            tray.show_message('Notify', f'{count} new messages')
        else:
            timeout = 300000
            tray.show_message('Notify', 'No new messages')
  1. Hide the window. If the title is the same as required, set the window to be hidden

    Note 1: if there is a login error, the hidden window may not be found because the title changes

    Note 2: the title may be different due to different environments. Please check the titles of the two windows and modify them
def enumWindowFunc(hwnd, windowList):
  1. Start the chrome driver, open the web page and read it
try:
    driver = webdriver.Chrome('D:/Python/Project/chromedriver')
except:
    sg.popup('Crome driver load failed, maybe wrong version !')
    quit()
driver.get('https://learnku.com/python')
  1. Create an icon and set the right-click menu toStartandQuit
menu_def = ['My_Menu', ['Start', 'Quit']]
tray = sg.SystemTray(menu=menu_def, data_base64=logo)
  1. Right click event processing and message notification update
timeout = 300000
flag = False
while True:

    event = tray.read(timeout=timeout)

    if event in [None, 'Quit']:
        break

    if flag:
        try:
            driver.refresh()
            notify()
        except:
            pass
    else:
        if event == 'Start':
            win32gui.EnumWindows(enumWindowFunc, [])
            # driver.minimize_window()
            notify()
            flag = True

driver.close()
driver.quit()
tray.close()
  1. Ie version
import PySimpleGUI as sg

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Jason Yang

Recommended Today

Game case | application evolution and practice of service mesh in happy games

author Chen Zhiwei, Tencent level 12 background expert engineer, is now responsible for the public background technology research and development and team management of happy game studio. Rich experience in micro service distributed architecture and game background operation and maintenance research and development. preface The background of happy game studio is a distributed micro service […]