def connect(browser='firefox', session=None): from seleniumrequests import Firefox driver = Firefox() driver.get("https://www.backstabbr.com/") # Restore previous session if specified if session: for cookie in pickle.load(open(session, "rb")): driver.add_cookie(cookie) return driver
def get_driver(debug: bool = False): """ Returns an alive Firefox WebDriver """ global driver ### webdriver with features from the normal requests lib from seleniumrequests import Firefox ### we need options to start a headless firefox instance from selenium.webdriver.firefox.options import Options from selenium.webdriver.remote.command import Command is_alive = None try: driver.execute(Command.STATUS) is_alive = True except: is_alive = False if not is_alive: browser_options = Options() browser_options.add_argument('--headless') browser_options.add_argument('--window-size=1920x1080') driver = Firefox(options=browser_options, executable_path=geckodriver_location) ### load existing cookies if cookies_path.exists(): driver.get(urls['login']) if debug: dprint("Found existing cookies. Attempting to reuse session...") import pickle with open(cookies_path, 'rb') as cookies_file: cookies = pickle.load(cookies_file) for cookie in cookies: driver.add_cookie(cookie) return driver