如何对具有两个 类 的元素使用方法 all

How to use method all for elements with two classes

此代码无效

descriptions = []
page.all('div', class: 'news-tidings__speech news-helpers_hide_mobile-small').each do |el|
  descriptions.push(el.text[1..200])
end

HTML的一部分:

<div class="news-tidings__speech news-helpers_hide_mobile-small">text </div>

有几种方法可以满足您的需求。

  1. 只需使用带有多个 类

    的标准 CSS 选择器
    page.all('div.news-tidings__speech.news-helpers_hide_mobile-small')
    
  2. 如果您想使用 :class 选项,它将采用所需 类 的数组(元素必须具有所有 类)

    page.all('div', class: ['div.news-tidings__speech','news-helpers_hide_mobile-small'])
    
  3. 如果您想确保这些是唯一的 类,并且按照特定的顺序,在元素上您可以使用 CSS 属性选择器

    page.all("div[class='news-tidings__speech news-helpers_hide_mobile-small']")