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"}
4
  • Could you post the error as well please? Commented Jan 8, 2021 at 8:51
  • 1
    Ya added exception now you can find it Commented Jan 8, 2021 at 8:54
  • could give us the full url to reproduce, please. Commented Jan 8, 2021 at 8:55
  • Should get logged in to get through the site, it's my college online class website. Commented Jan 8, 2021 at 8:57

3 Answers 3

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.

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

2 Comments

Can you show me the syntax for Xpath using class
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='label--Z12LMR3']"} (Session info: chrome=87.0.4280.88)
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.

2 Comments

Can you show me the syntax for Xpath using class
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='label--Z12LMR3']"} (Session info: chrome=87.0.4280.88)
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
    

Comments

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.