You can generate the css-selector from ul like ul > li:nth-child(1) for home. See below:

driver.findElement(By.cssSelector("ul > li:nth-child(1)")); >> home
driver.findElement(By.cssSelector("ul > li:nth-child(2)")); >> posts
driver.findElement(By.cssSelector("ul > li:nth-child(3)")); >> events

also reachin span is the same:

driver.findElement(By.cssSelector("ul > li:nth-child(1) > a > span")); >> home
Answer from Mesut GUNES on Stack Overflow
🌐
Sauce Labs
saucelabs.com › home › blog › selenium tips: css selectors
Selenium Tips: CSS Selectors
April 2, 2023 - Let’s write an XPath and css selector that will choose the input field after “username”. This will select the “alias” input, or will select a different element if the form is reordered. ... If you don’t care about the ordering of child elements, you can use an attribute selector in selenium to choose elements based on any attribute value.
People also ask

Why Use CSS Selectors in Selenium?
CSS Selectors in Selenium offer a fast, reliable, and simple way to locate web elements. They are interpreted directly by the browser, making them faster than XPath. Their clean syntax improves readability, ensures consistent results across browsers, and makes maintenance easier as web pages evolve.
🌐
testmu.ai
testmu.ai › testmu ai › learning hub › css selectors in selenium
CSS Selectors in Selenium: A Complete Guide to Locating Web Elements
How do CSS Selectors improve test script maintainability in Selenium?
CSS Selectors improve maintainability by providing shorter, cleaner locators that are easier to read and update. Since they closely match how browsers interpret elements, even small UI or DOM changes don’t break them as easily as XPath expressions. This makes long-term test maintenance smoother, especially in agile projects with frequent front-end updates.
🌐
testmu.ai
testmu.ai › testmu ai › learning hub › css selectors in selenium
CSS Selectors in Selenium: A Complete Guide to Locating Web Elements
How can you verify whether a CSS Selector correctly identifies an element before using it in Selenium?
You can validate a CSS Selector directly in your browser’s Developer Tools. Press F12 → Elements tab → Ctrl/Cmd + F, then paste the CSS Selector. If it correctly highlights the intended element, it’s valid. This step ensures accuracy and prevents script failures caused by incorrect locators.
🌐
testmu.ai
testmu.ai › testmu ai › learning hub › css selectors in selenium
CSS Selectors in Selenium: A Complete Guide to Locating Web Elements
🌐
Medium
moniljoshi.medium.com › css-selectors-for-locating-elements-in-details-e6cbd62b99fc
Different CSS Selectors techniques for locating elements in Selenium Web Driver | by Monil Joshi | Medium
October 19, 2021 - Here you can use any CSS selector like class, tagName attribute combination anything instead of siblings. nth child: We will consider same example. Using this technique we can access element directly using its index with respect to its parent.
🌐
TOOLSQA
toolsqa.com › selenium-webdriver › css-selectors-in-selenium
How to use and create CSS Selectors in Selenium with examples?
Subsequently, we can use them together to create a CSS Selector for locating the web element, as shown below: textarea.form-control[placeholder='Current Address'] ... Then we provided the value of the class attribute. In the end, inside the square bracket, we provided the placeholder attribute and its value. So this way, we can combine various attributes of an HTML element to locate the web element in Selenium uniquely.
🌐
BrowserStack
browserstack.com › home › guide › mastering selenium css selectors in 2026
CSS Selector in Selenium: Locate Elements with Examples | BrowserStack
December 10, 2025 - Learn to use CSS Selector in Selenium scripts for your automated tests with five types of CSS Selectors and code snippet examples.
🌐
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.
🌐
Guru99
guru99.com › home › selenium › css selector in selenium
CSS Selector in Selenium
April 24, 2025 - Step 2) Enter “css=input#email” into the Target box of Selenium IDE and click the Find button. Selenium IDE should be able to highlight that element. CSS Selector in Selenium using an HTML tag and a class name is similar to using a tag and ...
Find elsewhere
🌐
DevQA
devqa.io › selenium-css-selectors
Selenium CSS Selectors Examples
driver.findElement(By.cssSelector("input.myForm")); //or driver.findElement(By.cssSelector(".myForm"));
🌐
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 - Learn how to use CSS Selectors in Selenium to locate web elements efficiently with syntax, examples, and best practices for reliable test automation.
🌐
Stack Overflow
stackoverflow.com › questions › 24844538 › how-to-go-by-index-in-css-selector
selenium - How to go by index in CSS selector? - Stack Overflow
How to go by index in CSS selector? I am talking about Selenium identification here. say I have to point out to 5th tag under a specific tag.
🌐
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"
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.

🌐
TestGrid
testgrid.io › selenium › how to use css selectors in selenium webdriver
Mastering CSS Selectors in Selenium for efficient locators
June 16, 2025 - In the above example, 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: ... 6. Parent Child hierarchy: Selenium allows you to locate elements in the HTML DOM based on their relationship with other elements.
🌐
Seleniumeasy
seleniumeasy.com › selenium-tutorials › css-selectors-tutorial-for-selenium-with-examples
Seleniumeasy
Selenium Tutorial for beginners in Java, Python. We provide selenium webdriver tutorials with working examples step-by-step. Learn Selenium Webdriver, TestNG, Maven, Jenkins, JUnit, ANT, Java , Python (required of Selenium), JMeter, Appium JExcel, Apache POI to use them for your needs.
🌐
Software Testing Help
softwaretestinghelp.com › home › selenium automation › selenium – css selector for identifying web elements
Selenium - CSS Selector for Identifying Web Elements
May 9, 2025 - Locate/inspect the web element ... would create a CSS Selector. ... Type “css=input#Email” i.e. the locator value in the target box in the Selenium IDE and click on the Find button....
🌐
Gitbook
bharateeshd.gitbook.io › seleniumatoz › selenium › locators › css-selectors
CSS Selectors | Web Automation Using Selenium
In the above section, if I want to find the list element li which has none of the attributes inside ul unordered list then we can write css selector like
🌐
Selenium
selenium.dev › documentation › webdriver › elements › locators
Locator strategies | Selenium
1 month ago - We can use css selector locator strategy to identify the element on the page. If the element has an id, we create the locator as css = #id. Otherwise the format we follow is css =[attribute=value] . Let us see an example from above HTML snippet.
🌐
Pcloudy
pcloudy.com › home › understanding css selectors in selenium
Understanding CSS Selectors in Selenium
February 15, 2024 - CSS Selectors selectors are easy, precise and simple to use in Selenium tests and these selectors provide flexibility to the user so he can create his own selector and use it to find an element.
🌐
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