How to Automate Lichess.com with Python & Selenium & PyAutoGUI
Hello Everyone, We will create a Chess Automation project with Python & Selenium&PyAutoGUI in this article. We will use the Selenium library with Chrome driver in Python to login to lichess.com and we will also use the PyAutoGUI library to control chess pieces. Before we start, let’s learn the basic definitions related to the project.
Please note that this project for high school students. The students should have some basic knowledge of python programming.
LEARN – PRACTICE – SHARE
Selenium: It is an open-source tool that automates web browsers. It provides a single interface that lets you write test scripts in programming languages like Ruby, Java, NodeJS, PHP, Perl, Python, and C#, among others. Selenium is a tool that helps us to automate web browsers. Selenium is used for testing web applications.
Selenium WebDriver is a browser-controlling library, it supports all major browsers (Firefox, Edge, Chrome, Safari, Opera, etc.) and is available for different programming languages including Python.
ChromeDriver: ChromeDriver is a separate executable that Selenium WebDriver uses to control Chrome.
PyAutoGUI library lets your Python scripts control the mouse and keyboard to automate interactions with other applications. The API is designed to be as simple. PyAutoGUI works on Windows, macOS, and Linux.
Chess Automation with Python & Selenium&PyAutoGUI
Requirements for this project: Make sure you install the following programs on your computer.
- Install python – Download the latest version for your computer. www.python.org/downloads
- Install Pycharm – Download the latest version for your computer www.jetbrains.com/pycharm/download
- Install Selenium and PyAutoGUI Packages by using pip install library.
- Run these pip commands in your terminal:
How the code works:
#1 import libraries from selenium import webdriver import pyautogui import time
#2 initialize the Chrome driver driver = webdriver.Chrome("chromedriver")
#3 Access the lichess website driver.get("https://lichess.org/") #wait 2 seconds time.sleep(2)
#4 Start one minute bullet chess game driver.find_element_by_xpath("/html/body/div[1]/main/div[2]/div[2]/div[1]/div[1]").click() time.sleep(3)
PLEASE NOTE THAT we will inspect lichess.com to identify its HTML elements to find 1-minute chess game XPath fields. #right-click/inspect/copy/then click copy fullXPATH
How to code Scholar’s Mate?
In chess, a scholar’s mate is a four-move checkmate in which you use your white-square bishop and queen in a mating attack targeting the opponent’s f-pawn (f2 if white; f7 if black).
To do this we need the specific chess pieces coordination. To find correct coordination we will use the python code below.
#find the chess peaces coordiantions import pyautogui, sys import time print('Press Ctrl-C to quit.') try: while True: x, y = pyautogui.position() positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4) print(positionStr, end='') time.sleep(2) print('\b' * len(positionStr), end='', flush=True) time.sleep(2) except KeyboardInterrupt: print('\n')
How it works:
- Choose to play White
- Begin the game by moving your king’s pawn forward two squares from E2 to E4.
#pawn e4 pyautogui.moveTo(346, 515) pyautogui.click() pyautogui.click(348, 410) time.sleep(1)
- Move your king’s bishop out 3 squares diagonally from F1 to C4.
#bishop c4 pyautogui.moveTo(404, 580) pyautogui.click() pyautogui.click(241, 415) time.sleep(3)
- Advance your queen 4 squares diagonally from D1 to H5.
#queen f3 pyautogui.moveTo(290, 578) pyautogui.click() pyautogui.click(404, 471) time.sleep(3)
- Capture the king’s bishop’s pawn at F7 with your queen for checkmate.
#queen f7 pyautogui.moveTo(404, 471) pyautogui.click() pyautogui.click(404, 256) time.sleep(5)
FULL CODE:
Open the PyCharm, and then enter the following codes below.
Please note that this program works only on lichess.com with white pieces.
# How to Automate Lichess with Python / Selenium # Scholar's Mate in Chess #import libraries from selenium import webdriver import pyautogui import time # initialize the Chrome driver driver = webdriver.Chrome("chromedriver") # get to lichess website driver.get("https://lichess.org/") time.sleep(2) #Start one minute bullet chess game driver.find_element_by_xpath("/html/body/div[1]/main/div[2]/div[2]/div[1]/div[1]").click() time.sleep(3) #pawn e4 pyautogui.moveTo(346, 515) pyautogui.click() pyautogui.click(348, 410) time.sleep(1) #bishop c4 pyautogui.moveTo(404, 580) pyautogui.click() pyautogui.click(241, 415) time.sleep(3) #queen f3 pyautogui.moveTo(290, 578) pyautogui.click() pyautogui.click(404, 471) time.sleep(3) #queen f7 pyautogui.moveTo(404, 471) pyautogui.click() pyautogui.click(404, 256) time.sleep(5) #check mate # www.mikailalici.com
Result: