seleniumpythonpagedown-未知错误:net::ERR_NAME_NOT_RESOLVED
所以我目前正在开发一个 python 抓取工具,用 python 中的 selenium 收集网站信息。我遇到的问题是,如果我前往一个不活跃的页面,我会收到错误消息:unknown error: net::ERR_NAME_NOT_RESOLVED
我有一段时间没有使用 python,所以我的知识不是最好的。
这是我的代码
driver = webdriver.Chrome(ChromeDriverManager().install())
try:
driver.get('%s' %link)
except ERR_NAME_NOT_RESOLVED:
print ("page down")
示例网站:http : //www.whitefoxkeeping.co.uk
错误
Traceback (most recent call last):
File "C:UsersSTEAppDataLocalProgramsPythonPython39selenium emailselenium test.py", line 106, in <module>
driver.get('%s' %url)
File "C:UsersSTEAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagesseleniumwebdriverremotewebdriver.py", line 333, in get
self.execute(Command.GET, {'url': url})
File "C:UsersSTEAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagesseleniumwebdriverremotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:UsersSTEAppDataLocalPackagesPythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0LocalCachelocal-packagesPython38site-packagesseleniumwebdriverremoteerrorhandler.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)
我已经搜索了以前的问题,似乎无法找到解决方案。
任何帮助将不胜感激:)
回答
如果您想捕获错误,您必须将您的更改except为 selenium 为您提供的内容。在这种情况下:selenium.common.exceptions.WebDriverException。
所以首先导入它:
from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import WebDriverException
然后你可以捕捉:
THE END
二维码