Only using class names is not sufficient in your case.

  • By.cssSelector(".ban") has 15 matching nodes
  • By.cssSelector(".hot") has 11 matching nodes
  • By.cssSelector(".ban.hot") has 5 matching nodes

Therefore you need more restrictions to narrow it down. Option 1 and 2 below are available for CSS selector, 1 might be the one that suits your needs best.

Option 1: Using list items' index (CssSelector or XPath)

Limitations

  • Not stable enough if the site's structure changes

Example:

driver.FindElement(By.CssSelector("#rightbar > .menu > li:nth-of-type(3) > h5"));
driver.FindElement(By.XPath("//*[@id='rightbar']/ul/li[3]/h5"));

Option 2: Using Selenium's FindElements, then index them. (CssSelector or XPath)

Limitations

  • Not stable enough if a site's structure changes
  • Not the native selector's way

Example:

// Note that By.CssSelector(".ban.hot") and //*[contains(@class, 'ban hot')] are different, but doesn't matter in your case
IList<IWebElement> hotBanners = driver.FindElements(By.CssSelector(".ban.hot"));
IWebElement banUsStates = hotBanners[3];

Option 3: Using text (XPath only)

Limitations

  • Not for multilanguage sites
  • Only for XPath, not for Selenium's CssSelector

Example:

driver.FindElement(By.XPath("//h5[contains(@class, 'ban hot') and text() = 'us states']"));

Option 4: Index the grouped selector (XPath only)

Limitations

  • Not stable enough if the site's structure changes
  • Only for XPath, not CssSelector

Example:

driver.FindElement(By.XPath("(//h5[contains(@class, 'ban hot')])[3]"));

Option 5: Find the hidden list items link by href, then traverse back to h5 (XPath only)

Limitations

  • Only for XPath, not CssSelector
  • Low performance
  • Tricky XPath

Example:

driver.FindElement(By.XPath(".//li[.//ul/li/a[contains(@href, 'geo.craigslist.org/iso/us/al')]]/h5"));
Answer from Yi Zeng on Stack Overflow
๐ŸŒ
BrowserStack
browserstack.com โ€บ home โ€บ guide โ€บ mastering selenium css selectors in 2026
CSS Selector in Selenium: Locate Elements with Examples | BrowserStack
December 10, 2025 - To make it unique we should also use the โ€œhrefโ€ attribute. ... CSS Selectors in Selenium allows to match a partial String with the help of various symbols to represent the start, end and contents inside a text.
๐ŸŒ
Selenium
selenium.dev โ€บ documentation โ€บ webdriver โ€บ elements โ€บ locators
Locator strategies | Selenium
February 16, 2026 - import pytest from selenium import webdriver from selenium.webdriver.common.by import By def test_class_name(): driver = webdriver.Chrome() driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html") element = driver.find_element(By.CLASS_NAME, "information") assert element is not None assert element.tag_name == "input" driver.quit() def test_css_selector(driver): driver = webdriver.Chrome() driver.get("https://www.selenium.dev/selenium/web/locators_tests/locators.html") element = driver.find_element(By.CSS_SELECTOR, "#fname") assert element is not None assert element.get_
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ software testing โ€บ selenium-css-selectors
Selenium CSS Selectors - GeeksforGeeks
In this article, weโ€™ll break down the different types of CSS selectors available in Selenium using Java and walk you through practical examples to help you implement them effectively in your test scripts.
Published ย  July 23, 2025
๐ŸŒ
Selenium Python
selenium-python.readthedocs.io โ€บ locating-elements.html
4. Locating Elements โ€” Selenium Python Bindings 2 documentation
from selenium.webdriver.common.by import By driver.find_element(By.XPATH, '//button[text()="Some text"]') driver.find_elements(By.XPATH, '//button') The attributes available for the By class are used to locate elements on a page. These are the attributes available for By class: ID = "id" NAME = "name" XPATH = "xpath" LINK_TEXT = "link text" PARTIAL_LINK_TEXT = "partial link text" TAG_NAME = "tag name" CLASS_NAME = "class name" CSS_SELECTOR = "css selector"
๐ŸŒ
Bird Eats Bug
birdeatsbug.com โ€บ blog โ€บ css-selectors-in-selenium
CSS Selectors in Selenium: Examples and Best Practices | Bug Tracking Blog @ Bird Eats Bug
CSS selectors are a key way to locate elements on a web page when automating tests with Selenium. They provide a simple and efficient method to target elements by ID, class, tag, or other attributes.
Top answer
1 of 3
79

Only using class names is not sufficient in your case.

  • By.cssSelector(".ban") has 15 matching nodes
  • By.cssSelector(".hot") has 11 matching nodes
  • By.cssSelector(".ban.hot") has 5 matching nodes

Therefore you need more restrictions to narrow it down. Option 1 and 2 below are available for CSS selector, 1 might be the one that suits your needs best.

Option 1: Using list items' index (CssSelector or XPath)

Limitations

  • Not stable enough if the site's structure changes

Example:

driver.FindElement(By.CssSelector("#rightbar > .menu > li:nth-of-type(3) > h5"));
driver.FindElement(By.XPath("//*[@id='rightbar']/ul/li[3]/h5"));

Option 2: Using Selenium's FindElements, then index them. (CssSelector or XPath)

Limitations

  • Not stable enough if a site's structure changes
  • Not the native selector's way

Example:

// Note that By.CssSelector(".ban.hot") and //*[contains(@class, 'ban hot')] are different, but doesn't matter in your case
IList<IWebElement> hotBanners = driver.FindElements(By.CssSelector(".ban.hot"));
IWebElement banUsStates = hotBanners[3];

