Technically you can do the search using a CSS selector but the locator alone would not find the elements you want. You would have to loop through all the elements and look for contained text using .getText(). The below is an example

List<WebElement> labels = driver.findElements(By.cssSelector("div.entity-label.ng-binding[ng-bind-html='entity.Label']"));
int count = 0;
for (WebElement label : labels)
{
    if (label.getText().equals("Name"))
    {
        count++;
    }
}
System.out.println(count);

The more efficient way to do it (and the only way to locate an element with containing text) is to use an XPath. The XPath would contain the text you are looking for so that with only the locator you would find all the desired elements. The code below would return the count of elements that you are looking for.

driver.findElements(By.xpath("//div[.='Name'][ng-bind-html='entity.Label']")).size();
Answer from JeffC on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › finding items with selenium by css selector using multiple attributes
r/learnpython on Reddit: finding items with selenium by CSS selector using multiple attributes
March 23, 2023 -

Hi all. I'm trying to use Selenium to find an element by CSS selector and although I've tried using the documentation, I think I'm misunderstanding something.

This is what I'm trying:

driver.find_element(By.CSS_SELECTOR,f"img [src='images/icons/new.gif'][alt='Add new item to list']").click()

This is the element I want to find and click:

<img src="images/icons/new.gif" alt="Add new item to list">

The error I get is as follows:

Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element: {"method":"css selector","selector":"img [src='images/icons/new.gif'][alt='Add new item to list']"}

I can see it's on the page, however, so I'm not sure what I'm missing. I can find other items using XPATH and ID so selenium is up and running, just to rule that out. Can anybody suggest anything?

