forked from krishp0130/Liquor-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_script.py
More file actions
1226 lines (1092 loc) · 56.6 KB
/
Copy pathbot_script.py
File metadata and controls
1226 lines (1092 loc) · 56.6 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import asyncio
from playwright.async_api import async_playwright
import os
from dotenv import load_dotenv
import logging
from typing import Optional
from pathlib import Path
import csv
import time
import json
import urllib.request
import sys
if sys.platform == 'win32':
import msvcrt
def _lock_shared(f):
msvcrt.locking(f.fileno(), msvcrt.LK_NBLCK, 1)
def _lock_exclusive(f):
msvcrt.locking(f.fileno(), msvcrt.LK_NBLCK, 1)
def _unlock(f):
try:
msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, 1)
except OSError:
pass
else:
import fcntl
def _lock_shared(f):
fcntl.flock(f.fileno(), fcntl.LOCK_SH)
def _lock_exclusive(f):
fcntl.flock(f.fileno(), fcntl.LOCK_EX)
def _unlock(f):
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
# set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# loading env variables
load_dotenv()
class WebAutomationBot:
def __init__(self, headless: bool = False):
self.headless = headless
self.browser = None
self.context = None
self.page = None
self.playwright = None
self._content_frame = None # frame that has item entry (may be iframe)
async def setup(self, use_saved_auth: bool = True):
"""Initialize browser and login if needed"""
# starts playwright
self.playwright = await async_playwright().start()
# launch browser maximized (full screen)
self.browser = await self.playwright.chromium.launch(
headless=self.headless,
slow_mo=0,
args=['--start-maximized']
)
# no_viewport=True lets the page fill the maximized window naturally
auth_file = "auth_state.json"
if use_saved_auth and Path(auth_file).exists():
logger.info("Loading saved authentication state...")
self.context = await self.browser.new_context(
storage_state=auth_file,
no_viewport=True
)
else:
logger.info("Creating new browser context...")
self.context = await self.browser.new_context(no_viewport=True)
self.context.set_default_timeout(30000) # 30s default timeout
self.page = await self.context.new_page()
# navigates to website
site_url = os.getenv('SITE_URL')
logger.info(f"Navigating to {site_url}")
await self.page.goto(site_url)
# wait for page to load
await self.page.wait_for_load_state('networkidle')
# check for session message - ensure only ONE tab (if new tab opens, use it and close old)
try:
start_over = await self.page.wait_for_selector('a:has-text("Click Here to Start Over")', timeout=3000)
if start_over:
logger.info("Session message found, clicking Start Over...")
try:
async with self.context.expect_page(timeout=5000) as page_info:
await start_over.click()
new_page = await page_info.value
await self.page.close()
self.page = new_page
logger.info("Using single tab (closed old)")
except Exception:
await self.page.wait_for_load_state('networkidle')
await self.page.wait_for_load_state('networkidle')
logger.info("Session cleared, proceeding...")
except Exception:
logger.info("No session message, proceeding...")
# check if we need to login
login_needed = False
try:
# check if username field exists (indicates login page)
await self.page.wait_for_selector('input[aria-label="Username"]', timeout=3000)
login_needed = True
logger.info("Login required, entering credentials...")
# fills username and password
username = os.getenv('SITE_USERNAME')
password = os.getenv('SITE_PASSWORD')
await self.page.fill('input[aria-label="Username"]', username)
await self.page.fill('input[aria-label="Password"]', password)
# login
await self.page.click('button:has-text("Log in")')
# wait for login to complete
await self.page.wait_for_load_state('networkidle')
logger.info("Login submitted...")
# wait longer for page to fully load after login
await asyncio.sleep(3)
# check if 2FA is needed or if we're logged in
logged_in = False
# Use the exact selector for the Add/View Retail Orders element
main_selector = 'span.IconCaptionText:has-text("Add/View Retail Orders")'
try:
# First try with short timeout
await self.page.wait_for_selector(main_selector, timeout=5000)
logger.info("Login successful! Found Add/View Retail Orders button")
logged_in = True
except:
# 2FA might be required or page is still loading
logger.info("Waiting for page to load or 2FA completion...")
logger.info("Please complete any 2FA in the browser if prompted.")
# Wait with periodic checks and better feedback
for i in range(60): # 60 seconds total
try:
# Check if element exists
element = await self.page.query_selector(main_selector)
if element:
logger.info("✓ Login/2FA completed successfully!")
logged_in = True
break
# Also check for any error messages
error_element = await self.page.query_selector('text=/error|invalid|incorrect/i')
if error_element:
logger.warning("Possible login error detected. Check credentials.")
except:
pass
await asyncio.sleep(1)
if i % 10 == 0 and i > 0:
logger.info(f"Still waiting for login completion... ({60-i} seconds remaining)")
if not logged_in:
# Take screenshot for debugging
logger.error("Could not find main page after login")
await self.page.screenshot(path="login_timeout.png")
logger.info("Screenshot saved as login_timeout.png for debugging")
# Log current URL for debugging
current_url = self.page.url
logger.info(f"Current URL: {current_url}")
# Try to log page content for debugging
try:
page_text = await self.page.text_content('body')
if page_text:
logger.info(f"Page contains text (first 200 chars): {page_text[:200]}...")
except:
pass
raise Exception("Login failed or page structure unexpected. Check login_timeout.png")
# save authentication state after successful login
if logged_in:
await self.save_auth_state()
except Exception as e:
if not login_needed:
logger.info("Already logged in with saved authentication")
else:
logger.error(f"Login process error: {e}")
raise
# verify we're on the main page using the correct selector
try:
main_selector = 'span.IconCaptionText:has-text("Add/View Retail Orders")'
await self.page.wait_for_selector(main_selector, timeout=10000)
logger.info("✓ Bot is ready to process orders")
except:
logger.error("Could not verify main page loaded correctly")
logger.info("Looking for debugging information...")
# Try to find what's on the page
try:
all_spans = await self.page.query_selector_all('span.IconCaptionText')
if all_spans:
logger.info(f"Found {len(all_spans)} IconCaptionText spans:")
for span in all_spans[:5]: # Log first 5
text = await span.text_content()
logger.info(f" - {text}")
except:
pass
await self.page.screenshot(path="error_page.png")
logger.info("Screenshot saved as error_page.png")
raise Exception("Main page did not load correctly - check error_page.png")
async def navigate_to_home(self):
"""Navigate back to home page to start new order"""
logger.info("Navigating back to home page...")
site_url = os.getenv('SITE_URL')
await self.page.goto(site_url)
await self.page.wait_for_load_state('networkidle')
# wait for main page to load with correct selector
main_selector = 'span.IconCaptionText:has-text("Add/View Retail Orders")'
await self.page.wait_for_selector(main_selector, timeout=10000)
async def start_order(self):
"""Navigate to the item entry page to start a new order"""
# make sure we're on home page
await self.navigate_to_home()
# clicks on add/view retail orders using the correct selector
logger.info("Starting new order...")
main_selector = 'span.IconCaptionText:has-text("Add/View Retail Orders")'
await self.page.click(main_selector)
await self.page.wait_for_load_state('networkidle')
# clicking add order
await self.page.click('span:has-text("Add Order")')
await self.page.wait_for_load_state('networkidle')
await asyncio.sleep(1) # wait for modal/form to render
# click on manually enter items - try multiple selectors (IDs can be dynamic)
manually_clicked = False
# try Playwright's robust locators first
for locator in [
self.page.get_by_label("Manually Enter Items"),
self.page.get_by_label("Manually enter items"),
self.page.get_by_role("radio", name="Manually Enter Items"),
self.page.get_by_role("radio", name="Manually enter items"),
]:
try:
if await locator.count() > 0:
await locator.first.click()
manually_clicked = True
logger.info("Selected manually enter items (label/role)")
break
except Exception:
continue
if not manually_clicked:
for selector in [
'input[type="radio"][id="Dl-q"]',
'label:has-text("Manually Enter")',
'label:has-text("Manually enter")',
'label:has-text("Manual Entry")',
'label:has-text("Manually")',
'span:has-text("Manually Enter")',
'span:has-text("Manual Entry")',
]:
try:
el = await self.page.wait_for_selector(selector, timeout=2000)
if el:
await el.click()
manually_clicked = True
logger.info(f"Selected manually enter items via: {selector}")
break
except Exception:
continue
if not manually_clicked:
# fallback: click the first radio after "Add Order" (manual is often first option)
try:
radios = await self.page.query_selector_all('input[type="radio"]')
if radios:
await radios[0].click()
manually_clicked = True
logger.info("Selected first radio option (manually enter)")
except Exception as e:
logger.warning(f"Radio fallback failed: {e}")
if not manually_clicked:
raise Exception("Could not find/click 'Manually Enter Items' option")
await asyncio.sleep(0.5)
# click next button to go to item entry page
await self._scroll_to_bottom()
await asyncio.sleep(0.2)
await self._scroll_to_bottom()
await self.page.click('span.ActionButtonCaptionText:has-text("Next")')
await self.page.wait_for_load_state('networkidle')
logger.info("Ready to search for items")
async def _get_search_input(self):
"""Get the item search input - try main page and iframes (Glide uses iframes)"""
for frame in [self.page] + list(self.page.frames):
try:
for selector in ['input[id="Dm-8"]', 'input[placeholder*="Item"]', 'input[id^="Dm"]', 'input[type="text"]']:
try:
el = await frame.wait_for_selector(selector, timeout=500)
if el and await el.is_visible():
self._content_frame = frame
return el
except Exception:
continue
except Exception:
continue
raise Exception("Could not find item search input")
async def _click_add_item(self):
"""Click Add Item - multiple methods, imperative that one works (Glide/ServiceNow structure)."""
async def try_click_in_frame(frame):
for sel in ['span:has-text("Add Item")', 'div:has(span:has-text("Add Item"))', 'td:has(span:has-text("Add Item"))']:
try:
loc = frame.locator(sel).first
if await loc.count() == 0:
continue
await loc.scroll_into_view_if_needed()
await loc.click(force=True, timeout=2000)
return True
except Exception:
continue
return False
async def try_click():
# try content frame first (where search input was found)
if self._content_frame and await try_click_in_frame(self._content_frame):
return True
if await try_click_in_frame(self.page):
return True
for fr in self.page.frames:
if fr != self.page.main_frame:
try:
if await try_click_in_frame(fr):
return True
except Exception:
pass
eval_page = self._content_frame if self._content_frame else self.page
# Method 1: JS - find element with "Add Item" (exact or includes), get clickable parent
r = await eval_page.evaluate("""() => {
const all = document.querySelectorAll('span, div, td, button, a');
for (const e of all) {
const t = e.textContent && e.textContent.trim();
if ((t === 'Add Item' || t.includes('Add Item')) && e.offsetParent) {
const target = e.closest('[data-event]') || e.closest('td') || e.closest('div[role="button"]') || e.parentElement || e;
target.scrollIntoView({block: 'center'});
const rect = target.getBoundingClientRect();
const x = rect.left + rect.width/2, y = rect.top + rect.height/2;
['mousedown','mouseup','click'].forEach(n => target.dispatchEvent(new MouseEvent(n, {bubbles:true,cancelable:true,view:window,clientX:x,clientY:y})));
return true;
}
}
return false;
}""")
if r:
return True
# Method 2: JS - direct .click() on element containing "Add Item"
r = await eval_page.evaluate("""() => {
const all = document.querySelectorAll('span, div, td, button');
for (const e of all) {
const t = e.textContent && e.textContent.trim();
if ((t === 'Add Item' || t.includes('Add Item')) && e.offsetParent) {
e.scrollIntoView({block: 'center'});
e.click();
return true;
}
}
return false;
}""")
if r:
return True
# Method 3: Playwright - div with data-event containing Add Item, mouse.click at center
try:
el = await eval_page.query_selector('div[data-event]:has(span:has-text("Add Item"))')
if el:
await el.scroll_into_view_if_needed()
box = await el.bounding_box()
if box:
await self.page.mouse.click(box['x'] + box['width']/2, box['y'] + box['height']/2)
return True
except Exception:
pass
# Method 4: Playwright - td containing Add Item
try:
el = await eval_page.query_selector('td:has(span:has-text("Add Item"))')
if el:
await el.scroll_into_view_if_needed()
await el.click(force=True)
return True
except Exception:
pass
# Method 5: Playwright - span ActionButtonCaptionText
try:
el = await eval_page.query_selector('span.ActionButtonCaptionText:has-text("Add Item")')
if el:
await el.scroll_into_view_if_needed()
await el.click(force=True)
return True
except Exception:
pass
# Method 6: Playwright - any span with Add Item, mouse click at center
try:
el = await eval_page.query_selector('span:has-text("Add Item")')
if el:
await el.scroll_into_view_if_needed()
box = await el.bounding_box()
if box:
await self.page.mouse.click(box['x'] + box['width']/2, box['y'] + box['height']/2)
return True
except Exception:
pass
# Method 7: Playwright getByText - flexible text match
try:
loc = eval_page.locator('text=Add Item')
if await loc.count() > 0:
await loc.first.scroll_into_view_if_needed()
await loc.first.click(force=True)
return True
except Exception:
pass
# Method 8: JS - any element containing "add" and "item" (case insensitive)
r = await eval_page.evaluate("""() => {
const all = document.querySelectorAll('span, div, td, button, a');
for (const e of all) {
const t = (e.textContent || '').toLowerCase();
if (t.includes('add') && t.includes('item') && t.length < 20 && e.offsetParent) {
const target = e.closest('[data-event]') || e.closest('td') || e.parentElement || e;
target.scrollIntoView({block: 'center'});
target.click();
return true;
}
}
return false;
}""")
if r:
return True
return False
try:
result = await asyncio.wait_for(try_click(), timeout=15.0)
except asyncio.TimeoutError:
logger.warning(" _click_add_item timed out after 15s")
result = False
if result:
await asyncio.sleep(0.6)
return result
async def check_and_process_items(self, items):
"""Check all unfilled items and add any available ones to cart (one order, min 10 total qty).
Skips items with order_filled='yes' so ordered items are never re-ordered."""
items_found = []
total_qty_added = 0
self.trace_log = getattr(self, 'trace_log', [])
for item in items:
item_number = item['item_number']
quantity = int(item['quantity'])
# skip if already ordered (order_filled persisted to CSV on successful submit)
if item.get('order_filled', '').lower() == 'yes':
continue
# skip backorder items (will be picked up in next cycle)
if item.get('order_filled', '').lower() == 'backorder':
continue
t_item_start = time.time()
trace = {'item': item_number, 'steps': {}, 'result': '', 'total_ms': 0}
# Human-like delay between searches (1-3s random)
import random
delay = random.uniform(1.0, 3.0)
logger.info(f"Checking item #{item_number} (requested qty: {quantity})... (waiting {delay:.1f}s)")
await asyncio.sleep(delay)
# enter item number - click, clear, type (type() triggers proper input events)
search_input = await self._get_search_input()
await search_input.click()
await search_input.fill('')
await search_input.type(str(item_number), delay=30)
await search_input.press('Enter')
await self.page.wait_for_load_state('networkidle')
await asyncio.sleep(0.5) # wait for search results to render
# STEP 1: Read available qty FIRST — skip immediately if 0
try:
ctx = self._content_frame if self._content_frame else self.page
logger.info(f" [STEP 1] Reading available qty for item #{item_number}...")
t0 = time.time()
available_quantity = -1
try:
el = await ctx.wait_for_selector('span[id="fgvt_Dm-m-1"]', timeout=3000)
available_text = await el.text_content() or '0'
available_quantity = int(available_text.replace(',', ''))
logger.info(f" [STEP 1] Available qty: {available_quantity}")
except Exception:
logger.warning(f" [STEP 1] Could not read qty via fgvt_Dm-m-1")
check_ms = round((time.time() - t0) * 1000)
trace['steps']['read_qty'] = check_ms
# Log scan for intelligence engine
try:
from intelligence import log_scan
log_scan(item_number, max(available_quantity, 0),
available_quantity > 0, check_ms)
except Exception:
pass
# If qty is 0, skip — don't waste 15s trying to click inactive Add Item
if available_quantity == 0:
delay = random.uniform(1.0, 3.0)
logger.info(f" x Item #{item_number} — available qty is 0, skipping (waiting {delay:.1f}s)")
await asyncio.sleep(delay)
trace['result'] = 'SKIP (qty 0)'
trace['total_ms'] = round((time.time() - t_item_start) * 1000)
self.trace_log.append(trace)
raise Exception("Available quantity is 0")
# If qty > 0, adjust order qty if needed
backorder_qty = 0
if available_quantity > 0:
logger.info(f" ✓ Item #{item_number} is AVAILABLE! (qty: {available_quantity})")
if quantity > available_quantity:
backorder_qty = quantity - available_quantity
logger.info(f" Ordering {available_quantity} of {quantity} requested ({backorder_qty} → backorder)")
quantity = available_quantity
else:
logger.info(f" Using requested quantity: {quantity}")
else:
# Could not read qty via fgvt_Dm-m-1 — try broader selectors
logger.info(f" [STEP 1] Trying broader qty selectors...")
for qty_sel in ['span[id^="fgvt_Dm"]', 'span[id^="fgvt_"]']:
try:
els = await ctx.query_selector_all(qty_sel)
for qel in els:
txt = (await qel.text_content() or '').replace(',', '').strip()
if txt.isdigit():
val = int(txt)
el_id = await qel.get_attribute('id') or '?'
logger.info(f" Found {el_id}: {val}")
if val > 0 and available_quantity <= 0:
available_quantity = val
except Exception:
continue
if available_quantity > 0:
logger.info(f" ✓ Item #{item_number} is AVAILABLE! (qty: {available_quantity} from broader scan)")
if quantity > available_quantity:
backorder_qty = quantity - available_quantity
logger.info(f" Ordering {available_quantity} of {quantity} requested ({backorder_qty} → backorder)")
quantity = available_quantity
else:
logger.info(f" Using requested quantity: {quantity}")
else:
# Still unknown — check if Add Item button is active as last resort
logger.info(f" [STEP 1] Qty still unknown, checking Add Item button state...")
add_item_visible = None
for sel in ['span:has-text("Add Item")', 'button:has-text("Add Item")']:
try:
add_item_visible = await ctx.wait_for_selector(sel, timeout=1000)
if add_item_visible:
break
except Exception:
continue
if not add_item_visible:
delay = random.uniform(1.0, 3.0)
logger.info(f" x Item #{item_number} — not available, skipping (waiting {delay:.1f}s)")
await asyncio.sleep(delay)
raise Exception("Add Item button not found and qty unknown")
logger.warning(f" ⚠ Item #{item_number} — qty unknown but Add Item active, using requested qty: {quantity}")
# click Add Item button
logger.info(f" [STEP 2] Clicking Add Item button...")
add_clicked = await self._click_add_item()
if add_clicked:
logger.info(" [STEP 2] ✓ Add Item clicked successfully, waiting for quantity modal...")
if not add_clicked:
await self.page.screenshot(path="add_item_fail.png")
html_snippet = await ctx.evaluate("""() => {
const spans = document.querySelectorAll('span');
return Array.from(spans).filter(s => s.textContent && s.textContent.includes('Add')).slice(0,10).map(s => ({text: s.textContent.trim().substring(0,50), tag: s.tagName})).join('|');
}""")
logger.error(f" [STEP 2] FAILED - Add Item elements on page: {html_snippet}")
raise Exception("Failed to click Add Item - see add_item_fail.png")
await self.page.wait_for_load_state('networkidle')
await asyncio.sleep(0.4) # wait for quantity modal to open
# Type quantity into modal (use adjusted qty if we capped by available), then click Add Item
qty_str = str(int(quantity))
logger.info(f" [STEP 3] Entering quantity {qty_str} for item #{item_number}...")
done = False
all_frames = [self.page] + list(self.page.frames)
for frame in all_frames:
try:
# Find quantity input - id Ds_1-81, class DocControlQuantity
result = await frame.evaluate(f"""() => {{
const qtyInput = document.querySelector('input[id="Ds_1-81"]') || document.querySelector('input[id^="Ds_1"]') || document.querySelector('input.DocControlQuantity') || document.querySelector('input[name="Ds_1-81"]');
if (!qtyInput || !qtyInput.offsetParent) return false;
qtyInput.focus();
qtyInput.select();
qtyInput.value = '';
qtyInput.value = '{qty_str}';
qtyInput.dispatchEvent(new Event('input', {{ bubbles: true }}));
qtyInput.dispatchEvent(new Event('change', {{ bubbles: true }}));
qtyInput.dispatchEvent(new Event('blur', {{ bubbles: true }}));
const dialog = document.querySelector('[role="dialog"]') || document.querySelector('[class*="modal"]') || document;
const btns = dialog.querySelectorAll('button, span, div[role="button"], a');
for (const b of btns) {{
const t = (b.textContent || '').trim();
if (t === 'Add Item' || t === 'OK') {{
b.click();
return true;
}}
}}
return false;
}}""")
if result:
done = True
logger.info(f" [STEP 3] ✓ Entered quantity {qty_str} and clicked Add Item in modal")
break
except Exception:
continue
if not done:
logger.info(f" [STEP 3] JS method failed, trying Playwright method...")
# Playwright: find quantity input by exact id/class
for frame in all_frames:
try:
qty_input = await frame.query_selector('input[id="Ds_1-81"]') or await frame.query_selector('input[id^="Ds_1"]') or await frame.query_selector('input.DocControlQuantity')
if qty_input and await qty_input.is_visible():
await qty_input.click()
await qty_input.fill('')
await qty_input.type(qty_str, delay=20)
await asyncio.sleep(0.1)
add_btn = await frame.query_selector('[role="dialog"] button:has-text("Add Item"), [role="dialog"] span:has-text("Add Item")') or await frame.query_selector('[role="dialog"] button:has-text("OK"), [role="dialog"] span:has-text("OK")')
if add_btn:
await add_btn.click()
done = True
logger.info(f" [STEP 3] ✓ Entered quantity {qty_str} via Playwright method")
break
except Exception:
continue
if not done:
await self.page.screenshot(path="qty_modal_fail.png")
raise Exception("Could not enter quantity — see qty_modal_fail.png")
await self.page.wait_for_load_state('networkidle')
await asyncio.sleep(0.2) # wait for modal to close
# mark item as processed
item['order_filled'] = 'yes'
item['quantity'] = quantity # update to actual ordered qty
items_found.append(item)
total_qty_added += quantity
# Log win for intelligence engine
try:
from intelligence import log_scan, log_win
log_scan(item_number, available_quantity, True, check_ms,
order_attempted=True, order_success=True)
log_win(item_number, quantity, available_quantity)
except Exception:
pass
# create backorder entry for remaining qty
if backorder_qty > 0:
backorder_item = {
'item_number': item_number,
'quantity': backorder_qty,
'order_filled': 'backorder'
}
items.append(backorder_item)
logger.info(f" + Backorder created: {backorder_qty} units for item #{item_number}")
trace['result'] = f"ADDED {quantity} units" + (f" (+{backorder_qty} backorder)" if backorder_qty > 0 else "")
trace['total_ms'] = round((time.time() - t_item_start) * 1000)
self.trace_log.append(trace)
logger.info(f" [STEP 4] ✓ Added to cart: {quantity} units (total: {total_qty_added}) [{trace['total_ms']}ms]")
except Exception as e:
logger.error(f" ✗ Item #{item_number} FAILED at: {e}")
if 'SKIP' not in trace.get('result', ''):
trace['result'] = f"FAILED: {e}"
trace['total_ms'] = round((time.time() - t_item_start) * 1000)
self.trace_log.append(trace)
try:
await self.page.screenshot(path=f"item_{item_number}_fail.png")
logger.error(f" Screenshot saved: item_{item_number}_fail.png")
except Exception:
pass
try:
search_input = await self._get_search_input()
await search_input.click()
await search_input.press('Control+a')
await search_input.press('Backspace')
await asyncio.sleep(0.1)
except Exception:
pass
return items_found, total_qty_added
async def _scroll_to_bottom(self):
"""Scroll page and all frames to bottom - call before every Next/Submit."""
async def do_scroll(ctx):
try:
await ctx.evaluate('window.scrollTo(0, document.body.scrollHeight)')
await ctx.evaluate('() => { const d = document.scrollingElement || document.documentElement; if(d) d.scrollTop = d.scrollHeight; }')
await ctx.evaluate('''() => {
document.querySelectorAll('[style*="overflow"], .Overflown, [class*="Scroll"]').forEach(el => {
if (el.scrollHeight > el.clientHeight) el.scrollTop = el.scrollHeight;
});
}''')
except Exception:
pass
await do_scroll(self.page)
if self._content_frame:
await do_scroll(self._content_frame)
for fr in self.page.frames:
if fr != self.page.main_frame:
try:
await do_scroll(fr)
except Exception:
pass
async def _click_next_or_submit(self, text: str, timeout_ms: int = 3000):
"""Click Next or Submit - span.ActionButtonCaptionText structure.
Scrolls to bottom so buttons are visible, then clicks."""
await self._scroll_to_bottom()
await asyncio.sleep(0.1)
await self._scroll_to_bottom()
contexts = ([self._content_frame] if self._content_frame else []) + [self.page] + list(self.page.frames)
for ctx in contexts:
for sel in [
f'span.ActionButtonCaptionText:has-text("{text}")',
f'xpath=//span[@class="ActionButtonCaptionText" and contains(., "{text}")]',
f'span:has-text("{text}")',
f'button:has-text("{text}")',
]:
try:
btn = await ctx.wait_for_selector(sel, timeout=timeout_ms)
if btn and await btn.is_visible():
await btn.scroll_into_view_if_needed()
await btn.click()
return True
except Exception:
continue
# try clicking parent via JS (span may be inside button - parent receives click)
try:
span = await ctx.wait_for_selector(f'span.ActionButtonCaptionText:has-text("{text}")', timeout=timeout_ms)
if span and await span.is_visible():
clicked = await span.evaluate("""el => {
const p = el.closest('button, div[role="button"], [class*="ActionButton"], [class*="Button"]') || el.parentElement;
if (p && p.offsetParent) { p.click(); return true; }
return false;
}""")
if clicked:
return True
except Exception:
pass
return False
async def _fill_password_field(self, password: str) -> bool:
"""Click input#Dn-k and type password using real keyboard events.
Field has FastEvtFieldKeyDown/FastEvtFieldFocus so needs actual key events.
Searches content frame first (checkout often runs in iframe)."""
pw_js = """() => {
const el = document.getElementById('Dn-k') || document.querySelector('input[name="Dn-k"]') || document.querySelector('input.DocControlPassword') || document.querySelector('input[type="password"]');
if (!el) return false;
el.scrollIntoView({block: 'center'});
el.focus();
el.click();
el.value = '';
el.dispatchEvent(new Event('focus', {bubbles: true}));
return true;
}"""
pw_selectors = ['input#Dn-k', 'input[name="Dn-k"]', 'input.DocControlPassword', 'input[type="password"]']
# Build frame search order: content frame first, then main page, then others
frames_to_try = []
if self._content_frame:
frames_to_try.append(self._content_frame)
frames_to_try.append(self.page)
for fr in self.page.frames:
if fr != self.page.main_frame and fr not in frames_to_try:
frames_to_try.append(fr)
logger.info("Attempting password entry...")
# Method 1: JS focus + page.keyboard (per frame)
for ctx in frames_to_try:
try:
focused = await ctx.evaluate(pw_js)
if focused:
await asyncio.sleep(0.05)
await self.page.keyboard.type(password, delay=10)
logger.info("Password entered (JS focus + keyboard.type)")
return True
except Exception:
continue
# Method 2: Playwright click + keyboard.type (per frame)
for ctx in frames_to_try:
for sel in pw_selectors:
try:
el = await ctx.wait_for_selector(sel, timeout=1500)
if el and await el.is_visible():
await el.scroll_into_view_if_needed()
await el.click(force=True)
await asyncio.sleep(0.05)
await self.page.keyboard.press('Control+a')
await self.page.keyboard.press('Delete')
await self.page.keyboard.type(password, delay=10)
logger.info(f"Password entered (click {sel} + keyboard.type)")
return True
except Exception:
continue
# Method 4: Tab into field from known page state, then type
try:
await self.page.keyboard.press('Tab')
await asyncio.sleep(0.05)
await self.page.keyboard.type(password, delay=10)
logger.info("Password entered (Tab + keyboard.type)")
return True
except Exception as e:
logger.warning(f"Password method 4 failed: {e}")
return False
async def submit_order(self):
"""Complete checkout and submit the full order (all items in cart)"""
logger.info("Proceeding to checkout - placing full order...")
try:
# after item entry: click Next twice to reach payment/checkout
for step in range(2):
logger.info(f"Clicking Next (item entry step {step+1})...")
if not await self._click_next_or_submit("Next", timeout_ms=5000):
raise Exception(f"Could not find Next button (item entry step {step+1})")
try:
await self.page.wait_for_load_state('networkidle', timeout=10000)
except Exception:
pass
await asyncio.sleep(0.3)
# select ACH Debit Bank payment method if present
logger.info("Looking for ACH Debit Bank...")
for ctx in ([self._content_frame] if self._content_frame else []) + [self.page] + list(self.page.frames):
for sel in ['text=ACH Debit Bank', ':has-text("ACH Debit Bank")', 'span:has-text("ACH Debit")', 'label:has-text("ACH")']:
try:
ach = await ctx.wait_for_selector(sel, timeout=2000)
if ach and await ach.is_visible():
await ach.click()
logger.info("Selected ACH Debit Bank")
await asyncio.sleep(0.2)
break
except Exception:
continue
# checkout: Next (after ACH payment) -> Next (review) -> Next (confirm) -> Submit
for step in range(3):
logger.info(f"Clicking Next (checkout step {step+1})...")
if not await self._click_next_or_submit("Next", timeout_ms=5000):
raise Exception(f"Could not find Next button at checkout step {step+1}")
try:
await self.page.wait_for_load_state('networkidle', timeout=10000)
except Exception:
pass
await asyncio.sleep(0.3)
# final submit
logger.info("Clicking Submit...")
if not await self._click_next_or_submit("Submit"):
raise Exception("Could not find Submit button")
try:
await self.page.wait_for_load_state('networkidle', timeout=10000)
except Exception:
pass
await asyncio.sleep(0.3)
# password confirmation - uses keyboard.type for real key events
logger.info("Entering confirmation password...")
password = os.getenv('SITE_PASSWORD')
if not password:
raise Exception("SITE_PASSWORD not set - required for order confirmation")
await self._scroll_to_bottom()
await asyncio.sleep(0.1)
await self._scroll_to_bottom()
try:
scroll_more = await self.page.query_selector('a.ScrollForMoreLink, a[data-event="ScrollForMore"]')
if scroll_more and await scroll_more.is_visible():
await scroll_more.click()
await asyncio.sleep(0.1)
except Exception:
pass
pw_filled = await self._fill_password_field(password)
if not pw_filled:
raise Exception("Could not enter password - all methods failed")
await asyncio.sleep(0.1)
# click final Submit to confirm after password
logger.info("Clicking final Submit after password...")
if not await self._click_next_or_submit("Submit"):
raise Exception("Could not find final Submit button after password")
try:
await self.page.wait_for_load_state('networkidle', timeout=10000)
except Exception:
pass
logger.info("✓ Order submitted successfully!")
await asyncio.sleep(1) # wait for confirmation
return True
except Exception as e:
logger.error(f"Error during checkout: {e}")
raise
async def process_multiple_items(self, items):
"""Process unfilled items: start order, add available ones to cart, submit if any found.
Returns dict with success, items_ordered, message for reporting."""
unfilled_items = [i for i in items if i.get('order_filled', '').lower() != 'yes']
if not unfilled_items:
return {'success': False, 'items_ordered': [], 'message': 'No unfilled items'}
# Start new order if not already on item entry page
current_url = self.page.url
if 'itemEntry' not in current_url:
await self.start_order()
items_found, total_qty_added = await self.check_and_process_items(items)
if not items_found:
logger.info("No items available at this time")
return {'success': False, 'items_ordered': [], 'message': 'No items were available'}
if total_qty_added < 10:
logger.warning(f"Order requires minimum 10 quantity total (have {total_qty_added}). Not submitting - add more items.")
for item in items_found:
item['order_filled'] = ''
return {'success': False, 'items_ordered': [], 'message': f'Need min 10 qty (have {total_qty_added})'}
item_numbers = [str(i['item_number']) for i in items_found]
logger.info(f"✓ Found {len(items_found)} items, {total_qty_added} total qty: {', '.join(item_numbers)}")
try:
await self.submit_order()
logger.info(f"ORDER SUCCESS: Placed order for items {', '.join(item_numbers)}")
return {
'success': True,
'items_ordered': item_numbers,
'message': f'Order placed for {len(items_found)} item(s): {", ".join(item_numbers)}'
}
except Exception as e:
logger.error(f"ORDER FAILED: {e}")
for item in items_found:
item['order_filled'] = ''
return {
'success': False,
'items_ordered': item_numbers,
'message': f'Checkout failed: {e}'
}
async def save_auth_state(self):
"""Save authentication state for future runs"""
logger.info("Saving authentication state...")
await self.context.storage_state(path="auth_state.json")
logger.info("Authentication state saved")
async def cleanup(self):
"""Clean up browser resources"""
if self.browser:
await self.browser.close()
if self.playwright:
await self.playwright.stop()
def read_csv_file(filename):
"""Read CSV file and return list of items"""
items = []
try:
with open(filename, 'r', newline='') as file:
reader = csv.DictReader(file)