Invalid selector: Compound class names not permitted error using Selenium

I am trying to print one of my messages from a chat via webWhatsapp.

I was able to do it via Javascript from the Console tab i did it this way

recived_msg = document.getElementsByClassName('XELVh selectable-text invisible-space copyable-text') // returns an array of the chat
recived_msg[5].innerText // shows me the 4th message content

The problem is that i tried to do same thing on python but it doesnt work for me..

Heres what i tried:

from selenium import webdriver
recived_msg = driver.find_element_by_class_name('XELVh selectable-text invisible-space copyable-text')
final = recived_msg[5].innerText #doesnt work for some reason

my Error that i'm getting is: Message: invalid selector: Compound class names not permitted

I'm kinda new to javascript so sorry for missunderstanding and thank you for your help! :)


As per the documentation of selenium.webdriver.common.by implementation:

class selenium.webdriver.common.by.By
    Set of supported locator strategies.

    CLASS_NAME = 'class name'

So,

  • Using find_element_by_class_name() you won't be able to pass multiple class names. Passing multiple classes you will face the error as:

    Message: invalid selector: Compound class names not permitted
    
  • Additionally, as you want to return an array of the chats, so instead of find_element* you need to use find_elements*


Solution

As an alternative you can use either of :the following Locator Strategies:

  • CSS_SELECTOR:

    recived_msg = driver.find_elements_by_css_selector(".XELVh.selectable-text.invisible-space.copyable-text")
    
  • XPATH:

    recived_msg = driver.find_elements_by_xpath("//*[@class='XELVh selectable-text invisible-space copyable-text']")