当前位置:   article > 正文

selenium---定位一组元素(find_elements)_selenium find.element的查找范围

selenium find.element的查找范围

前言

  前面写了定位方法基本上都是单个定位方法,如果我们想要定位页面上的多个相同的元素呢?这样我们应该怎么定位?selenium这么强大,提供了有find_elements方法来帮助我们定位多个相同的元素

find_elements

find_elements属于selenium中复数的定位元素内容。find_elements主要定位定位页面上多个相同的元素坐标。

源码:(这里我们简单的拿一个进行查看)

def find_elements_by_xpath(self, xpath):
        """
        Finds multiple elements by xpath.

        :Args:
         - xpath - The xpath locator of the elements to be found.

        :Returns:
         - list of WebElement - a list with elements if any was found.  An
           empty list if not

        :Usage:
            elements = driver.find_elements_by_xpath("//div[contains(@class, 'foo')]")
        """
        return self.find_elements(by=By.XPATH, value=xpath)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

可以发现定位方法和find_element其实一样的,只不过多了一个s相当于定位多组

小试牛刀

这里安静来拿百度的搜索结果来使用find_elements

先在百度搜索测试-安静

在这里插入图片描述

 

找到多个元素,class="result c-container ",我们通过复数进行定位,然后获取全部的文本内容

在这里插入图片描述

通过一个简单的例子,认识find_elements复数定位。具体的一些实例方法,我们后续一点点的涉及。


安静写的文章如果对您有帮助,点个关注,持续更新。不懂的或者写错的地方,可以下方留言。也可以关注安静的个人工作号:【跟着安静学测试】

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号