[英]How to catch exception info for the wrong url when to open it with selenium?
在Firefox或其他浏览器中输入http://www.python.or/
(故意使用错误的URL)时,浏览器显示如下内容:
连接被重置
The connection to the server was reset while the page was loading.
The site could be temporarily unavailable or too busy. Try again in a few moments.
If you are unable to load any pages, check your computer’s network connection.
If your computer or network is protected by a firewall or proxy, make sure that Firefox is permitted to access the
现在,让我们对硒执行相同的任务。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("--headless")
browser = webdriver.Chrome(chrome_options=chrome_options)
browser.get("http://www.python.or")
当您在控制台中执行以上代码时,没有错误信息,如何捕获诸如Firefox之类的异常与硒?
我建议您尝试HTML请求
首先,从URL获取请求:
import requests r = requests.get(your_url)
现在,您需要从请求中获取status_code :
print(r.status_code)
r.status_code包含http的错误处理程序。
这是所有HTTP状态代码的列表
尝试此操作,您会知道何时发生了某些错误( 例如错误404)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.