Selenium: Strategies for Dealing with Access Denied Pages

Apr 2, 2024 ยท 3 min read

As a test automation engineer using Selenium, one of the most frustrating issues you may encounter is your test running into an access denied page. This usually occurs when your test tries to access a part of the application that requires login or other credentials. When an access denied page appears, it breaks the flow of your test and leads to failures.

There are several strategies you can employ to avoid and handle access denied pages in your Selenium tests:

Log Into the Application Before Testing

The simplest solution is to have your test log into the application at the start, before attempting any other actions. This will ensure your tests run in an authenticated state and can access restricted pages:

// Log into application 

// Continue with testing steps...

Logging in upfront eliminates many access denied errors.

Check for Access Denied Pages

Even when logged in, some parts of an application may have additional access restrictions. Your test may unintentionally navigate to such pages.

A best practice is to regularly check for access denied pages as your test runs:

// Perform test step

// Check if access was denied
if(driver.getPageSource().contains("Access denied")) {

  // Handle access denied page
  // ...

} else {

  // Continue testing


This allows your test to detect access issues and handle them appropriately.

Refresh Tokens to Maintain Session

For applications with strict session timeouts, you may encounter access issues if your test runs too long. The session and credentials may expire mid-test.

You can keep the session alive by periodically refreshing tokens:

// Log in 

// Save auth token 
String authToken = driver.manage().getCookieNamed("authToken").getValue();

// Later in test...

// Check if session expired
if(driver.getCurrentUrl().contains("sessionExpired")) {

  // Refresh session
  driver.manage().addCookie(new Cookie("authToken", authToken));


This reuses the old auth token to refresh the session, avoiding denied pages.

Handle Access Denied Pages

Sometimes access issues are hard to prevent entirely. When your test does hit a denied page, you want to handle it gracefully.

Here are some approaches:

Log and continue: Record the failure, then redirect the test to a valid page to continue other steps:

if(driver.getPageSource().contains("Access denied")) {

  System.out.println("Hit access denied page");


Retry with alternate credentials: Try logging in with other user accounts that may have required access:


Report issues: Raise defects highlighting unintentional access restrictions that need resolution:

System.out.println("Section restricts access for valid user - DEFECT");

With robust access denied handling, your tests can recover and provide useful feedback.

Key Takeaways

Here are some key tips for dealing with access issues in Selenium:

  • Log into the application upfront before testing
  • Actively check for access denied pages
  • Keep sessions alive by refreshing tokens
  • Handle denied pages by logging, retrying or reporting
  • Identify and resolve invalid restrictions causing failures
  • By anticipating and managing access issues, you can create reliable Selenium tests.

    Focusing on robust authentication, session handling, and failure strategies will help your test automation smoothly drive applications with strict access controls.

    Browse by language:

    The easiest way to do Web Scraping

    Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you

    Try ProxiesAPI for free

    curl ""

    <!doctype html>
        <title>Example Domain</title>
        <meta charset="utf-8" />
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />


    Don't leave just yet!

    Enter your email below to claim your free API key: