首页 > 解决方案 > 为什么 selenium 不能识别 Python 中的类?

问题描述

Selenium 无法识别该类。我试图单击使用 selenium 的网站上的音频按钮,但它无法识别此类

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

这是我的代码

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()

但是硒抛出了一个异常

巨无霸--Z12Rgj4

例外:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".label--Z12LMR3"}

标签: pythonseleniumxpathcss-selectorswebdriverwait

解决方案


如果再次检查,将会有不止一个类'jumbo--Z12Rgj4',因此 selenium 会抛出异常。为了使其更具动态性,最好使用带有“contains(@class,'jumbo--Z12Rgj4')”的 xpath,后跟另一个标识符以使搜索唯一。


推荐阅读