3

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))
1

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

ok thanks, actually I can't get django work...it has some issues with the settings
If you need some help about it i'm there.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.