Option 3: Using text (XPath only)

Limitations

  • Not for multilanguage sites
  • Only for XPath, not for Selenium's CssSelector

Example:

driver.FindElement(By.XPath("//h5[contains(@class, 'ban hot') and text() = 'us states']"));

Option 4: Index the grouped selector (XPath only)

Limitations

  • Not stable enough if the site's structure changes
  • Only for XPath, not CssSelector

Example:

driver.FindElement(By.XPath("(//h5[contains(@class, 'ban hot')])[3]"));

Option 5: Find the hidden list items link by href, then traverse back to h5 (XPath only)

Limitations

  • Only for XPath, not CssSelector
  • Low performance
  • Tricky XPath

Example:

driver.FindElement(By.XPath(".//li[.//ul/li/a[contains(@href, 'geo.craigslist.org/iso/us/al')]]/h5"));
2 of 3
0

By.cssSelector(".ban") or By.cssSelector(".hot") or By.cssSelector(".ban.hot") should all select it unless there is another element that has those classes.

In CSS, .name means find an element that has a class with name. .foo.bar.baz means to find an element that has all of those classes (in the same element).

However, each of those selectors will select only the first element that matches it on the page. If you need something more specific, please post the HTML of the other elements that have those classes.

๐ŸŒ
Sauce Labs
saucelabs.com โ€บ home โ€บ blog โ€บ selenium tips: css selectors
Selenium Tips: CSS Selectors
April 2, 2023 - The WebDriver code library provides methods to do just that, such as findelement() or findelements(). These usually take a locator, which can be created by ID, XPATH Code, or Cascading Style Sheets (CSS).
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ css โ€บ css_selectors.asp
CSS Selectors
The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element. The CSS rule below will be applied ...
๐ŸŒ
Functionize
functionize.com โ€บ blog โ€บ css-selectors-what-they-are-and-use-in-selenium
What are CSS selectors and how they are used in Selenium.
June 3, 2025 - Anyone who has created a .css file ... and thus specify what style it should be given. Selenium reuses CSS selectors in order to uniquely identify each element in your UI....
๐ŸŒ
TestGrid
testgrid.io โ€บ selenium โ€บ how to use css selectors in selenium webdriver
CSS Selectors in Selenium: Types, Syntax & Examples
June 16, 2025 - In the above example, the pre-filled text field has a value of โ€œTextโ€ฆโ€. If the value is dynamic, for example, โ€œTextโ€ฆโ€ or โ€œNextโ€ฆโ€, you can use a CSS selector that matches elements whose value attribute always contains โ€œextโ€œ. Here is how you can write it: ... Selenium allows you to locate elements in the HTML DOM based on their relationship with other elements. The following are some of the ways by which child elements can be selected using CSS selectors.
๐ŸŒ
QACRAFT -
qacraft.com โ€บ home โ€บ selenium testing โ€บ css selector in selenium
Css Selector in Selenium - QACRAFT
January 28, 2026 - Match a Substring: The motivation behind this is to compare to the string by utilizing a matching substring. ... Here the input is HTML Tag, name is attribute and โ€˜yourโ€™ is the substring value of the string. 5) Inner Text: Utilizing inner text distinguishes and makes CSS Selectors in Selenium WebDriver by using a string design that the HTML Tag appears on the site page.
๐ŸŒ
Requestly
requestly.com โ€บ home โ€บ uncategorized โ€บ css selector in selenium: complete tutorial with examples
CSS Selector in Selenium: Complete Tutorial with Examples
September 17, 2025 - Learn how to use CSS selectors in Selenium to locate elements, handle dynamic content, and write robust automation tests.
๐ŸŒ
Testmu
testmu.ai โ€บ testmu ai โ€บ learning hub โ€บ css selectors in selenium
CSS Selectors in Selenium: A Complete Guide to Locating Web Elements
January 11, 2026 - CSS Selectors in Selenium offer a seamless way to locate elements without the complexity of long XPath expressions. Since they align closely with how browsers interpret styles, they deliver faster execution and greater stability.
๐ŸŒ
TOOLSQA
toolsqa.com โ€บ selenium-webdriver โ€บ css-selectors-in-selenium
How to use and create CSS Selectors in Selenium with examples?
Other than starting and ending, the CSS Selector in Selenium is also available with contains text(). It can locate the element by using any sequential characters from the attribute value.
๐ŸŒ
Seleniumeasy
seleniumeasy.com โ€บ selenium-tutorials โ€บ css-selectors-tutorial-for-selenium-with-examples
set of examples
Just to recap, In selenium we have used different types of locators such as ID, Name, CSS Selector, XPath, ClassName, TagName, LinkText and Partial LinkText.
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ selenium โ€บ css selector in selenium
CSS Selector in Selenium
April 24, 2025 - CSS Selectors in Selenium are string patterns used to identify an element based on a combination of HTML tag, id, class, and attributes. Locating by CSS Selectors in Selenium is more complicated than the previous methods, but it is the most ...
๐ŸŒ
Bright Data
brightdata.com โ€บ faqs โ€บ selenium โ€บ find-elements-by-css
How to Find Elements by CSS Selector in Selenium?
July 10, 2024 - To find elements by CSS Selector in Selenium, you need to use the find_element_by_css_selector method.
๐ŸŒ
SWTestAcademy
swtestacademy.com โ€บ home โ€บ css selectors in selenium 17 tactics and examples
CSS Selectors in Selenium 17 Tactics and Examples
January 1, 2022 - CSS Selectors in Selenium Complete Tutorial! This CSS in Selenium Tutorial explains how to write dynamic css selectors in webdriver projects.