0% found this document useful (0 votes)
10 views6 pages

Common Selenium Exceptions & Fixes

Deepak Kumar Gupta discusses various exceptions encountered during automation testing with Selenium WebDriver and Java, including NoSuchElementException, StaleElementReferenceException, and TimeoutException. He outlines the reasons for these exceptions and the strategies used to handle them, such as using WebDriverWait, retry logic, and proper error handling techniques. Additionally, he emphasizes the importance of proactive exception management and maintaining utility classes for improved testing efficiency.

Uploaded by

Er deepak Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Common Selenium Exceptions & Fixes

Deepak Kumar Gupta discusses various exceptions encountered during automation testing with Selenium WebDriver and Java, including NoSuchElementException, StaleElementReferenceException, and TimeoutException. He outlines the reasons for these exceptions and the strategies used to handle them, such as using WebDriverWait, retry logic, and proper error handling techniques. Additionally, he emphasizes the importance of proactive exception management and maintaining utility classes for improved testing efficiency.

Uploaded by

Er deepak Gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question:

What types of exceptions have you faced, and how did you handle
them?

Answer (Deepak Kumar Gupta):

During my automation testing experience using Selenium WebDriver


with Java, I've encountered various exceptions. Here are some of the
most common ones, along with reasons and how I overcame them:

1. NoSuchElementException

Reason:
This occurs when Selenium is unable to locate an element on the page
using the provided locator.

Real Case:
In our HRMS platform, some elements took longer to load due to API
delays or JavaScript rendering.

How I handled it:

 Used WebDriverWait with ExpectedConditions (like


presenceOfElementLocated or visibilityOfElementLocated)

 Verified that the locator was correct and stable across different
environments

 Added retry logic in custom utility functions

2. StaleElementReferenceException

Reason:
Occurs when an element is no longer attached to the DOM, typically after
page refreshes or dynamic updates (AJAX, JS frameworks).

Real Case:
During automation of dynamic dropdown selections, the DOM was
reloaded after choosing a filter.

Fix:

 Re-located the element after page updates

 Used a combination of
[Link]([Link](...)) and then re-fetched
the element
3. ElementClickInterceptedException

Reason:
Selenium tries to click an element, but another element (like loader,
tooltip, or modal) overlaps it.

Fix:

 Added waits to ensure modals/loaders were hidden

 Used JavascriptExecutor to perform the click as a workaround:

((JavascriptExecutor) driver).executeScript("arguments[0].click();",
element);

4. TimeoutException

Reason:
Element didn't appear or condition didn’t meet within the specified wait
time.

Fix:

 Optimized waits using explicit waits instead of implicit waits

 Adjusted timeouts based on environment performance (QA vs


staging vs production)

5. Database-related Exceptions (SQLException)

Scenario:
While performing backend validations using JDBC in automation scripts, I
sometimes got syntax errors or null pointer exceptions.

Fix:

 Wrapped DB code with proper try-catch and logging

 Verified SQL syntax before execution

 Used parameterized queries to avoid SQL injection and handle


dynamic values properly

6. NullPointerException
Reason:
Object not initialized before use — very common in Java.

Fix:

 Added null checks before operations

 Used logging to trace object assignments

 Refactored code to ensure proper constructor and object setup

Bonus Tip (Interviewer Value Add):

"I also maintain utility classes for waits, exception logging, and recovery
steps — so that exception handling is not just reactive, but proactively
built into the framework."

Would you like me to convert this into a short mock answer for a spoken
interview format?

You said:

some more

ChatGPT said:

Absolutely — here's a continued list of real-world exceptions you


might face as a Selenium/Java automation tester along with
advanced causes, fixes, and practical examples. These will help
show depth in your experience during interviews.

✅ 7. WebDriverException

Reason:
General error that occurs when WebDriver is unable to perform an action
due to driver/browser mismatch or improper session handling.

Real Case:

 I faced this when trying to run scripts in headless mode using an


outdated ChromeDriver version.

Fix:

 Ensured browser and WebDriver versions are always compatible

 Wrapped such actions in a retry mechanism


 Updated [Link] via automation if needed

✅ 8. InterruptedException (mostly in waits or multi-threading)

Reason:
Thrown when a thread is waiting, sleeping, or otherwise occupied, and
gets interrupted.

Fix:

 Used [Link]() carefully or replaced it with WebDriverWait

 Managed interruptible tasks in try-catch blocks

✅ 9. IllegalArgumentException

Reason:
Passed incorrect arguments to WebDriver API — e.g., incorrect locator, null
values, invalid file path during upload.

Fix:

 Always validated inputs before passing them to methods

 Created custom assertions for preconditions

✅ 10. FileNotFoundException

Context: File handling (e.g., uploading CSV/Excel during automation)

Fix:

 Ensured relative file paths were used (so that they work across
environments)

 Added checks using [Link]() before upload actions

✅ 11. SessionNotCreatedException

Reason:
Mismatch between Chrome version and chromedriver version, or trying to
start a session after [Link]()

Fix:

 Maintained version control of drivers


 Used WebDriverManager (for automatic driver management)

✅ 12. UnhandledAlertException

Reason:
Unexpected browser alert/dialog appears (JS alert, confirmation, etc.) and
disrupts WebDriver’s control.

Real Case:
A confirmation popup appeared after form submission in the finance
module.

Fix:

 Used [Link]().alert().accept()

 Added alert-handling logic before click/submit actions

✅ 13. AssertionError (from TestNG or JUnit assertions)

Reason:
Assertion fails when actual != expected.

Fix:

 Improved assertion messages for clarity

 Logged actual values before asserting to debug more easily

 Used soft assertions in places where test shouldn't immediately fail

✅ 14. NullPointerException in Page Object Model (POM)

Real Case:
WebElement not initialized via @FindBy, or PageFactory not used correctly.

Fix:

 Always called [Link](driver, this) in constructors

 Checked that objects were initialized before use

✅ 15. TimeoutException in CI/CD pipeline

Real Case:
Automation suite ran slower on Jenkins than locally, leading to timeouts.
Fix:

 Increased timeout thresholds in CI

 Reduced suite execution time by tagging fast/slow tests and running


them in parallel

✅ Bonus: Handling Non-Exceptions (Flaky Failures)

Even when no actual exception is thrown, flaky failures due to:

 Slow loading

 AJAX re-rendering

 Captchas

 Intermittent popups
are common in real projects.

Approach:

 Used custom wait conditions

 Wrapped retry logic with counters

 Logged screenshots and page source on failure for analysis

🧠 Pro Tip (for Interview):

“In addition to handling exceptions, I actively review test run logs, take
screenshots on failures, and include exception types in automation reports
— so failures are traceable and fixable.”

You might also like