### Install Selenium and WebDriver Manager Source: https://fantraxapi.readthedocs.io/en/latest Install the necessary Python packages for browser automation. ```bash pip install selenium pip install webdriver-manager ``` -------------------------------- ### Install fantraxapi Source: https://fantraxapi.readthedocs.io/en/latest Install the fantraxapi package using pip. Documentation is available on Read the Docs. ```bash pip install fantraxapi ``` -------------------------------- ### Get FantraxAPI Instance Source: https://fantraxapi.readthedocs.io/en/latest Instantiate the League object to connect to the Fantrax API. Requires a league ID. ```python from fantraxapi import League league_id = "96igs4677sgjk7ol" league = League(league_id) ``` ```python import fantraxapi league_id = "96igs4677sgjk7ol" league = fantraxapi.League(league_id) ``` -------------------------------- ### Get Season Scores Source: https://fantraxapi.readthedocs.io/en/latest Retrieve and print scoring periods for a given league. Requires an initialized League object. ```python from fantraxapi import League league_id = "96igs4677sgjk7ol" league = League(league_id) for _, scoring_period in league.scoring_periods().items(): print("") print(scoring_period) ``` -------------------------------- ### Automate Fantrax Login with Selenium Source: https://fantraxapi.readthedocs.io/en/latest This code sets up an automatic login process for Fantrax using Selenium. It overrides the default API request method to handle login via Google Chrome, saving and loading cookies to maintain session persistence. Ensure you replace 'YOUR_USERNAME_HERE' and 'YOUR_PASSWORD_HERE' with your actual Fantrax credentials. ```python import os import pickle import time from requests import Session from selenium import webdriver from selenium.webdriver import Keys from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions from selenium.webdriver.support.ui import WebDriverWait from webdriver_manager.chrome import ChromeDriverManager from fantraxapi import League, NotLoggedIn, api from fantraxapi.api import Method username = "YOUR_USERNAME_HERE" # Provide your Fantrax Username here password = "YOUR_PASSWORD_HERE" # Provide your Fantrax Password here cookie_filepath = "fantraxloggedin.cookie" # Name of the saved Cookie file old_request = api.request # Saves the old function def new_request(league: "League", methods: list[Method] | Method) -> dict: try: if not league.logged_in: add_cookie_to_session(league.session) # Tries the login function when not logged in return old_request(league, methods) # Run old function except NotLoggedIn: add_cookie_to_session(league.session, ignore_cookie=True) # Adds/refreshes the cookie when NotLoggedIn is raised return new_request(league, methods) # Rerun the request api.request = new_request # replace the old function with the new function def add_cookie_to_session(session: Session, ignore_cookie: bool = False) -> None: if not ignore_cookie and os.path.exists(cookie_filepath): with open(cookie_filepath, "rb") as f: for cookie in pickle.load(f): session.cookies.set(cookie["name"], cookie["value"]) else: service = Service(ChromeDriverManager().install()) options = Options() options.add_argument("--headless") options.add_argument("--window-size=1920,1600") options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36") with webdriver.Chrome(service=service, options=options) as driver: driver.get("https://www.fantrax.com/login") username_box = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, "//input[@formcontrolname='email']"))) username_box.send_keys(username) password_box = WebDriverWait(driver, 10).until(expected_conditions.presence_of_element_located((By.XPATH, "//input[@formcontrolname='password']"))) password_box.send_keys(password) password_box.send_keys(Keys.ENTER) time.sleep(5) cookies = driver.get_cookies() with open(cookie_filepath, "wb") as cookie_file: pickle.dump(driver.get_cookies(), cookie_file) for cookie in cookies: session.cookies.set(cookie["name"], cookie["value"]) league_id = "usglqmvqmelpe6um" my_league = League(league_id) print(my_league.trade_block()) # The Trade Block Page is always private ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.