【问题标题】:get http status code python selenium获取http状态码python selenium
【发布时间】:2018-10-28 17:02:47
【问题描述】:

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

from urllib.request import *

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

【问题讨论】:

标签: python selenium webdriver http-status-codes


【解决方案1】:

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

1 : 检查内容

self.assertFalse("<insert here a string which is only in the error page>" in self.driver.page_source)

2 : 使用 Django 测试客户端

response = self.client.get(link)
self.assertEqual(response.status_code,200)

【讨论】:

  • 好的,谢谢,实际上我无法让 django 工作...设置有一些问题
  • 如果你需要一些帮助,我就在那里。
【解决方案2】:

编辑了您的示例。 尝试使用 requests 库。

from requests import get

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

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2013-08-31
    • 1970-01-01
    • 2011-10-18
    • 2013-04-04
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2021-03-15
    相关资源
    最近更新 更多