0

As a newbie, I wonder whether there is a method to get the http response status code to judge some expections, like remote server down, url broken, url redirect, etc...

Matt
  • 47
  • 1
  • 6
  • 1
    Possible duplicate of [How to get HTTP Response Code using Selenium WebDriver](https://stackoverflow.com/questions/6509628/how-to-get-http-response-code-using-selenium-webdriver) – Navarasu Oct 17 '18 at 13:28
  • You need to use another library for it. Check out the requests library in python. – slckayhn Oct 17 '18 at 13:51

1 Answers1

4

In Selenium it's Not Possible!

For more info click here.

You can accomplish it with request:

import requests
from selenium import webdriver

driver = webdriver.get("url")
r = requests.get("url")
print(r.status_code)
Moshe Slavin
  • 4,696
  • 5
  • 18
  • 34
  • Why even use selenium then ? – Vipin Joshi Sep 01 '19 at 14:03
  • @VipinJoshi selenium is used for automation testing, assuming the site is up indeed... if you do need to check for the status code you can use `requests` and put the automation in an `if` statement something like `if(requests.get("url").status_code == 200)` – Moshe Slavin Sep 01 '19 at 14:30
  • It is widely used for scraping too, and lots of websites block you detecting user behaviour. Requests just does not emulates the way selenium does, thus not useful for scraping applications. I get it, the question wasn't asked in context to scraping stuff. – Vipin Joshi Sep 01 '19 at 15:33
  • 1
    @VipinJoshi yes selenium is used for scraping too, but as you can see in the issue that was opened in GitHub >"We will not be adding this feature to the WebDriver API as it falls outside of our current scope (emulating user actions)" see more of the discussion [here](https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/141) – Moshe Slavin Sep 01 '19 at 15:40