3

I'm trying to click on the following element with the class name equals "clean right":

<li class="clean right"></li>

How could I locate it by using driver.find_element_by_class_name()

3
  • it must be 'clean.right' Commented Mar 4, 2020 at 20:35
  • 1
    Have you tried anything, done any research? Commented Mar 4, 2020 at 20:46
  • 1
    Does this answer your question? Selenium Compound class names not permitted Commented Mar 4, 2020 at 20:47

2 Answers 2

7

You can't pass multiple classnames as argument through find_element_by_class_name() and doing so you will face an error as:

invalid selector: Compound class names not permitted

There are multiple approaches to solve this usecase and you can use either of the following Locator Strategies:

  • If the element is uniquely identified only through the classname clean you can use:

    driver.find_element_by_class_name("clean")
    
  • If the element is uniquely identified only through the classname right you can use:

    driver.find_element_by_class_name("right")
    
  • If both the classnames, clean and right are mandatory to identify the element, you can use as follows:

    driver.find_element_by_css_selector("li.clean.right")
    
  • As an alternative you can also use as follows:

    driver.find_element_by_xpath("//li[@class='clean right']")
    

tl; dr

Invalid selector: Compound class names not permitted error using Selenium


Reference

Find div element by multiple class names?

Sign up to request clarification or add additional context in comments.

Comments

1

The previous answer is partially incorrect. please check the source code at :

https://github.com/SeleniumHQ/selenium/blob/9160de55af9cc230f758f4ce6a2af8d1570f0614/py/selenium/webdriver/remote/webdriver.py

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

Example for using class with space:

from selenium import webdriver
from time import sleep

options = webdriver.ChromeOptions()
#options.headless = True
options.add_argument("--window-size=1920,1080")
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument(
    "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
browser = webdriver.Chrome(options=options)
browser.get("https://www.instagram.com")
sleep(5)
#browser.refresh()
elem=browser.find_element_by_class_name('RP4i1.UVauz')
print(elem.get_attribute("outerHTML"))
browser.get_screenshot_as_file(f"screenshot.png")

Output:

<img class="RP4i1  UVauz" src="/static/images/homepage/screenshot1.jpg/d6bf0c928b5a.jpg" alt="">

If you check the exception from the by_class_name:

enter image description here

You can see that it is using css_class locator under the hood ( You can see it add . in frontautomatically)

Another Working example:

from selenium import webdriver

import time

from selenium.webdriver.support import expected_conditions as EC

driver = 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"))

19 Comments

I'm forced to downvote this answer as this answer is conceptually wrong. You are completely messing up the concept of locators.
@DebanjanB could you explain why ?
@DebanjanB in what level do you think the answer is wrong ?
THe answer provides a working example also
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'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.