Selenium Compound class names not permitted Selenium Compound class names not permitted python python

Selenium Compound class names not permitted


Leon's comment leads to the correct information that compound class names are no longer supported. What you could do instead is try using css selectors. In your case, the following line of code should help you get the element you want :

el3 = driver.find_element_by_css_selector(".action-btn.cancel.alert-display")

It finds the element with all three classes (action-btn, cancel and alert-display) in the class attribute. Do note that the order of the classes does not matter here and any of the classes may appear anywhere in the class attribute. As long as the element has all three classes, it will be selected. If you want the order of the classes to be fixed, you can use the following xpath :

el3 = driver.find_element_by_xpath("//*[@class='action-btn cancel alert-display']") 


I am late to this question. But I also found a work around by treating the compound classes as a String, using tag_name, and get_attribute('class'), when you are not familiar with Xpath. It needs some more lines of code but it's straight forward and fit for beginners like me.

   elements = driver.find_elements_by_tag_name('Tag Name Here')        for element in elments:            className = watchingTable.get_attribute('class')            print(className)                if className == 'Your Needed Classname':                    #Do your things


The answer here are incorrect :

If you check the exception from the by_class_name:

enter image description here

you can see it is using css under the hood

by_class_name just adds '.' infront of the locator provided so 'a' will be passed as '.a' and a.b will be passed as '.a.b'

So You can use class_name for multiple classes , just need to replace space with '.'

so "a b c" should be passed as "a.b.c"

Eg:

Working example:

from selenium import webdriverimport timefrom selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Chrome()driver.get("https://stackoverflow.com/questions/65579491/find-element-by-class-name-in-selenium-giving-error/65579606?noredirect=1#comment115946541_65579606")time.sleep(5)elem = driver.find_element_by_class_name('overflow-x-auto.ml-auto.-secondary.grid.ai-center.list-reset.h100')print(elem.get_attribute("outerHTML"))