Selenium CSS Selector Wildcard Example - Part 2

In the previous post, we showed how to match an ID value by using wild cards in Selenium. In this post, we will provide some more methods that are available in Selenium to match tags with a specific pattern. On the same lines as of starts with type of matching, it is possible to match tags with a ends with a specific suffix or contains a specific pattern in its attribute value. Note that the operator used to match a specific prefix in CSS selector is ^. To find pattern matching a suffix or in the entire string, we have to use different operators as shown in the example below;

1) To match a tag that ends with a specific pattern (suffix)
We use the operator "$=" for this purpose. As an example, to match all 'INPUT' type tags that has an ID ending with '_selector', write the CSS selector code as shown below;
css=input[id$='_selector']

2) To match a tag that contains a specific pattern (in the entire value)
We use the wildcard '*=' for  this case. As an example, to match all 'INPUT' type tags that has an ID containing the word 'selector', write the CSS selector code as shown below;
css=input[id*='selector']

Just to recollect, if you want to match all tags with a prefix of 'selector_' you have to write your CSS Selector as shown below;
css=input[id^='selector_']

No comments:

Post a Comment