Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 808
user2426679 Avatar asked Mar 30 '14 05:03

user2426679


People also ask

How do you check if a page is loaded in Selenium?

We can get Selenium to recognize that a page is loaded. We can set the implicit wait for this purpose. It shall make the driver to wait for a specific amount of time for an element to be available after page loaded.

How do I find the error message in Selenium?

To capture error message or simple text as well we can use predefined method called getText(). getText() method will simply capture the error message or text and will return you a String then you can store in String variable and you can use in other application or you can verify as well.

How does Selenium handle page loading issues?

There are three ways to implement Selenium wait for page to load: Using Implicit Wait. Using Explicit Wait. Using Fluent Wait.

Can a website detect when you are using Selenium?

The answer is YES! Websites can detect the automation using JavaScript experimental technology navigator. webdriver in the navigator interface. If the website is loaded with automation tools like Selenium, the value of navigator.


2 Answers

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.

like image 56
Faiz Avatar answered Oct 23 '22 22:10

Faiz


If you just need to remote-control the Tor Browser, you might also consider the Marionette framework by Mozilla. Bonus: It fails when a page cannot be loaded: (see navigate(url) in the API)

The command will return with a failure if there is an error loading the document or the URL is blocked. This can occur if it fails to reach the host, the URL is malformed, the page is restricted (about:* pages), or if there is a certificate issue to name some examples.

Example use (copy from other answer):

To use with the Tor Browser, enable marionette at startup via

Browser/firefox -marionette

(inside the bundle). Then, you can connect via

from marionette import Marionette
client = Marionette('localhost', port=2828);
client.start_session()

and load a new page for example via

url='http://mozilla.org'
client.navigate(url);

For more examples, there is a tutorial.

like image 39
serv-inc Avatar answered Oct 23 '22 21:10

serv-inc