Inability to catch an exception

So I'm trying to catch a Webdriver exception and don't want its trace to pollute my logs. Here is some code

from selenium.common.exceptions import TimeoutException, WebDriverException

try:
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, '.loading')))
except TimeoutException:
    log.msg("Seneium Timeout: {}".format(response.url))
except WebDriverException as e:
    log.msg("Selenium Exception: {0} Message: {1}".format("my message", str(e)))
finally:
    driver.quit()

      

while i still get:

 <full traceback here>
 selenium.common.exceptions.WebDriverException: Message: Can not connect to GhostDriver

      

What am I doing wrong?

+3


source to share


1 answer


The exception is thrown outside of your block try/except

when you initialize the instance WebDriver

:

driver = webdriver.PhantomJS()

      


FYI, this happens when run PhantomJS

from GhostDriver

, quote from source :



def start(self):
    """
    Starts PhantomJS with GhostDriver.

    :Exceptions:
     - WebDriverException : Raised either when it can't start the service
       or when it can't connect to the service
    """
    try:
        self.process = subprocess.Popen(self.service_args, stdin=subprocess.PIPE,
                                        close_fds=platform.system() != 'Windows',
                                        stdout=self._log, stderr=self._log)

    except Exception as e:
        raise WebDriverException("Unable to start phantomjs with ghostdriver.", e)
    count = 0
    while not utils.is_connectable(self.port):
        count += 1
        time.sleep(1)
        if count == 30:
             raise WebDriverException("Can not connect to GhostDriver")

      

And it start()

is called in the WebDriver

constructor ( __init__()

)
.

In other words, it starts a service but cannot connect to it.

+6


source







All Articles