headless by default, headful for login
This commit is contained in:
@@ -2,54 +2,92 @@ from pathlib import Path
|
||||
import random
|
||||
import time
|
||||
|
||||
from playwright.sync_api import sync_playwright, TimeoutError as PlaywrightTimeoutError
|
||||
from playwright.sync_api import (
|
||||
sync_playwright,
|
||||
TimeoutError as PlaywrightTimeoutError,
|
||||
Error as PlaywrightError,
|
||||
)
|
||||
|
||||
CHROME_DATA_DIR = Path(__file__).parent / "chrome-data-dir"
|
||||
CHROME_DATA_DIR.mkdir(exist_ok=True)
|
||||
|
||||
LOGIN_TIMEOUT_MS = 4 * 60 * 1000
|
||||
|
||||
|
||||
def launch_context(p, *, headless: bool):
|
||||
return p.chromium.launch_persistent_context(
|
||||
user_data_dir=CHROME_DATA_DIR,
|
||||
headless=headless,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
# user_data_dir = "C:\\Users\\Admin\\AppData\\Local\\Google\\Chrome\\User Data"
|
||||
# not usable because New Chrome blocks this. to fight malware.
|
||||
# chrome_exe = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
|
||||
with sync_playwright() as p:
|
||||
context = p.chromium.launch_persistent_context(
|
||||
# user_data_dir,
|
||||
user_data_dir=Path(__file__).parent / "chrome-data-dir",
|
||||
# executable_path=chrome_exe,
|
||||
# args=[
|
||||
# "--profile-directory=Default",
|
||||
# ],
|
||||
headless=False,
|
||||
)
|
||||
context = launch_context(p, headless=True)
|
||||
|
||||
page = context.new_page()
|
||||
|
||||
page.goto("https://game.skport.com/endfield/sign-in")
|
||||
|
||||
login_form = page.locator("form")
|
||||
container = page.locator("#lottie-container")
|
||||
|
||||
now = time.time()
|
||||
while time.time() - now < 60:
|
||||
deadline = time.time() + 60
|
||||
|
||||
while time.time() < deadline:
|
||||
# need manual login
|
||||
if login_form.first.is_visible():
|
||||
print("Please log in manually and then close the browser.")
|
||||
page.wait_for_event("close") # god damn
|
||||
print("Please log in manually, then close the browser window.")
|
||||
|
||||
context.close()
|
||||
|
||||
headful = launch_context(p, headless=False)
|
||||
|
||||
page = headful.pages[0] if headful.pages else headful.new_page()
|
||||
page.goto("https://game.skport.com/endfield/sign-in")
|
||||
|
||||
try:
|
||||
headful.wait_for_event(
|
||||
"close",
|
||||
timeout=LOGIN_TIMEOUT_MS,
|
||||
)
|
||||
|
||||
print("Browser closed. Please rerun the script.")
|
||||
|
||||
except PlaywrightTimeoutError:
|
||||
print("Login timeout reached.")
|
||||
headful.close()
|
||||
|
||||
break
|
||||
|
||||
# happy path
|
||||
if container.first.is_visible():
|
||||
try:
|
||||
container.first.click(timeout=2000, delay=random.randint(100, 300))
|
||||
container.first.click(
|
||||
timeout=2000,
|
||||
delay=random.randint(100, 300),
|
||||
)
|
||||
|
||||
print("clicked")
|
||||
break
|
||||
except (TimeoutError, PlaywrightTimeoutError) as e:
|
||||
if all(
|
||||
x in str(e)
|
||||
for x in ("intercepts pointer events", "completed-overlay")
|
||||
|
||||
except (PlaywrightTimeoutError, PlaywrightError) as e:
|
||||
msg = str(e)
|
||||
|
||||
if (
|
||||
"intercepts pointer events" in msg
|
||||
and "completed-overlay" in msg
|
||||
):
|
||||
print("Work done somewhere else, exiting...")
|
||||
print("Work already completed elsewhere.")
|
||||
break
|
||||
|
||||
raise # re-raise other exceptions
|
||||
|
||||
page.wait_for_timeout(200)
|
||||
|
||||
page.close()
|
||||
context.close()
|
||||
try:
|
||||
context.close()
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user