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?
- 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. - 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:
- How to check presence of an element when it disappears in less than 500ms
- Some thoughts about the infamous StaleElementReferenceException
- Handling CSRF prevention tokens / dynamic data in JMeter
- Webdriver – clicking on a visible element out of a group:
- Wedbriver wait for ajax to finish and JQuery animation
Recent Comments