Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...