-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinterbot.py
88 lines (76 loc) · 3.22 KB
/
printerbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from login_info import username, password
import time
import pyautogui
class PrinterBot():
def __init__(self):
self.driver = webdriver.Chrome()
def login(self):
# Navigate to 8SAM Login page
self.driver.get(
'https://samadmin.azurewebsites.net/login.aspx')
# Select the username field and type in username
username_in = self.driver.find_element_by_xpath(
'//*[@id="txtLoginID"]')
username_in.send_keys(username)
# Select the password field and type in password
password_in = self.driver.find_element_by_xpath(
'//*[@id="txtPassword"]')
password_in.send_keys(password)
# Click login button
login_btn = self.driver.find_element_by_xpath('//*[@id="btnLogin"]')
login_btn.click()
def nav_to_print(self):
# Click on "Worksheets" button
worksheet_btn = self.driver.find_element_by_xpath(
'//*[@id="lnk1"]/p/img')
worksheet_btn.click()
# Click on "Print Worksheets" button
print_worksheet_btn = self.driver.find_element_by_xpath(
'//*[@id="AIcolumns_NaviContainer"]/div/ul/li[2]/a[2]')
print_worksheet_btn.click()
def keyboard_shortcut_print(self):
# Hold 'command' and 'option' key
pyautogui.keyDown('command')
pyautogui.keyDown('option')
# Press and release 'P' key
pyautogui.press('p')
# Release 'command' and 'option' key
pyautogui.keyUp('command')
pyautogui.keyUp('option')
def print_all(self):
# Start function once base window is closed
base_window = self.driver.window_handles[0]
while True:
if self.driver.window_handles[0] != base_window:
windows_length = len(self.driver.window_handles)
# Iterate through pdf windows and runs print command for x number of windows
for x in range(windows_length):
# print(self.driver.window_handles)
self.driver.switch_to_window(self.driver.window_handles[0])
time.sleep(3)
# Open print dialogue
# Hold 'command' and 'option' key
pyautogui.keyDown('command')
pyautogui.keyDown('option')
pyautogui.press('p')
pyautogui.keyUp('command')
pyautogui.keyUp('option')
time.sleep(3)
pyautogui.press('enter')
# pyautogui.click(print_btn_location)
# Wait for print job to send?
print('Successfully printed a PDF from window ' +
self.driver.window_handles[0])
time.sleep(3)
print('Wait 3 seconds completed')
# Close current pdf after printing
self.driver.close()
# .close() removes the window from the window_handles array
print('Successfully printed all PDFs!')
break
new_bot = PrinterBot()
new_bot.login()
new_bot.nav_to_print()
new_bot.print_all()