Exception in Selenium Python

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.

Locate element by id

List of Exceptions in Selenium python

ElementClickInterceptedException :

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 :

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.

Reasons :

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 :

ElementNotSelectableException is related with Select in selenium when the user tries o select an option that is not selectable.

ElementNotVisibleException :

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.

Reasons :

  1. Clicking an element that is not visible
  2. sendkeys to element which is not visible
  3. The element may not be visible because there are more elements under different pages, but that page source code is still present in the cache.

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();
ErrorInResponseException :

Thrown when an error has occurred on the server-side. This may happen when communicating with the firefox extension or the remote driver server.

InvalidArgumentException :

The arguments passed to a command are either invalid or malformed.

InvalidCoordinatesException :

Selenium throws InvalidCoordinatesException when a user tries to move the mouse to a co-ordinate which is not valid using action class methods.

Reasons :

  • When we run our test in different sizes of monitors, we may face this issue.
InvalidElementStateException :

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.

Reasons :

  • When element state is disabled

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();
InvalidSelectorException :

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").

InvalidSwitchToTargetException :

Thrown, when frame or window target to be switched, doesn’t exist.

MoveTargetOutOfBoundsException :

Selenium bindings throw MoveTargetOutOfBoundsException when the user tries to move/drag the element or the cursor outside the visible screen.

Reasons :

  • Change of screen size ( test cases are written in desktop, but current execution is happening laptop, considering that desktop screen size is bigger)

Code which causes the MoveTargetOutOfBoundsException :

actions = new Actions(driver);
actions.mouseMove( 10, 25 )
actions.perform();
NoAlertPresentException :

Selenium throws a NoAlertPresentException exception when a user tries to access an alert popup, which is currently not present. Here Alert popup could be Alert Box, Confirmation Box, Prompt Box from the Javascript commands.

Reasons :

  • NoAlertPresentException occurs because of one of the below reasons
  • When a user tries to access an alert that is not present in at the current time.
  • When javascript is blocked in the browser
  • Not giving enough time for the alert to load
  • When an alert is already closed

Code which causes the NoAlertPresentException :

driver.switch_to_alert()
NoSuchElementException :

Webdriver throws NoSuchElementException when there is no matching element present on the webpage.

Reasons :

  • NoSuchElementException occurs because of one of the below reasons
  • Either Xpath you specified for the element is wrong.
  • The second reason is AJAX has not returned yet, and you've already obtained NoSuchElementException.
  • The page is still being rendered, but you've already finished your element search because of a low wait time.
  • The element is not on the page at all

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 :

NoSuchFrameException happens when a user tries to switch to a frame that is not present at the time.

Reasons :

  • NoSuchFrameException occurs because of one of the below reasons.
  • There is no frame with given details, or the details changed during the page loading time.
  • Target Frame is not a frame if the locators match exactly with another element( as the first instance).
  • We have not given enough wait time to load the frame.

Code which causes the NoSuchFrameException :

driver.switch_to_frame("frame1");
NoSuchWindowException :

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.

Reasons :

  • The current window closed.
  • The target window is not opened yet, but you were trying to perform the operation.
  • When the target window has been closed, but still the code attempted to operate the target window.

Code which causes the NoSuchWindowException :

driver.switch_to_window("window Gu ID")
ScreenshotException :

ScreenshotException exception will be thrown when selenium fails to take a screenshot of the webpage. If ScreenshotException occurs then the screenshot captured turns black.

Reasons :

  • When we try to take a screenshot in the headless browser(phantomjs, htmlunitdriver) without enabling the screenshot functionality.

Code which causes the ScreenshotException :

TakesScreenshot screenShot =((TakesScreenshot) driver);
TimeoutException :

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.

Reasons :

  • Element is not present or given Condition is not met.
  • When the given timeout is very low. When the page takes more time to load. When executeAsyncScript() takes more time to return its operation

Code which causes the TimeoutException :

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement idElement;
idElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("123")));
UnexpectedAlertPresentException :

Thrown when an unexpected alert has appeared. Usually raised when an expected modal is blocking webdriver form executing any more commands.

UnexpectedTagNameException :

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.

Reasons :

  • The dropdown is formed without using the select tag name.
  • The HTML code might have changed to a non-select tag after writing the test automation code.
  • If dropdown HTML code is specific to the browser used, Sometimes code can be rendered based on the browser.

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'))
About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions