2

How can I get the http status code in python if I open a link? I did it this way, but it always shows 200 even if the page throws a 404 error...

from urllib.request import *

self.driver.get(link)
code = urlopen(link).getcode()
if not code == 200: 
  print('http status: ' + str(code))
marshmallow
  • 86
  • 1
  • 8
  • posible dublicate https://stackoverflow.com/questions/5799228/how-to-get-status-code-by-using-selenium-py-python-code – Druta Ruslan May 18 '18 at 09:37

2 Answers2

1

That's the problem, Selenium will always find a page. You have 2 ways to check if loading didn't work :

1 : Check content

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

2 : Use Django test Client

response = self.client.get(link)
self.assertEqual(response.status_code,200)
AntoineLB
  • 482
  • 3
  • 19
0

Edited your example. Try to use requests library.

from requests import get

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

Hope, this will help you.

Oleksandr Makarenko
  • 779
  • 1
  • 6
  • 18