FindElementByCss
is generally faster unless using IE, and then it depends which version of IE and what type of traversal is required.
Repeated tests have proven FindElementByCss
to be more performant than FindElementByXPath
(Note: that if there is a unique id present then selecting by id is always the first choice!)
In benchmarked tests Chrome
and FireFox
saw faster matching using CSS consistently across different traversal paths. They are optimized with CSS in mind and using CSS selectors is advocated as selenium best practice. IE
was more variable with most instances of XPath
being slightly more performant, but there being some clear paths that favoured CSS
selection. Long XPath selectors will be costly and prone to breakage. Later versions of IE
saw more variability. Opera12
browser came in with mixed results.
I would use a CSS selector:
So, for a simple selection based on likely unique attribute, I would go with an attribute CSS selector of [value='8897']
to target the value
attribute. The []
means attribute selector. So value
attribute with value of 8897
.
driver.FindElementByCss("[value='8897']").Click
If you want to be more selective you can throw in an additional attribute selector, as follows, to target the type
attribute.
driver.FindElementByCss("[type=checkbox][value='8897']").Click
When should I use XPath then?
Older IE versions for sure.
Any requirement for walking up the DOM would point to XPath usage.
XPath has some great additional locator strategies for hard to find elements, but that is not necessary AFAIK here. You can see some of the additional considerations here.