Selenium CSS Selector Wildcard Example

Yesterday, when trying to get a dynamic count of number of fields for a Ruby on Rails application, I stumbled across a scenario where I had to match the ID value for a HTML tag by using a wildcard pattern. The requirement was to get a count of number of ID tags matching a specific pattern and then update the last of that tag with a user provided value. I tried several examples using CSS selectors, and finally found out how to use wildcard for CSS selectors inside Selenium. I'm posting a Java code snippet of my selector code, that I passed to the getCssCount method of com.thoughtworks.selenium.DefaultSelenium class. 
int tagcount=selenium.getCssCount("css=input[id^='post_tags_attributes'][type='text']").intValue();
This code looks for "INPUT" tags with ID starting with post_tags_attributes and has a "type" attribute matching a value of "text". The operator '^' is a wild card that helps to mimic a "begins with" search on the ID attribute.

No comments:

Post a Comment