Skip to content

Commit

Permalink
Updated with try block to always cleanup even if exception occurs or …
Browse files Browse the repository at this point in the history
…user interception
  • Loading branch information
dmytrokoren committed Aug 29, 2024
1 parent 565553b commit 56c5767
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions lib/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,39 +144,38 @@ def _get_driver(self) -> Driver:
logger.debug("Starting webdriver for current session")
browser_path = self.checkin_scheduler.reservation_monitor.config.browser_path

# Create a new temporary directory for Chrome profile
# Create a persistent directory for Chrome profile
profile_dir = os.path.join(tempfile.gettempdir(), "chrome_profile")
os.makedirs(profile_dir, exist_ok=True)

# Randomly decide whether to use a fresh profile or the persistent one
temp_dir = tempfile.mkdtemp() if random.choice([True, False]) else profile_dir

driver = Driver(
binary_location=browser_path,
driver_version=driver_version,
user_data_dir=temp_dir,
page_load_strategy="none",
headed=is_docker,
headless=not is_docker,
uc_cdp_events=True,
undetectable=True,
incognito=True,
)
logger.debug("Using browser version: %s", driver.caps["browserVersion"])

driver.add_cdp_listener("Network.requestWillBeSent", self._headers_listener)
driver.delete_all_cookies()

logger.debug("Loading Southwest check-in page (this may take a moment)")
driver.set_window_size(random.randint(1024, 1920), random.randint(768, 1080))
driver.uc_open_with_reconnect(BASE_URL, 2)
driver.wait_for_element("//*[@alt='Check in banner']")
self._take_debug_screenshot(driver, "after_page_load.png")
time.sleep(random_sleep_duration(1, 2))
driver.click("//*[@alt='Check in banner']")

# Clean up the temporary directory after use
shutil.rmtree(driver.user_data_dir, ignore_errors=True)
try:
driver = Driver(
binary_location=browser_path,
driver_version=driver_version,
user_data_dir=profile_dir,
page_load_strategy="none",
headed=is_docker,
headless=not is_docker,
uc_cdp_events=True,
undetectable=True,
incognito=True,
)
logger.debug("Using browser version: %s", driver.caps["browserVersion"])

driver.add_cdp_listener("Network.requestWillBeSent", self._headers_listener)
driver.delete_all_cookies()

logger.debug("Loading Southwest check-in page (this may take a moment)")
driver.set_window_size(random.randint(1024, 1920), random.randint(768, 1080))
driver.uc_open_with_reconnect(BASE_URL, 2)
driver.wait_for_element("//*[@alt='Check in banner']")
self._take_debug_screenshot(driver, "after_page_load.png")
time.sleep(random_sleep_duration(1, 2))
driver.click("//*[@alt='Check in banner']")

finally:
# Clean up the profile directory after use
shutil.rmtree(profile_dir, ignore_errors=True)

return driver

Expand Down

0 comments on commit 56c5767

Please sign in to comment.