This question is just another form of one that comes up pretty often: "How can I get past a CAPTCHA or 2FA using automation tools?"
First, you can't! This would be a violation of CAPTCHA and 2FA! If you can get past it using Selenium or other like tools, so can any nefarious actor for illegitimate reasons!
The best option for you to do is to disable CAPTCHA or 2FA in your Dev and Test environments. This gives you control over your testing. Remember that not every test needs to be automated, and this is one of those use cases!
Now, let's look at the Selenium documentation on this topic! They have page full of discouraged practices which include CAPTCHAs, 2FAs, and logins to web-based email/social media sites
I recommend reading those links. while this comes from Selenium, it applies to any similar UI test tools like Cypress, Playwright, etc!
To sum them up:
CAPTCHA, short for Completely Automated Public Turing test to tell
Computers and Humans Apart, is explicitly designed to prevent
automation, so do not try! There are two primary strategies to get
around CAPTCHA checks:
- Disable CAPTCHAs in your test environment
- Add a hook to allow tests to bypass the CAPTCHA
Automating this seamlessly and consistently is a big challenge in
Selenium. There are some ways to automate this process. But that will
be another layer on top of our Selenium tests and not as secure. So,
you should avoid automating 2FA.
There are a few options to get around 2FA checks:
- Disable 2FA for certain Users in the test environment, so that you can
use those user credentials in the automation.
- Disable 2FA in your test environment.
- Disable 2FA if you login from certain IPs. That way we can configure our test machine IPs to avoid this.
Logging into sites like Gmail and Facebook using WebDriver is not
recommended. Aside from being against the usage terms for these sites
(where you risk having the account shut down), it is slow and
The ideal practice is to use the APIs that email providers offer, or
in the case of Facebook the developer tools service which exposes an
API for creating test accounts, friends, and so forth. Although using
an API might seem like a bit of extra hard work, you will be paid back
in speed, reliability, and stability. The API is also unlikely to
change, whereas webpages and HTML locators change often and require
you to update your test framework.
Based on your question, you'll need to use the API from an email provider to get access to an email message.