Friday, October 28, 2011

Selenium WebDriver tips 2

1. "Xpath expression cannot be evaluated or does notresult in a WebElement" error

During our recent automation project we've been encountering really irritating error using WebDriver
"The xpath expression '//given xpath' cannot be evaluated or does notresult in a WebElement"

This error was appearing randomly, for example script was working for 10 times but failed on the 11th. Eventually after several experiments we were able to narrow the problem to following factors:

- this problem appears on the IE only(IE8 in our case)
- this problems appears when WebElement is identified using XPath selector
- this problems appears often shortly after webpage is loaded

Possible Solutions:

Get rid of the XPath selectors
This is great idea and Alister Scott presents sound arguments for this here. This approach however is not always applicable in our context. We know that sophisticated XPath selectors can be brittle but having to automate application based on Microsott SharePoint there is not much choice

Add pause before first FindElement (with XPath selector) method
This solution worked out however you need to remember about adding this pause every time new page is opened. Because in general we don't like pauses in our automation scripts for many reasons we were looking for something else.

Update current FindElement method
We found this solution indirectly thanks to Alister Scott and his SpecDriver example on github. Because this problem appears usually at the first attempt to find element using XPath it's easy to update FindElement method and create your own where you can catch the exception for given number of times. An example in C#:

for (int i = 1; i <= 10; i++)
return driver.FindElement(selector);
catch (OpenQA.Selenium.WebDriverException e)
Console.WriteLine("Error " + i + " raised: " + e.Message);

It really works and often above loop doesn't need more than two iteration to return correct WebElement.

2. WebDriver loses focus on IE8

Another issue which appears randomly. Fortunately we were able to identify one of the reason of this issue - Microsoft Office Communicator. In order to run tests using WebDriver on IE (Windows 7, IE8 in our case) Microsoft's IM needs to be switched off if not then WebDriver randomly switches from IE to IM window during script execution.



  1. This comment has been removed by the author.

  2. Very informative article Selenium Webdriver .....thanks for sharing

  3. From what I'm reading, I think implicit waits would be a helpful solution for problem #1. See "Implicit waits" section here:

    By configuring an implicit wait, you get the advantage of pausing when you need to, without having to pause all the time when you don't.

    1. The think is that in this case the selector is found by WebDriver, but somehow it is not visible as WebElement (correct HTML tag). I guess this may happend while the selector is not fully loaded. In this specific case the implicit wait doesn't help.

    2. Hi,
      I am using Webdriver for testing.
      In our application there is report, in which
      Xpath is same for column. See the below example:
      All combo boxes have same Xpath.
      All text boxes have same Xpath.
      All links have same Xpath.
      Then how to handle it.
      I want to write the code for selection of the combo box values and relative text for every row.

      |Column Name 1 |Column Name 2|Column Name 3|
      |Combo box | Text box |Link |
      |Combo box | Text box |Link |
      |Combo box | Text box |Link |
      |Combo box | Text box |Link |
      |Combo box | Text box |Link |

      Add another (This is the link to add new row) >>

  4. Really informative! I met exactly the same issue as problem 1.
    Another question is, my IE9 cannot keep in the front after opened and logged in.Seems similar to problem 2. But I don't have office communicator installed. Maybe other IM impact webdriver execution, too.Would you please tell me how you found it was caused by office communicator?


  5. Nice article and through this article one can learn the selenium automation's best practices.

    What one should avoid while writing scripts to automate certain thing on a web site