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...
-
1Possible duplicate of How to get HTTP Response Code using Selenium WebDriverNavarasu– Navarasu2018-10-17 13:28:47 +00:00Commented Oct 17, 2018 at 13:28
-
You need to use another library for it. Check out the requests library in python.sayhan– sayhan2018-10-17 13:51:08 +00:00Commented Oct 17, 2018 at 13:51
Add a comment
|
2 Answers
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)
4 Comments
Vipin Joshi
Why even use selenium then ?
Moshe Slavin
@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)Vipin Joshi
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.
Moshe Slavin
@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
Update: It actually is possible using the chrome-developer-protocoll with event listeners.
See example script at https://stackoverflow.com/a/75067388/20443541