2

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...

2

2 Answers 2

5

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

4 Comments

Why even use selenium then ?
@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)
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.
@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
1

Update: It actually is possible using the chrome-developer-protocoll with event listeners.

See example script at https://stackoverflow.com/a/75067388/20443541

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.