I have a task to check presence of the favicon on the page. How to do it? I use Selenium WebDriver.

I made in the same way, addressed according to the link "http://my_site.com/favicon.ico", and later I checked for existence of a faviсon on this page (also I pulled out an element), but not on main page and my test passed.


It depends. There are three cases to consider:

  1. The page does not have a favicon.
  2. The page has a favicon by virtue of a favicon.ico file located in the website's root but does not specify the favicon in HTML.
  3. The page specifies a favicon using a link element in HTML.

Selenium cannot distinguish between cases (1) and (2).

Selenium can check for case (3) the same way you would check for any other element, e.g. using a XPath selector. For example, if your link looks like this:

<link rel="shortcut icon" href="http://example.com/myicon.ico" />

then you might check for that element with an XPath like this:

//xpath[@rel='shortcut icon']

Wikipedia's favicon page has a list of other ways to specify the link element.


You won't be able to get the favicon with Selenium you would have to use another program to grab it. The only way you would be able to get it is if your website rendered the favicon.ico as a link such as

<link rel="shortcut icon"
 href="http://example.com/myicon.ico" />

However typically websites just store the favicon.ico in the root directory and on page request the browser retrieves it and drops it in the address bar or tab or wherever favicons are used. If this is how your favicon is rendered then there will be no code or anything to search for with Selenium.

Also the above code while it does work has some buggy support for IE7.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.