Skip to content

Instantly share code, notes, and snippets.

@lauromoura
Last active May 31, 2021 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lauromoura/9431072d5c2fcdc40ace5b67c0e7261b to your computer and use it in GitHub Desktop.
Save lauromoura/9431072d5c2fcdc40ace5b67c0e7261b to your computer and use it in GitHub Desktop.
WPE WebDriver demo with lichess
# Copyright (C) 2021 Igalia S.L.
#
# Distributed under terms of the MIT license.
import argparse
import sys
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
def wait_for(condition, timeout=3):
start = time.time()
while time.time() < start + timeout:
if condition():
return
time.sleep(0.5)
raise Exception("Timed out waiting for condition to become true")
def parse_args():
"""Parse command line arguments"""
parser = argparse.ArgumentParser()
parser.add_argument(
"address", help="Address of the remote WebDriver, like 192.168.68.107:8088"
)
parser.add_argument("--browser", default="/usr/bin/cog", help="Path to browser")
return parser.parse_args()
def capabilities(browserPath):
return {
"wpe:browserOptions": {
"binary": browserPath,
"args": ["--automation", "--platform=fdo"],
}
}
def main():
args = parse_args()
try:
print("Starting driver...")
driver = webdriver.Remote(
command_executor=args.address,
desired_capabilities=capabilities(args.browser),
)
print("Driver started")
except Exception as e:
print(e)
sys.exit(1)
print("Loading LiChess")
driver.get("https://lichess.org/analysis")
wrapper = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "cg-wrap"))
)
square_size = wrapper.size["height"] / 8
def click_at(col, line):
col = {
"a": 0,
"b": 1,
"c": 2,
"d": 3,
"e": 4,
"f": 5,
"g": 6,
"h": 7,
}[col]
x = col * square_size + square_size / 2
y = (8 - line) * square_size + square_size / 2
actions = ActionBuilder(driver)
pointer = actions.pointer_action
pointer.move_to(wrapper, x, y)
pointer.click()
actions.perform()
time.sleep(1)
# Select pawn at e2
print("e4")
click_at("e", 2)
click_at("e", 4)
print("e6")
click_at("e", 7)
click_at("e", 5)
print("Qh5")
click_at("d", 1)
click_at("h", 5)
print("Nc6")
click_at("b", 8)
click_at("c", 6)
print("Bc4")
click_at("f", 1)
click_at("c", 4)
print("Nf6??")
click_at("g", 8)
click_at("f", 6)
print("Qxf7#")
click_at("h", 5)
click_at("f", 7)
time.sleep(10)
driver.close()
driver.quit()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment