An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.
Exception handling is the process of responding to the occurrence of exceptions that may happen during the execution of our program.
For example, when we are trying to click on an element that is not at all displayed on the browser, it will cause our program to throw an exception, altering the expected flow.
In addition to the flow alteration, exceptions can crash the system and forbid the next valid statements to get executed. The exception classes are available in selenium.common.exceptions.
ElementClickInterceptedException occurs when a click could not be appropriately executed because the target element was obscured in some way, I never faced this exception in my testing life.
ElementNotInteractableException is throws when we try to perform operation on an element which not intereactable at the moment. ElementNotInteractableException has two subclasses which are ElementNotVisibleException, ElementClickInterceptedException.
When element size is 0 x 0 (width =0 and height =0). Code which causes the ElementNotInteractableException :
<input type='button' height=0px width=0px value='save'> // click the button which has width=0 and height=0 driver.find_element_by_xpath("//input[@type='button']")).click();
ElementNotSelectableException is related with Select in selenium when the user tries o select an option that is not selectable.
Selenium throws ElementNotVisibleException when a user tries to perform an operation on a web element, which is present on the page but the element is currently not visible.
Code which causes the ElementNotVisibleException :
<input type='button' hidden=true value='save'> // click the button which is hidden (not visible) driver.find_element_by_xpath("//input[@type='button']")).click();
Thrown when an error has occurred on the server-side. This may happen when communicating with the firefox extension or the remote driver server.
The arguments passed to a command are either invalid or malformed.
Selenium throws InvalidCoordinatesException when a user tries to move the mouse to a co-ordinate which is not valid using action class methods.
Selenium python throws InvalidElementStateException when the user tries to perform an operation on a Webelement whose state is disabled.
Subclasses of this Exception gives detail explanations, the subclasses are ElementNotInteractableException, ElementNotSelectableException.
Code which causes the InvalidElementStateException :
<input type='button' disabled=true value='save'> // click the button which is disabled driver.find_element_by_xpath("//input[@type='button']")).click();
Thrown when the selector which is used to find an element does not return a WebElement. Currently, this only happens when the selector is an XPath expression, and it is either syntactically invalid (i.e., it is not an XPath expression), or the expression does not select WebElements (e.g. "count(//input").
Thrown, when frame or window target to be switched, doesn’t exist.
Selenium bindings throw MoveTargetOutOfBoundsException when the user tries to move/drag the element or the cursor outside the visible screen.
Code which causes the MoveTargetOutOfBoundsException :
actions = new Actions(driver); actions.mouseMove( 10, 25 ) actions.perform();
Code which causes the NoAlertPresentException :
Webdriver throws NoSuchElementException when there is no matching element present on the webpage.
Code which causes the NoSuchElementException :
driver.find_element_by_xpath("//label/span") # the error message <div" class="warn">Exception Message:org.openqa.selenium. NoSuchElementException: Unable to locate element: //label/span
NoSuchFrameException happens when a user tries to switch to a frame that is not present at the time.
Code which causes the NoSuchFrameException :
selenium python bindings throw NoSuchWindowException when a user tries to switch to or perform an operation on browser windows when the window is not present.
Code which causes the NoSuchWindowException :
driver.switch_to_window("window Gu ID")
ScreenshotException exception will be thrown when selenium fails to take a screenshot of the webpage. If ScreenshotException occurs then the screenshot captured turns black.
Code which causes the ScreenshotException :
TakesScreenshot screenShot =((TakesScreenshot) driver);
TimeoutException is related with explicitwait, fluentwait, pageLoadTimeOut, scriptLoadTimeout. Selenium throws the TimeoutException exception when the user has defined the wait time, but the element has not been found within the wait time.
Code which causes the TimeoutException :
WebDriverWait wait = new WebDriverWait(driver, 30); WebElement idElement; idElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("123")));
Thrown when an unexpected alert has appeared. Usually raised when an expected modal is blocking webdriver form executing any more commands.
UnexpectedTagNameException occurs when the user is using Select Class in selenium to perform an action on the dropdown/element where the element is not formed with a select HTML tag.
Code which causes the UnexpectedTagNameException :
// throws the UnexpectedTagNameException dropdown = Select(driver.find_element_by_id('//input')) // this will not throw exception dropdown = Select(driver.find_element_by_id('//select'))
I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.