=================================================================================
Exceptions are faults or disruptions that occur during the execution of a program/application. Exception handling is crucial for maintaining the natural or normal flow of the application.
Selenium exceptions can be broadly categorized into two types: Checked and Unchecked Exceptions.
Checked exceptions are handled during the coding process itself. Unchecked exceptions occur during run-time and can have a much greater impact on the application flow. We have compiled some of the most common selenium exceptions along with the various ways to handle them.
- WebDriverException
· NoSuchElementException
· NoSuchFrameException
· NoAlertPresentException
· NoSuchWindowException
· ElementNotVisibleException
· ElementNotInteractableException
· SessionNotCreatedExceptio
· TimeoutException
· InvalidSelectorException
· IllegalStateException
. StaleElementReferenceException
One of the most frequent exceptions in Selenium Webdriver, NoSuchWindowException occurs if the current list of windows is not updated. The previous window does not exist, and you can’t switch to it.
To handle this exception, use webdriver methods called “driver.getWindowHandles().”
2. NoSuchFrameException
Similar to NoSuchWindowException, the NoSuchFrameException occurs when switching between multiple frames is not possible.
The solution used for handling NoSuchWindowException must ideally work for this exception too.
3. NoSuchElementException
Happens when the locators are unable to find or access elements on the web page or application. Ideally, the exception occurs due to the use of incorrect element locators in the findElement(By, by) method.
To handle this exception, use the wait command. Use Try/Catch to ensure that the program flow is interrupted if the wait command doesn’t help.
4. NoAlertPresentException
Happens when the user is trying to you switch to an alert which is not present. In simple terms, it means that the test is too quick and is trying to find an alert that has not yet been opened by the browser.
To handle or simply avoid this exception, use explicit or fluent wait in all events where an alert is expected.
5. InvalidSelectorException
This exception occurs due to an incorrect selector. More often than not, the selector syntax is wrong.
To avoid this exception, first, check the locator syntax. If it is incorrect, make sure the locator is syntactically correct.
6. TimeoutException
This exception is thrown if the command did not execute or complete within wait time. As already mentioned, waits are used to avoid NoSuchElementException. However, TimeoutException will be thrown after the page components fail to load within wait time.
Avoiding this exception is simple. All one needs to do is to calculate the average page load time and adjust the wait time accordingly.
7. ElementNotVisibleException
This exception occurs when the WebDriver tries to find an element that is hidden or invisible. To handle this exception, it is essential that the exact reason is identified, which can be due to nested web elements or overlapping web elements.
In the first case, you have to locate and correct the specific element. In the second case, use explicit wait.
8. ElementNotSelectableException
This exception belongs to the same class as InvalidElementStateException. In such exceptions, the element is present on the web page, but the element cannot be selected.
To handle this exception, the wait command must be used.
9. NoSuchSessionException
As the name suggests, the exception is thrown if a method is called post the browser is closed. Other reasons for this exception include a browser crash.
To avoid this handle, ensure that your browser is updated and stable.
10. StaleElementReferenceException
This exception occurs when the web element is no longer part of the web page. The element may have been part of the source code, but it is no longer part of the window. There can be multiple reasons for this exception. It can occur either from a switch to a different page, the element is no longer part of DOM or due to frame/window switch
To handle this exception, you can either use Dynamic Xpath for handling DOM operations or try to use the Page Factory Model or try to access the element in loops before throwing the exception.
1)Stale Element Reference Exception in Selenium Webdriver
Definition:Stale means old, decayed, no longer fresh. Stale Element means an old element or no longer available element. Assume there is an element that is found on a web page referenced as a WebElement in WebDriver. If the DOM changes then the WebElement goes stale. If we try to interact with an element which is staled then the StaleElementReferenceException is thrown.
Causes of Stale Element Reference Exception:
A stale element reference exception is thrown in one of two cases, the first being more common than the second.The two reasons for Stale element reference are- The element has been deleted entirely.
- The element is no longer attached to the DOM.
We face this stale element reference exception when the element we are interacting is destroyed and then recreated again. When this happens the reference of the element in the DOM becomes stale. Hence we are not able to get the reference to the element.How To Overcome Stale Element Reference Exception in Selenium:
Assume you are trying to click on a link and getting the stale element exception.Sample code to overcome the issueSolution 2:If an element is not attached to DOM then you could try using ‘try-catch block’ within ‘for loop’Solution 3:Wait for the element till it gets availableUse ExpectedConditions.refreshed to avoid StaleElementReferenceException and retrieve the element again. This method updates the element by redrawing it and we can access the referenced element.Solution 4:We can handle Stale Element Reference Exception by using POM.
We could avoid StaleElementException using POM. In POM, we use initElements() method which loads the element but it won’t initialize elements. initElements() takes latest address. It initializes during run time when we try to perform any action on an element. This process is also known as Lazy Initialization.Exception5:.Exception in thread "main" org.openqa.selenium. WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: WIN10
Build info: version: '4.0.0', revision: '3a21814679'Solution:1)Install firefox in your system. that will solve the issuestill not solved include below code:The simplest way is to use the below code before you define your Driver.
Method1)System.setProperty("webdriver.firefox.bin",
"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
(OR)
Method2):
System.setProperty("webdriver.gecko.driver","D:\\Workspace\\demoproject\\src\\lib\\geckodriver.exe");
File pathBinary = new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
FirefoxBinary firefoxBinary = new FirefoxBinary(pathBinary);
FirefoxOptions options = new FirefoxOptions();
options.setBinary(firefoxBinary);
WebDriver driver = new FirefoxDriver(options);
driver.get("https://www.google.co.in/");
NextException):
Exception in thread "main" org.openqa.selenium. TimeoutException: Expected condition failed: waiting for title to contain "Dead Poets Society - IMDb". Current title: "Dead Poets Society (1989) - IMDb" (tried for 30 second(s) with 500 milliseconds interval)
Build info: version: '4.0.0', revision: '3a21814679'
when you provide wrong title then it will throw timeoutException
NextException:
Exception in thread "main" org.openqa.selenium. NoSuchSessionException: Tried to run command without establishing a connection
Build info: version: '4.0.0', revision: '3a21814679'
when session is closed with driver.close() then you will get
NoSuchSessionException.
Make sure close() is used outside loops.
----------------------------------------------------------------------------------------
NEXT EXCEPTION:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@href='logout']"}
(Session info: chrome=96.0.4664.45)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.