zoukankan      html  css  js  c++  java
  • selenium---定位一组元素(find_elements)

      前面写了定位方法基本上都是单个定位方法,如果我们想要定位页面上的多个相同的元素呢?这样我们应该怎么定位?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)

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

    小试牛刀

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

    先在百度搜索测试-安静

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

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

    安静写的文章如果对您有帮助,点个关注,持续更新。不懂的或者写错的地方,可以下方留言。

  • 相关阅读:
    web.xml配置详解
    oracle按时间创建分区表
    cron表达式详解
    临时表
    配置非安装版tomcat服务
    CodeForces 785 D Anton and School
    CodeForces 601B Lipshitz Sequence
    CodeForces 590C Three States BFS
    CodeForces 592D Super M DP
    CodeForces 507E Breaking Good 2维权重dij
  • 原文地址:https://www.cnblogs.com/qican/p/13534167.html
Copyright © 2011-2022 走看看