如何按 class 名称长度查找元素

How to find elements by class name length

我正在寻找页面中每个 class 具有特定长度的内容。

现在我正在使用

list=[] #empty array
elements = x.find_elements(By.XPATH,"//*[@class]") #finds out every element that have a class in the page, x is my chromedriver
for element in elements: #inspect every single element found
    class = element.get_attribute('class') #filters only the class name
    if (len(class)==8): #filters the class name exacy length
        list.append(element) #the element is now inside of the list and the loop can inspect next element

但这非常慢,我认为这是因为 for 循环。您是否知道如何直接在 find_elements 函数内查找特定长度或避免 for 循环或任何其他解决方案来加快速度?

我只是在回答我自己的问题,因为我认为我找到了一种非常快速的方法

我想找到类名“g”或“g <6alphanumerics>” 出于某种原因,在 javascript 中,如果你制作 document.getElementsByClassName('g'),它会找到我需要的一切

所以新的片段是:

classes = x.execute_script("return document.getElementsByClassName('g')")

return 很重要,否则数组将为空