(I'm using selenium 4.8.2 and python 3.11.1, if that helps)

Discussions

Selenium CSS selector by id AND multiple classes - Stack Overflow
I had a quick look in the bug tracker in Selenium but I didn't see anything about that. I wanted to ask here before raising an issue that doesn't need to be. Also as far as I understand it doesn't work in IE6 but who cares. I was using firefox for this run. ... Actually the two are quite different selectors. ... Sign up to request clarification or add additional context in comments. ... findElement() finds an element ... More on stackoverflow.com
🌐 stackoverflow.com
java - Find div element by multiple class names? - Stack Overflow
Also, in cases like this, Css Selector is always in favor of XPath (fast, concise, native). ... Sign up to request clarification or add additional context in comments. ... thanks for the nice explanations, it works perfectly in my selenium instance 2019-11-25T14:18:58.627Z+00:00 ... /** * Find elements ... More on stackoverflow.com
🌐 stackoverflow.com
Pulling multiple elements from the same page
I would recommend not using XPath for a multitude of reasons and would use the same CSS selectors before and store in a List. Then use a for loop and use .get(i) and .get(I+1) to get the values, increment your loop by 2. More on reddit.com
🌐 r/selenium
32
1
September 18, 2022
finding items with selenium by CSS selector using multiple attributes
Your CSS selector is wrong - there shouldn't be a space between the img and the [src]. A space between items in a CSS selector means 'descendant of', so img[src] means an image with a src, but img [src] means an image with a descendant element which has a source. There probably aren't any of those on your page, so you're getting an error. In the future you can check whether your issue is with Selenium or with your CSS selector by trying out a Document.querySelector() call in your browser console to see if you can find the element with the selector you've written! More on reddit.com
🌐 r/learnpython
2
2
March 23, 2023
🌐
Bird Eats Bug
birdeatsbug.com › blog › css-selectors-in-selenium
CSS Selectors in Selenium: Examples and Best Practices | Bug Tracking Blog @ Bird Eats Bug
Grouping simplifies scripts and makes them easier to maintain when multiple elements need the same action. Pseudo-classes allow selection based on position or state. Examples include :first-child, :last-child, and :nth-of-type(n). For instance, ul li:nth-of-type(2) selects the second list item in every unordered list. Pseudo-classes are valuable when elements lack unique IDs or consistent attributes, letting testers rely on structure instead. Using CSS selectors in Selenium test scripts helps locate elements quickly and perform actions efficiently.
🌐
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.
🌐
Softwaretestingmentor
softwaretestingmentor.com › home › selenium css selector #3 – css selector with multiple attributes
Selenium CSS Selector #3 - CSS Selector with Multiple Attributes - Software Testing Mentor
September 27, 2021 - In this Selenium CSS selector tutorial we will learn how to write Selenium CSS selector using multiple attributes of the webelement. You can write advanced
🌐
BrowserStack
browserstack.com › home › guide › mastering selenium css selectors in 2026
CSS Selector in Selenium: Locate Elements with Examples | BrowserStack
December 10, 2025 - They offer a fast, flexible way to target elements when writing automated tests. ... Selenium uses CSS selector expressions to match elements in the DOM.
🌐
YouTube
youtube.com › software testing mentor
Selenium CSS Selector #3 - CSS Selector with Multiple Attributes - YouTube
❖ FREE Training's at https://training.rcvacademy.com ❖ In this Selenium CSS selector tutorial we will learn how to write Selenium CSS selector using multiple...
Published   April 2, 2020
Views   549
Find elsewhere
🌐
RCV Academy
rcvacademy.com › home › automation testing › selenium css-selector tutorial › selenium css selector #3 – css selector with multiple attributes
Selenium CSS Selector #3 – CSS Selector with Multiple Attributes - RCV Academy
September 15, 2021 - In this Selenium CSS selector tutorial we will learn how to write Selenium CSS selector using multiple attributes of the webelement. You can write advanced CSS selectors using the mix of Tag, ID or CLASSNAME and other attributes of the webelement. Syntax for CSS selector with multiple attributes: ...
🌐
The Test Tribe
thetesttribe.com › home › automation › css selectors in selenium explained (with examples)
CSS Selectors in Selenium Explained (with Examples) - The Test Tribe
April 2, 2025 - By enhancing skills in CSS Selectors, testers can write robust, maintainable, and reliable test scripts. You can combine multiple selectors using a comma (,) to select various elements.
🌐
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 ...
🌐
YouTube
youtube.com › natasa tech
How to use Multiple Attributes in CSS Selector Selenium | NATASA Tech - YouTube
How to use Multiple Attributes CSS SelectorIf you have many elements which is all having the same attribute and attribute value, then How to select specific...
Published   September 16, 2020
Views   292
🌐
TESTEROPS
seleniumwithjavapython.wordpress.com › selenium-with-python › web-elements-locating-mechanisms › handling-multiple-css-selectors
Handling Multiple CSS Selectors - testerops - WordPress.com
July 20, 2016 - """ element5=driver.find_element_by_css_selector("label[class^='class-name1']") print(element5.text) sleep(5) element6=driver.find_element_by_css_selector("label[class$='class-name2']") print(element6.text) sleep(5) element7=driver.find_element_by_css_selector("input[class*='class-name2']") element7.clear() element7.send_keys('narkanda@gmail.com') sleep(5) def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
🌐
ScrapeOps
scrapeops.io › home › selenium web scraping playbook › python selenium find elements css
Python Selenium Guide - Finding Elements by CSS Selectors | ScrapeOps
January 8, 2024 - When using CSS selectors, we can also use the attributes of our element as criteria. The example below finds all elements that contain an ID and then prints their ID to the console. from selenium import webdriver from selenium.webdriver.common.by import By #open Chrome driver = webdriver.Chrome() #navigate to the site driver.get("https://quotes.toscrape.com/login") #find all elements that contain an ID new_list = driver.find_elements(By.CSS_SELECTOR, "[id]") #print the id of each element for item in new_list: print(item.id) #close the browser driver.quit()
🌐
Pcloudy
pcloudy.com › home › understanding css selectors in selenium
Understanding CSS Selectors in Selenium
February 15, 2024 - In cases where multiple elements match your selector, adjust the specificity by adding or removing attribute selectors or using different combinators. Use CSS Selector Testers: Explore online CSS selector testing tools that allow you to enter ...
🌐
TestGrid
testgrid.io › selenium › how to use css selectors in selenium webdriver
CSS Selectors in Selenium: Types, Syntax & Examples
June 16, 2025 - Learn how to use CSS Selectors in Selenium to locate dynamic web elements using ID, class, attributes, and partial string matching.
🌐
DevQA
devqa.io › selenium-css-selectors
Selenium CSS Selectors Examples
In the above snippet, we want to select an available day (i.e. the two last div elements) As can be seen, all four divs contain “calendar-day-“ but the first two also contain “unavailable” which we don’t want. The CSS selector for Not selecting the first two divs is · driver.findElement(By.cssSelector("div[class*=calendar-day-]:not([class*='unavailable'])"));" ... There are occasions when there are multiple child elements within the same parent element such as list elements
🌐
Reddit
reddit.com › r/selenium › pulling multiple elements from the same page
r/selenium on Reddit: Pulling multiple elements from the same page
September 18, 2022 -

So I am making a Garmin crawling script and I want it to pull multiple elements if they are from the same day and add the time together for some activities, time, distance and heart rate for another for example.

Layout of website

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import login as login
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import datetime
import time

x = datetime.datetime.now()
x = x.strftime("%b %d")

driver = browser = webdriver.Firefox()
driver.get("https://connect.garmin.com/modern/activities")

driver.implicitly_wait(1)

iframe = driver.find_element(By.ID, "gauth-widget-frame-gauth-widget")
driver.switch_to.frame(iframe)

driver.find_element("name", "username").send_keys(login.username)

driver.find_element("name", "password").send_keys(login.password)
driver.find_element("name", "password").send_keys(Keys.RETURN)

driver.switch_to.default_content()

time.sleep(10)

driver.find_element("name", "search").send_keys("Reading")
driver.find_element("name", "search").send_keys(Keys.RETURN)

time.sleep(2)

element = driver.find_element(By.CSS_SELECTOR, '.activity-date > span:nth-child(1)').text

time.sleep(2)
print(element)

time_read = 0

if element == x:
	spent = driver.find_element(By.CSS_SELECTOR, 'li.list-item:nth-child(1) > div:nth-child(2) > div:nth-child(5) > div:nth-child(2) > span:nth-child(1) > span:nth-child(1)').text

        result = time.strptime(spent, "%H:%M:%S")

	time_read += result.tm_hour * 60

	time_read += result.tm_min

	print(time_read)

So this is my current code. It finds the date, checks if it is today and adds the minutes to the variable time_read.

Now I need some help in how I go about adding multiple elements, and if this can be done with some kind of for loop, where it loops between the dates and can then extract the time from the element?

Do I need to set them up one by one, since I need to provide which element a specific iteration needs to pull from? So maybe I should have 5 or 6 checks for example, instead of some kind of loop that goes through and does it? Then it will be a lot of manual work, which makes me question if there isn't a better way to deal with it.

I do not want to use CSV.

Some relevant HTML

<div class="pull-left activity-date date-col">
        <span class="unit">Sep 14</span>
        <span class="label">2022</span>
    </div>

<span class="unit" title="3:32:00"><span class="" data-placement="top" title="3:32:00">3:32:00</span></span>

<span class="unit" title="1:00:00"><span class="" data-placement="top" title="1:00:00">1:00:00</span></span>
<span class="" data-placement="top" title="1:00:00">1:00:00</span>

Also a bit unsure what the best way is to locate elements? Is CSS.SELECTOR good or should I use XPATH preferably?

Thanks

🌐
TutorialsPoint
tutorialspoint.com › home › selenium › selenium webdriver: identify multiple elements
Selenium Webdriver - Identify Multiple Elements
April 7, 2021 - Once we navigate to a webpage, ... job is to identify the elements. We can create a css selector for their identification and utilize the method find_elements_by_css_selector....