from playwright.sync_api import sync_playwright from datetime import datetime import os import re EMAIL = "vbuzalka@its.jnj.com" PASSWORD = "%zT3Wqfc9)cWua5" LOGIN_URL = "https://xsp.covance.com/" HOME_URL = "https://xsp.labcorp.com/sampletracking/home" PROTOCOL = "77242113UCO3001" FROM_DATE = "01-Jan-2025" OUT_DIR = r"U:\PythonProject\Janssen\Covance_UCO3001\Source" PROFILE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "browser_profile") # (název dlaždice, suffix výstupního souboru) TILES = [ ("Cancelled Samples", "SamplesCancelled"), ("Samples Not Available", "SamplesNotAvailable"), ("Samples Received", "samplesReceived"), ("Samples In Inventory", "samplesInInventory"), ("Samples Shipped from Labcorp", "samplesShipped"), ("Samples Pending Arrival", "samplesNotReceived"), ("Samples Shipped from Site to Labcorp", "ShippedToLabcorp"), ("Samples Shipped from Site to Third Party", "ShippedToThirdParty"), ("Samples Discarded from Specimen Management", "samplesDiscarded"), ("All Samples", "allSamples"), ] def login(page): page.goto(LOGIN_URL) page.wait_for_load_state("networkidle", timeout=120000) page.get_by_label("Email").fill(EMAIL) page.get_by_role("button", name="Next").click() page.wait_for_load_state("networkidle", timeout=120000) page.get_by_label("Password").fill(PASSWORD) page.get_by_role("button", name="Verify").click() page.wait_for_timeout(15000) page.wait_for_load_state("networkidle", timeout=120000) print(f"Prihlaseni OK: {page.url}") def select_protocol(page): page.goto(HOME_URL) page.wait_for_load_state("networkidle", timeout=120000) page.wait_for_timeout(3000) page.locator("span").filter(has_text=PROTOCOL).first.click() page.wait_for_load_state("networkidle", timeout=120000) page.wait_for_timeout(3000) print(f"Protokol vybran: {page.url}") def apply_country_filter(page): page.get_by_text("keyboard_arrow_down").nth(3).click() page.wait_for_timeout(2000) page.get_by_role("checkbox", name="Czech Republic").check() page.wait_for_timeout(2000) page.mouse.move(0, -50, steps=5) page.wait_for_load_state("networkidle", timeout=120000) page.wait_for_timeout(3000) print("Country filter CZ aplikovan.") def export_tile(page, tile_label, file_suffix, timestamp): # Klikni na View Samples u správné dlaždice page.locator("div.study-group-card").filter( has=page.locator("span.label", has_text=tile_label) ).locator("button.view-sample").click() page.wait_for_load_state("networkidle", timeout=120000) page.wait_for_timeout(3000) print(f" Otevreno: {tile_label} ({page.url})") # Date picker page.get_by_role("button", name="DD/MM - DD/MM").click() page.wait_for_timeout(2000) page.get_by_role("textbox", name="Date input field").first.click() page.get_by_role("textbox", name="Date input field").first.press("End") page.get_by_role("textbox", name="Date input field").first.press("Shift+Home") page.get_by_role("textbox", name="Date input field").first.fill(FROM_DATE) page.wait_for_timeout(500) page.get_by_role("button", name="Apply").click() page.wait_for_load_state("networkidle", timeout=120000) page.wait_for_timeout(3000) # Zkontroluj Record Count record_el = page.locator("text=Record Count:").first record_text = record_el.inner_text(timeout=10000) count_str = record_text.replace("Record Count:", "").strip().replace(",", "") count = int(count_str) if count_str.isdigit() else -1 if count == 0: print(f" Record Count: 0 — preskakuji.") return print(f" Record Count: {count}") with page.expect_download(timeout=120000) as dl: page.get_by_role("button", name="Export arrow_drop_down").click() page.wait_for_timeout(1000) page.get_by_text("Export As CSV").click() dest = os.path.join(OUT_DIR, f"{timestamp} sponsor-study-36940-samples-{file_suffix}.csv") dl.value.save_as(dest) print(f" Stazeno: {dest}") def download(page): timestamp = datetime.now().strftime("%Y-%m-%d_%H%M%S") for tile_label, file_suffix in TILES: print(f"Zpracovavam: {tile_label}") select_protocol(page) apply_country_filter(page) export_tile(page, tile_label, file_suffix, timestamp) print("Hotovo.") if __name__ == "__main__": with sync_playwright() as p: context = p.chromium.launch_persistent_context( user_data_dir=PROFILE_DIR, headless=False, args=["--disable-blink-features=AutomationControlled"], user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36", accept_downloads=True, ) context.add_init_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})") page = context.new_page() login(page) download(page) context.close()