【问题标题】:In webdriverios, how to select the last element with the same classname?在 webdriverios 中,如何选择具有相同类名的最后一个元素?
【发布时间】:2021-12-10 18:40:16
【问题描述】:

我有多个具有相同类名的元素,我想选择最后一个。 我尝试了以下方法,但它们不起作用。 任何帮助将不胜感激。

 const elem = $('.example')[-1]
 const elem = $('.example[-1]')
 const elem = $('.example').last()

【问题讨论】:

    标签: webdriver-io


    【解决方案1】:

    您可以使用 ":last-child" CSS 伪选择器:

    const elem = $('.example:last-child')
    

    https://developer.mozilla.org/en-US/docs/Web/CSS/:last-child

    【讨论】:

      【解决方案2】:

      :last-of-type 也可能有用:) 例如我们得到这样一篇文章:

      <article>
            <h1>A Title</h1>
            <p>Paragraph 1.</p>
            <p>Paragraph 2.</p>
            <p>Paragraph 3.</p>
            <img src="...">
          </article>
      

      我们可以像这样选择最后一个 p 元素:

        const elem = $('p:last-of-type')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-18
        • 2013-12-13
        • 2014-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多