获取http状态代码python selenium

获取http状态代码python selenium,python,selenium,webdriver,http-status-codes,Python,Selenium,Webdriver,Http Status Codes,如果打开链接,如何获取python中的http状态代码? 我这样做的,但它总是显示200,即使页面抛出404错误 from urllib.request import * self.driver.get(link) code = urlopen(link).getcode() if not code == 200: print('http status: ' + str(code)) 这就是问题所在,Selenium总是能找到一个页面。有两种方法可以检查加载是否无效: 1:检查内容 s

如果打开链接,如何获取python中的http状态代码? 我这样做的,但它总是显示200,即使页面抛出404错误

from urllib.request import *

self.driver.get(link)
code = urlopen(link).getcode()
if not code == 200: 
  print('http status: ' + str(code))

这就是问题所在,Selenium总是能找到一个页面。有两种方法可以检查加载是否无效:

1:检查内容

self.assertFalse("<insert here a string which is only in the error page>" in self.driver.page_source)
编辑您的示例。 尝试使用请求库

from requests import get

self.driver.get(link)
request = get(link)
if not request.status_code == 200:
  print('http status: ' + str(request.status_code))

希望,这会对您有所帮助。

可能会很简单好,谢谢,实际上我无法让django工作……它的设置有一些问题。如果您需要帮助,我就在那里。
from requests import get

self.driver.get(link)
request = get(link)
if not request.status_code == 200:
  print('http status: ' + str(request.status_code))