selenium python page down - unknown error: net::ERR_NAME_NOT_RESOLVED

Viewed 9913

So I'm currently working on a python scraper to collect website info with selenium in python. The issue I'm having is if I head to a page that isn't live I get the error: unknown error: net::ERR_NAME_NOT_RESOLVED

I haven't used python in a while so my knowledge isn't the best.

Here is my code

driver = webdriver.Chrome(ChromeDriverManager().install())
try:
    driver.get('%s' %link)
except ERR_NAME_NOT_RESOLVED:
    print ("page down")

example website: http://www.whitefoxcatering.co.uk

error

Traceback (most recent call last):
  File "C:\Users\STE\AppData\Local\Programs\Python\Python39\selenium email\selenium test.py", line 106, in <module>
    driver.get('%s' %url)
  File "C:\Users\STE\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\remote\webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Users\STE\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\STE\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: net::ERR_NAME_NOT_RESOLVED
  (Session info: chrome=87.0.4280.88)

I've searched through the previous questions and cannot seem to find a solution to this.

Any help would be hugely appreciated :)

1 Answers

If you want to catch the error you have to change your except to what selenium is raising for you. In this case: selenium.common.exceptions.WebDriverException.

So first import it:

from selenium.common.exceptions import WebDriverException

Then you can catch:

try:
    driver.get('http://www.whitefoxcatering.co.uk')
except WebDriverException:
    print("page down")
Related