How to detect when Selenium loads a browser's error page

firefox, htmlunit, selenium

Solution

WebDriver API doesnot expose HTTP status codes , so if you want to detect/manage HTTP errors, you should use a debugging proxy.

See Jim's excellent post Implementing WebDriver HTTP Status on how to do exactly that.

Problem

Is there a universal way to detect when a selenium browser opens an error page? For example, disable your internet connection and do ``` driver.get("http://google.com") ``` In Firefox, Selenium will load the 'Try Again' error page containing text like "Firefox can't establish a connection to the server at www.google.com." Selenium will NOT throw any errors. Is there a browser-independent way to detect these cases? For firefox (python), I can do ``` if "errorPageContainer" in [ elem.get_attribute("id") for elem in driver.find_elements_by_css_selector("body > div") ] ``` But (1) this seems like computational overkill (see next point below) and (2) I must create custom code for every browser. If you disable your internet and use htmlunit as the browser you will get a page with the following html ``` <html> <head></head> <body>Unknown host</body> </html> ``` How can I detect this without doing ``` if driver.find_element_by_css_selector("body").text == "Unknown host" ``` It seems like this would be very expensive to check on every single page load since there would usually be a ton of text in the body. Bonus points if you also know of a way to detect the type of load problem, for example no internet connection, unreachable host, etc.

Original source

Related problems