How to Handle Google Authenticator (2FA) in Selenium Automation

Automating web tasks with Selenium is powerful—but things get tricky when 2FA (Two-Factor Authentication) comes into play. I recently faced this challenge while trying to automate file downloads from Amazon Seller Central, which requires Google Authenticator for 2FA.

Let’s break down the problem, my approach, and the best ways to handle such cases.

The Use Case: Automating Excel Downloads from Amazon Seller Central

My goal was simple: automate the download of 500–1000 Excel files from this URL:

perlCopyEdithttps://sellercentral.amazon.de/listing/download?ref=ag_dnldinv_apvu_newapvu

But here’s the challenge:

  • You must be logged into Amazon Seller Central.
  • It enforces 2FA via Google Authenticator.
  • Selenium launches a new browser session, which doesn’t share your login state.
  • Manual login isn’t feasible for every run.

The Problem: Session Isolation in Selenium

When Selenium launches a browser (e.g., Firefox), it uses a fresh profile by default. So even if you’re logged into Amazon in your regular browser, the Selenium-controlled one knows nothing about it—no cookies, no session, nothing.

You might have thought: “Can I just log in manually once, then reuse that session?” Good thought, but by default, Selenium won’t share it.


Solution 1: Reuse an Existing Logged-in Firefox Profile

This is the simplest and cleanest approach when dealing with 2FA:

Step-by-step:

  1. Log in manually to your Amazon Seller account (with 2FA).
  2. Use the same Firefox profile with Selenium.

Code Example:

pythonCopyEditfrom selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.set_preference('profile', '/Users/your-username/Library/Application Support/Firefox/Profiles/your-profile-folder.default-release')

driver = webdriver.Firefox(options=options)
driver.get("https://sellercentral.amazon.de/listing/download?ref=ag_dnldinv_apvu_newapvu")

To find your Firefox profile folder:

  • Open about:profiles in Firefox.
  • Look for the one marked as “default” or “default-release”.

This method keeps you logged in across browser launches—as long as the session/cookies are valid.


Gotchas:

  • Don’t use your primary browser profile in production scripts—copy it instead.
  • Avoid running multiple Selenium sessions on the same profile simultaneously.

Solution 2: Use Cookies After Manual Login

You can manually log in once, grab the session cookies, and reuse them in your Selenium script.

Step 1: Login and export cookies manually

Use something like this script:

pythonCopyEditimport pickle
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://sellercentral.amazon.de")

# Wait and log in manually (including 2FA)

input("Press Enter after login...")

cookies = driver.get_cookies()
with open("amazon_cookies.pkl", "wb") as f:
    pickle.dump(cookies, f)

Step 2: Use those cookies in your automation script

pythonCopyEditimport pickle
from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://sellercentral.amazon.de")

with open("amazon_cookies.pkl", "rb") as f:
    cookies = pickle.load(f)

for cookie in cookies:
    driver.add_cookie(cookie)

driver.get("https://sellercentral.amazon.de/listing/download?ref=ag_dnldinv_apvu_newapvu")

This approach is browser-agnostic and works with Chrome or Edge too.


Bonus Tip: Automating OTPs with TOTP Libraries (Advanced)

If you own the Google Authenticator secret key (which generates the 2FA codes), you can use a Python library like pyotp:

pythonCopyEditimport pyotp
totp = pyotp.TOTP("YOUR_GOOGLE_AUTHENTICATOR_SECRET")
print("Current OTP:", totp.now())

Then, insert that OTP into the web page with Selenium:

pythonCopyEditdriver.find_element("id", "auth-mfa-otpcode").send_keys(totp.now())

But this only works if you can extract the OTP secret (usually when setting up the 2FA QR code).


Conclusion

Selenium + 2FA is a challenge, but not impossible. In most real-world cases:

  • Reusing a logged-in browser profile is the fastest solution.
  • Cookie exporting is great for cross-browser setups.
  • For advanced automation, pyotp and QR extraction might help—if you have access.

Previous Article

How Tech Shapes the Future of Work in 2024

Next Article

Fixing "Unknown Command" Error in Selenium When Uploading a File (Python)

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