0

Selenium is not recognizing the class. I was trying to click an audio button on a website using selenium but it is not recognizing this class

class="jumbo--Z12Rgj4 buttonWrapper--x8uow audioBtn--1H6rCK"

Here is my code

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions() 
options.add_argument(r"user-data-dir=./c")

driver=webdriver.Chrome(executable_path=r"C:\Users\Users\Desktop\Scrapping\chromedriver.exe",options=options)
    
driver.get(r"https://www.xx------xx.com")
    
button5 = driver.find_element_by_class_name(r"jumbo--Z12Rgj4")
    
button5.click()

But selenium is throwing an exception

jumbo--Z12Rgj4

Exception:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".label--Z12LMR3"}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

3 Answers3

1

If you check again, there would be more than one class 'jumbo--Z12Rgj4', hence selenium is throwing exception. To make it more dynamic, better use xpath with "contains(@class,'jumbo--Z12Rgj4')", followed by another identifier to make the search unique.

1

The element that you wish to target probably has a uniquely hashed classname every time you load it, because it is not being found at all on the website, so rather than using the classname, try to use its xpath instead.

Arvind Raghu
  • 408
  • 3
  • 9
1

The value of class attribute i.e. jumbo--Z12Rgj4 buttonWrapper--x8uow audioBtn--1H6rCK is dynamic and will keep on changing everytime you access the application. To click on the element with text as save you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element_by_css_selector("[class^='jumbo']").click()
    
  • Using xpath:

    driver.find_element_by_xpath("//*[starts-with(@class, 'jumbo')]").click()
    

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[class^='jumbo']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[starts-with(@class, 'jumbo')]"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352