So I am trying to catch the Webdriver exception and don't want its traceback to pollute my logs. Here is the bit of 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()
yet I'm still getting these:
<full traceback here>
selenium.common.exceptions.WebDriverException: Message: Can not connect to GhostDriver
What am I doing wrong?
The exception is raised outside of your try/except
block while initializing a WebDriver
instance:
driver = webdriver.PhantomJS()
FYI, this happens while starting PhantomJS
with GhostDriver
, quote from the source code:
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, start()
is called in WebDriver
's constructor (__init__()
method).
In other words, it starts a service, but cannot connect to it.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments