The answer is No, You can't use driver.find_element_by_class_name () or driver.find_elements_by_class_name () with multiple class names. It accepts only single class name.

However, you can use find_elements_by_xpath or find_element_by_css_selector method to achieve finding element with multiple class names.

for example below code will find element on google website using two different class names.

url= "http://google.com"
driver = webdriver.Chrome()
driver.get(url)
driver.find_elements_by_xpath("//*[@class='sfibbbc' or @class='jsb']")
# Following line will result in error 
driver.find_elements_by_class_name("sfibbbc jsb")
Answer from Gaurang Shah on Stack Overflow
๐ŸŒ
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()
Discussions

Using CSS selector for find element with multiple class in Selenium in Python - Stack Overflow
I want to find element below with find_elements_by_css_selector(...) but it have multiple class artdeco-modal, artdeco-modal--layer-default ,ip-fuse-limit-alert. I used Selenium IDE for collecting ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
February 26, 2021
automated testing - How to select element by class when multiple elements match? - Software Quality Assurance & Testing Stack Exchange
If I select flight departure time with class name through selenium in python but class name is used by both div & span tags it fails. How can I select only one element type (div) and not sdpan... More on sqa.stackexchange.com
๐ŸŒ sqa.stackexchange.com
May 4, 2019
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
Selenium CSS selector by id AND multiple classes - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ find-div-element-by-multiple-class-names-in-selenium
Find div element by multiple class names in Selenium?
With the css locator, we can use all the values set for the class attributes in a compound class. This is done by combining all them separated by a dot(.) and also adding a dot(.) at the start of the css expression. WebElement l = driver.findElement(By.cssSelector(".toc.chapters")); import ...
๐ŸŒ
Makeseleniumeasy
makeseleniumeasy.com โ€บ 2017 โ€บ 09 โ€บ 26 โ€บ how-to-locate-web-element-which-has-multiple-class-names
How To Locate Web Element Which Has Multiple Class Names
September 26, 2017 - If you do not want to specify node name or want it to be any tag name then combine all class names with dot (.) and append a dot (.) in beginning which generally represents a class value in cssSelector i.e. .classname1.classname2.classname3 ยท driver.findElement(By.cssSelector(โ€œ.required_field.cityPadRight.ac_input.origin_acโ€)).sendKeys(โ€œDELHIโ€); package SpecialConcepts; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; import io.github.bonigarcia.wdm.WebDriverManager; public class El
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 66383426 โ€บ using-css-selector-for-find-element-with-multiple-class-in-selenium-in-python
Using CSS selector for find element with multiple class in Selenium in Python - Stack Overflow
February 26, 2021 - Use any of the css selector. either classname or tagname[attributename='attributevalue'] div.artdeco-modal.artdeco-modal--layer-default.ip-fuse-limit-alert ... div.artdeco-modal.artdeco-modal--layer-default.ip-fuse-limit-alert[aria-labelledby='ip-fuse-limit-alert__header'] You can use following xpath as well to identify the element. //div[@class='artdeco-modal artdeco-modal--layer-default ip-fuse-limit-alert']
๐ŸŒ
YouTube
youtube.com โ€บ fewsteps
Handling Multiple class names using Selenium And Python - YouTube
This video explains how to Handle Multiple class names using Selenium And Python โ™ฅ๏ธ Watch all Selenium / channel videos / Subscribe: https://www.youtube.com/...
Published ย  May 5, 2020
Views ย  166
Find elsewhere
๐ŸŒ
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"
๐ŸŒ
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)

๐ŸŒ
w3tutorials
w3tutorials.net โ€บ blog โ€บ selenium-python-selecting-via-css-selector
Selenium Python: How to Select Elements with Shared Classes Using CSS Selectors โ€“ Fixing Password Change Test Issues
Notice the input fields: all have the class form-control. If you try to select them using find_element(By.CSS_SELECTOR, ".form-control"), Selenium will return the first matching element (Current Password), but you need to target all three fields separately.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ selenium - multiple links same class name
r/learnpython on Reddit: Selenium - Multiple Links Same Class Name
March 12, 2021 -

