There are cases when using XPath has some advantages over CSS selectors.
For example if there is a site where we can select an item only by text because the other type of locators are not clear(id’s and classes are generated and they are used in multiple places).

What can we do in this situation?

  1. Use a CSS locator to select multiple elements and then call the getText() function and compare the results with the selected string
    This is a working solution, but Webdriver calls are expensive and it can really slow down test runs in IE8 and IE9 browsers.
  2. Use the XPath’s contains function to select the element by its text value
    span[contains(text(),’Text to search for’)]
Short background story:

To solve our initial problem using CSS we have been forced to use the getText() function to be able to compare the text and get the appropriate element.
When we came up with the 2. solution the overall run time of the tests have been reduced and the test runs became more stable in IE8 and IE9.

Similar Posts from the author: