首页 > 其他 > 详细

selenium元素定位篇:class定位

时间:2021-03-03 15:07:58      阅读:166      评论:0      收藏:0      [点我收藏+]

在前端的单个页面中,通过id定位是万无一失的,因为id值是唯一的。但是,并不是每个元素都有id属性,所以我们可以通过其他元素进行元素定位。
今天要说的就是元素定位方法之一:class定位。

在python中,通过class定位的方法如下:

    def find_element_by_class_name(self, name):
        """
        Finds an element by class name.

        :Args:
         - name: The class name of the element to find.

        :Returns:
         - WebElement - the element if it was found

        :Raises:
         - NoSuchElementException - if the element wasn‘t found

        :Usage:
            element = driver.find_element_by_class_name(‘foo‘)
        """
        return self.find_element(by=By.CLASS_NAME, value=name)

参数name:元素的class属性值。
返回值:如果找到元素,则返回WebElement对象。否则,抛出NoSuchElementException异常。
用法举例:element = driver.find_element_by_class_name("foo")

拓展:selenium中,八种定位方式最终都是通过调用find_element(by, value)方法

selenium元素定位篇:class定位

原文:https://www.cnblogs.com/ctltest/p/14473041.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!