selenium元素定位篇:class定位


在前端的單個頁面中,通過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)方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM