Python: Message: invalid selector: Compound class names not permitted

Submitted 3 years, 7 months ago
Ticket #155
Views 433
Language/Framework Python
Priority Medium
Status Closed

I'm tyring to test the Python Selenium and I have this code:

from selenium import webdriver
url = 'https://www.youtube.com/channel/UC8tgRQ7DOzAbn9L7zDL8mLg/videos'
driver = webdriver.Chrome()

driver.get(url)

videos = driver.find_elements_by_class_name('style-scope ytd-grid-renderer')

for video in videos:
    title = video.find_elements_by_xpath('.//*[@id="video-title"]').text
    views = video.find_elements_by_xpath('.//*[@id="metadata-line"]/span[1]').text
    when = video.find_elements_by_xpath('.//*[@id="metadata-line"]/span[2]').text
    print(title,views,when)

And during run I have this error:

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Compound class names not permitted
  (Session info: chrome=85.0.4183.83)
  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.18363 x86_64)

Submitted on Sep 11, 20
add a comment

1 Answer

Verified

If not finding Xpath ,  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")

Or else you can try

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

Submitted 3 years, 7 months ago


Latest Blogs