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 not new and it is the process of responding to the occurrence of exceptions that may happen during execution of our program.

For example, when we are trying to click on an element which is not at all displayed on the browser, will cause our program to throw exception altering the expected flow.

Addition to the flow alteration, exceptions can crash the system and forbid next valid statements to get executed.

The exception classes are available in selenium.common.exceptions

List of Exceptions in Selenium python

  1. ElementClickInterceptedException : Indicates that a click could not be properly executed because the target element was obscured in some way, I never faced this exception in my testing life.
  2. 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 :

    1. 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();
    									
  3. ElementNotSelectableException : ElementNotSelectableException is related with Select in selenium, when user tries o select a option which is not selectable..
  4. ElementNotVisibleException : Selenium throws ElementNotVisibleException when user tries to perform an operation on an web element, which is present in the page but the element is currently not visible.
    Reasons :

    1. Clicking an element which is not visible
    2. sendkeys to element which is not visible
    3. Element may not be visible because there are more elements under different pages, but those page source code is still present in 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();
    									
  5. 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.
  6. ImeActivationFailedException : Indicates that activating an IME engine has failed.
  7. ImeNotAvailableException : Indicates that IME support is not available. This exception is thrown for every IME-related method call if IME support is not available on the machine.
  8. InsecureCertificateException : Navigation caused the user agent to hit a certificate warning, which is usually the result of an expired or invalid TLS certificate.
  9. InvalidArgumentException : The arguments passed to a command are either invalid or malformed.
  10. InvalidCookieDomainException : webdriver throws InvalidCookieDomainException when user attempts to add a cookie under a different domain (url) than the current URL.
  11. InvalidCoordinatesException : Selenium throws InvalidCoordinatesException when user tries to move the mouse to a co-ordinate which is not valid using action class methods.
    Reasons :

    1. When we run our test in different size of monitors, we may face this issue.
  12. InvalidElementStateException : Selenium Webdriver throws InvalidElementStateException when user tries to perform operation on a Webelement whose state is disabled.

    Subclasses of this Exception gives detail explanations, the subclasses are ElementNotInteractableException, ElementNotSelectableException

    Reasons :

    1. 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();
    									
  13. 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 a xpath expression) or the expression does not select WebElements (e.g. “count(//input)?).
  14. InvalidSessionIdException : Occurs if the given session id is not in the list of active sessions, meaning the session either does not exist or that it’s not active.
  15. InvalidSwitchToTargetException : Thrown when frame or window target to be switched doesn’t exist.
  16. JavascriptException :
  17. MoveTargetOutOfBoundsException : Selenium throws MoveTargetOutOfBoundsException, when user tries to move/drag the element or the cursor outside the visible screen.
    Reasons :

    1. 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();
    									
  18. NoAlertPresentException : Selenium throws NoAlertPresentException exception when user tries to access a 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 user tries to access alert which is not present in the at current time
    • When javascript is blocked in the browser
    • Not giving enough time for the alert to load
    • When alert is already closed


    Code which causes the NoAlertPresentException :

    
    									driver.switch_to_alert()
    									
  19. NoSuchAttributeException : Thrown when the attribute of element could not be found. You may want to check if the attribute exists in the particular browser you are testing against. Some browsers may have different property names for the same property. (IE8’s .innerText vs. Firefox .textContent)
  20. NoSuchCookieException : No cookie matching the given path name was found amongst the associated cookies of the current browsing context’s active document.
  21. NoSuchElementException : Webdriver throws NoSuchElementException when there is no matching element present in 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 obtain NoSuchElementException
    • The page is still being rendered but you've already finished your element search because of low wait time.
    • The element is not on the page at all

    Code which causes the NoSuchElementException :

    
    									driver.find_element_by_xpath("//label/span")
    									


    Exception Message:org.openqa.selenium.NoSuchElementException: Unable to locate element: //label/span
  22. NoSuchFrameException : NoSuchFrameException happens when user tries to switch to a frame which is not present at the time.

    Reasons : NoSuchFrameException occurs because of one of the below reasons

    1. There is no frame with given details, or the details changed during the page loading time
    2. Target Frame is not actually a frame, if the locators matches with exactly with another element( as first instance).
    3. We have not given enough wait time to load the frame.


    Code which causes the NoSuchFrameException :

    
    									driver.switch_to_frame("frame1");
    									
  23. NoSuchWindowException : selenium webdriver throws NoSuchWindowException when user tries to switch to or perform an operation on browser windows when the window is not present.

    Reasons : NoSuchWindowException occurs because of one of the below reasons

    1. Current window closed
    2. Target window is not opened yet, but you were trying to perform operation
    3. When target window has been closed but still the code tried to perform operation on the target window.


    Code which causes the NoSuchWindowException :

    
    									driver.switch_to_window("window Gu ID");
    									
  24. RemoteDriverServerException :
  25. ScreenshotException : ScreenshotException exception will be thrown when selenium fails to take screenshot of the webpage. If ScreenshotException occurs then the screenshot captured turns black

    Reasons :

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


    Code which causes the ScreenshotException :

    
    									TakesScreenshot screenShot =((TakesScreenshot) driver);
    									
  26. SessionNotCreatedException : A new session could not be created.
  27. StaleElementReferenceException :
  28. TimeoutException : TimeoutException is related with explicitwait, fluentwait, pageLoadTimeOut, scriptLoadTimeout. Selenium webdriver throws the TimeoutException exception when user has defined the wait time but the element have not been found within the wait time.

    Reasons : TimeoutException occurs because of one of the below reasons

    • Element is not present or given Condition is not met
    • When the given timeout is very low
    • When 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")));
    									
  29. UnableToSetCookieException : Webdriver throws UnableToSetCookieException when a driver fails to set a cookie.<
  30. UnexpectedAlertPresentException : Thrown when an unexpected alert is appeared. Usually raised when when an expected modal is blocking webdriver form executing any more commands.
  31. UnexpectedTagNameException : UnexpectedTagNameException occurs when user is using Select Class in selenium to perform action on the dropdown/element where the element is not formed with select html tag.
    Reasons :

    1. Dropdown is formed without using select tag name.
    2. Html code might have changed to non-select tag after writing the test automation code.
    3. If dropdown html code is specific to 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'))
    									
  32. UnknownMethodException : The requested command matched a known URL but did not match an method for that URL.
  33. WebDriverException : WebDriverException is the top most exception class in the selenium webdriver, it extends RuntimeException class. All selenium Exceptions are subclasses of WebDriverException class.

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
 
Join My Facebook Group
Join Group