Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
oliv-k
GitHub Repository: oliv-k/99math-bot
Path: blob/master/bot.py
110 views
1
from time import sleep
2
from selenium import webdriver
3
from selenium.webdriver.common.by import By
4
from selenium.webdriver.support.ui import WebDriverWait
5
from selenium.webdriver.support import expected_conditions as EC
6
7
# 99math's numpad
8
def buttons(button):
9
if button == 1:
10
driver.find_element_by_xpath(
11
'/html/body/div[1]/div/div/div/div[2]/div[2]/div[1]/div[1]/button[1]').click()
12
elif button == 2:
13
driver.find_element_by_xpath(
14
'/html/body/div[1]/div/div/div/div[2]/div[2]/div[1]/div[1]/button[2]').click()
15
elif button == 3:
16
driver.find_element_by_xpath(
17
'/html/body/div[1]/div/div/div/div[2]/div[2]/div[1]/div[1]/button[3]').click()
18
elif button == 4:
19
driver.find_element_by_xpath(
20
'/html/body/div[1]/div/div/div/div[2]/div[2]/div[1]/div[2]/button[1]').click()
21
elif button == 5:
22
driver.find_element_by_xpath(
23
'/html/body/div[1]/div/div/div/div[2]/div[2]/div[1]/div[2]/button[2]').click()
24
elif button == 6:
25
driver.find_element_by_xpath(
26
'/html/body/div[1]/div/div/div/div[2]/div[2]/div[1]/div[2]/button[3]').click()
27
elif button == 7:
28
driver.find_element_by_xpath(
29
'/html/body/div[1]/div/div/div/div[2]/div[2]/div[1]/div[3]/button[1]').click()
30
elif button == 8:
31
driver.find_element_by_xpath(
32
'/html/body/div[1]/div/div/div/div[2]/div[2]/div[1]/div[3]/button[2]').click()
33
elif button == 9:
34
driver.find_element_by_xpath(
35
'/html/body/div[1]/div/div/div/div[2]/div[2]/div[1]/div[3]/button[3]').click()
36
else:
37
driver.find_element_by_xpath(
38
'/html/body/div[1]/div/div/div/div[2]/div[2]/div[1]/div[4]/button[2]').click()
39
40
# does a calculaton based on the symbol
41
def calculate(symbol, num1, num2):
42
if symbol == '-':
43
answer = num1 - num2
44
elif symbol == '+':
45
answer = num1 + num2
46
elif symbol == 'x':
47
answer = num1 * num2
48
else:
49
answer = num1 // num2
50
return answer
51
52
# waits until the answer button is clickable
53
# and also starts the main code
54
def wait():
55
wait_element.until(EC.element_to_be_clickable((
56
By.XPATH, '/html/body/div[1]/div/div/div/div[2]/div[2]/div[2]/button[2]')))
57
sleep(5)
58
main()
59
60
# this part does all the main work
61
def main():
62
while True:
63
# get the operaton and split it to 3 parts
64
try:
65
operation = driver.find_element_by_xpath('/html/body/div[1]/div/div/div/div[2]/div[1]/h2').text
66
except:
67
wait()
68
69
operation = operation.split(' ')
70
71
# call the function to make the calculation
72
answer = calculate(operation[1], int(operation[0]), int(operation[2]))
73
74
# click the buttons basedon the answer
75
for i in range(len(list(answer))):
76
try:
77
buttons(answer[i])
78
except:
79
wait()
80
81
# click on the answer button
82
try:
83
driver.find_element_by_xpath(
84
'/html/body/div[1]/div/div/div/div[2]/div[2]/div[2]/button[2]').click()
85
except:
86
wait()
87
88
# asks the user for game code and username
89
code = input('Enter game code: ')
90
name = input('Enter your name: ')
91
92
# opening the browser
93
driver = webdriver.Firefox()
94
driver.get('http://www.99math.com/join/' + code)
95
96
# browsers wait time
97
wait_element = WebDriverWait(driver, 30)
98
99
# entering the username and joining the game
100
driver.find_element_by_xpath('/html/body/div[1]/div/div/input').send_keys(name)
101
driver.find_element_by_xpath('/html/body/div[1]/div/div/button').click()
102
103
# waits and then starts the main code
104
wait()
105
106