Selenium Login Automation

Purpose

Selenium Login Automation specifically addresses the challenge of securely and reliably logging into Qualtrics using institutional credentials (NYU in this case). Unlike simple API-based authentication, Qualtrics requires navigating a complex web login flow involving username/password entry, two-factor authentication (2FA), and browser trust prompts. This subtopic automates those interactions using Selenium WebDriver, simulating a human user to bypass bot detection and handle dynamic UI elements that cannot be managed via direct HTTP requests. It ensures that once logged in, authenticated session cookies can be extracted and reused for subsequent API calls.

Functionality

At its core, this subtopic encapsulates a browser automation sequence that:

  1. Launches a headless Chrome browser with custom user-agent and sandboxing options for stability and stealth.

  2. Navigates to the Qualtrics login page on the configured subdomain.

  3. Waits for the username and password input elements to be present, then enters credentials with human-like typing delays (_human_type method).

  4. Clicks the login button, triggering the authentication process.

  5. Handles two-factor authentication and trust prompts by polling the page state and interacting with any browser trust confirmation dialogs.

  6. Confirms successful login by detecting Qualtrics-specific page elements indicating the user is authenticated.

  7. Extracts session cookies from the Selenium-controlled browser and transfers them to a requests.Session object for continued authenticated HTTP requests without the browser.

The human-like interaction is modeled by the `_human_type` function, which introduces variable pauses between keystrokes to mimic natural typing rhythms, and the `human_sleep` method, which models pauses between UI actions using a truncated normal distribution for realistic delays.

Key Code Snippets

options = Options()
options.add_argument("--headless")
options.add_argument(f"user-agent={self.user_agent}")
driver = webdriver.Chrome(options=options)
driver.get(f"https://{self.subdomain}.qualtrics.com/ControlPanel/")
username_input = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'username')))
self._human_type(username_input, nyu_username)

password_input = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'password')))
self._human_type(password_input, nyu_password)
while not self._at_qualtrics(driver):
    if self._check_for_trust_browser_page(driver):
        trust_browser_button = driver.find_element(By.ID, 'trust-browser-button')
        trust_browser_button.click()
        self.human_sleep()
cookies = driver.get_cookies()
for cookie in cookies:
    self.session.cookies.set(cookie["name"], cookie["value"])

Integration

This subtopic forms the foundational authentication mechanism within the broader **Automated Qualtrics Login** main topic. It enables secure access by simulating a full login flow that cannot be done with simple API calls alone. Once the automated login completes and session cookies are harvested, these cookies authenticate subsequent API requests made by other subtopics such as **Survey Collaboration Management** and **Session and API Request Management**.

The Selenium Login Automation tightly couples with the human interaction simulation subtopic by implementing the realistic typing and pause behaviors that improve login success rates and reduce detection risk. It also works in concert with the two-factor authentication handling subtopic by detecting and responding to 2FA and trust prompts during the login flow.

By abstracting the browser-based login into a method (`_selenium_login`), it cleanly integrates with the rest of the `CollaborationClient` class, allowing downstream methods to rely on a valid authenticated session for collaboration management operations.


Sequence Diagram: Selenium Login Flow

sequenceDiagram
    participant Client as CollaborationClient
    participant Browser as Selenium WebDriver
    participant Qualtrics as Qualtrics Login UI

    Client->>Browser: Launch headless Chrome with options
    Browser->>Qualtrics: Navigate to login page
    Qualtrics-->>Browser: Load username input
    Browser->>Browser: Wait and human-type username
    Qualtrics-->>Browser: Load password input
    Browser->>Browser: Wait and human-type password
    Browser->>Qualtrics: Click login button
    Qualtrics->>Browser: Show 2FA or trust prompt (if any)
    Browser->>Browser: Detect and click trust browser button
    loop Wait for login completion
        Browser->>Qualtrics: Poll for authenticated page elements
    end
    Browser->>Client: Extract cookies
    Browser->>Browser: Close browser

This detailed automation of the login process enables seamless, programmatic authentication necessary for all subsequent Qualtrics API interactions within the project.