-1

I'm using selenium for web scraping, and sometimes, on the website unexpected alerts appear, which breaks the script.
Alert is one type of error; sometimes, the page didn't get loaded properly, and selenium could not find an element due to server performance issues.

So I'm thinking of adding a try/except block so that whenever selenium through an error, it gets captured, and the script gets re-run. But I'm able to catch errors specific to selenium. How can I catch errors thrown by only selenium?

Sourabh
  • 19
  • 5

1 Answers1

0

Perhaps take a loot at the Selenium exceptions here?https://www.browserstack.com/guide/exceptions-in-selenium-webdriver

This might also be relevant org.openqa.selenium.UnhandledAlertException: unexpected alert open

Here are 10 common exceptions for reference

  • ElementNotVisibleException: In spite of the element being present in the DOM, it is not visible (can not be interactive). For example, elements defined in HTML with type =”hidden”

  • ElementNotSelectableException: An element is disabled (can not be clicked/selected) in spite of being present in the DOM

  • NoSuchElementException: Webdriver is not able to determine the elements during runtime, i.e., the FindBy method cannot find a particular component

  • NoSuchFrameException: Webdriver attempts to switch to an invalid frame, which is unavailable

  • NoAlertPresentException: Webdriver is trying to switch to an invalid alert, which is unavailable

  • NoSuchWindowException: Webdriver is trying to switch to an invalid window, which is unavailable

  • StaleElementReferenceException: The referenced element is no longer present on the DOM page (a reference to a component is now Stale). For example, the item belongs to a different frame than the current one or the user has navigated away to another page

  • SessionNotFoundException: Webdriver is acting immediately after ‘quitting’ the browser

  • TimeoutException: The command did not complete in the specified time. For example, the element didn’t display at the specified time. This is especially encountered when working with waits

  • WebDriverException: Webdriver is acting immediately after ‘closing’ the browser

  • Thanks, I looked at these exceptions, but I don't want to add a new except statement for every exceptions selenium throws. I am looking for something generic like adding one except statement, and all selenium related exceptions get captured. – Sourabh Jan 31 '22 at 04:21