Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get http status code python selenium

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))
like image 557
marshmallow Avatar asked Jun 21 '26 19:06

marshmallow


1 Answers

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)
like image 150
AntoineLB Avatar answered Jun 23 '26 19:06

AntoineLB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!