如何检测Selenium何时加载浏览器的错误页面 [英] How to detect when Selenium loads a browser's error page
问题描述
driver.get(http://google.com)$ b $在Firefox中,Selenium将加载Try Again错误页面,其中包含Firefox无法建立连接到www.google.com服务器。 Selenium不会抛出任何错误。
是否有浏览器无关的方式来检测这些情况?对于firefox(python),我可以做
pre $ 如果elem.get_attribute(id)中的errorPageContainer但是(1)这看起来像是计算过度的杀伤性的(2)我必须为每个浏览器创建自定义的代码。
如果你禁用你的互联网,并使用htmlunit作为浏览器,你将得到一个页面与下面的HTML
< html>
< head>< / head>
< body>未知的主机< / body>
< / html>
我怎样才能检测到这个
如果driver.find_element_by_css_selector(body b $ b 看起来像这样检查每一个页面加载将会非常昂贵,因为通常会有大量的文本在文本中。
奖金点,如果你也知道一种方式来检测负载问题的类型,例如没有互联网连接,无法访问的主机等。
解决方案 WebDriver API不会公开HTTP状态代码 ,所以如果你想检测/管理HTTP错误,你应该使用一个调试代理。
请参阅Jim的优秀文章实现WebDriver HTTP状态关于如何完成那个。
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.
解决方案 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.
这篇关于如何检测Selenium何时加载浏览器的错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!