I'm having trouble clicking a certain button. It appears all the buttons have the same class name so I can't use that. Any help would be so much appreciated.

Website Code:

https://imgur.com/a/54juO4V

I've tried the following

browser.find_element_by_link_text('New Registration').click()

browser.find_element_by_xpath("//*[contains(text(), 'main content')]").click()

browser.find_element_by_css_selector('.row:nth-child(2) .btn').click()

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 33855026 โ€บ selenium-with-python-find-elements-of-more-than-one-css-class
Selenium with Python - find elements of more than one css class - Stack Overflow
May 24, 2017 - If you want elements that have .dropdown and .right CSS class, then : elements = driver.find_elements_by_css_selector(".dropdown.right") # elements[0] # elements[1] # ...
๐ŸŒ
TOOLSQA
toolsqa.com โ€บ selenium-webdriver โ€บ css-selectors-in-selenium
How to use and create CSS Selectors in Selenium with examples?
October 5, 2016 - 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. With the ever-growing new technologies of web development, multiple ...
Top answer
1 of 2
13

This By.classname("btn btn-xs btn-custom") will not work, as it contains multiple spaces which means it is combination of 3 classes.

You will have to switch to css selector or xpath , I do not know why you have mentioned that you do not want to use both of them.

However, If you are interest to use css selector :

You can try this :

By.cssSelector("btn.btn-xs.btn-custom")  

If you go by precedence :

  1. ID
  2. name
  3. classname
  4. linkText
  5. partialLinkText
  6. tagName
  7. css selector
  8. xpath
2 of 2
1

The class attribute of this element...

<button style="padding:2px 7px; background-color:#4caeea" 
  class="btn btn-xs btn-custom" </button>
  

contains multiple classnames.

Using By.classname("btn btn-xs btn-custom") you won't be able to locate it as:

Find elements based on the value of the "class" attribute. If an element has multiple classes, then this will match against each of them. For example, if the value is "one two onone", then the class names "one" and "two" will match.

As per your requirement, avoiding the xpath & css-selectors you still can use classname but the element may not be the first/only match. As an example:

  • By.classname("btn") //would match all the elements which contains the classname btn
  • By.classname("btn-xs") //would match all the elements which contains the classname btn-xs
  • By.classname("btn-custom") //would match all the elements which contains the classname btn-custom

As an alternative, you can also use the following Locator Strategies:

  • cssSelector:

    By.cssSelector("button.btn.btn-xs.btn-custom")

  • xpath

    By.xpath("//button[@class='btn btn-xs btn-custom']")

๐ŸŒ
Edureka Community
edureka.co โ€บ home โ€บ community โ€บ categories โ€บ selenium โ€บ find div element by multiple class names
Find div element by multiple class names - Selenium
December 4, 2020 - I'd like to identify that web element. It only has this ... className = "value test") @CacheLookup private WebElement test;
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 71270003 โ€บ how-to-find-elements-with-multiple-attributes-using-cssselector-python-and-selen
How to find_elements with multiple attributes using cssSelector Python and Selenium - Stack Overflow
6 Selenium Python: find_element_by_css_selector() using :contains() 3 selenium findElement By.cssSelector for multiple classes ยท 0 Python - Selenium (find elements by CSS selector) 5 Python: find_element_by_css_selector ยท 4 Finding elements by CSS selector with ChromeDriver (Selenium) in Python ยท
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 71881074 โ€บ how-to-get-all-elements-with-multiple-classes-in-selenium
python - How to get all elements with multiple classes in selenium - Stack Overflow
elements_using_ec = WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "li.foo.foo-default.cat.bar"))) print(len(elements)) ... from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC ... Sign up to request clarification or add additional context in comments. ... You can find the imports above. 2022-04-15T08:36:27.183Z+00:00 ... @Stophface Does your html actually have <li class=foo foo-default cat bar/> or is it <li class="foo foo-default cat bar"/>? 2022-04-15T07:25:43.28Z+00:00