selenium元素定位篇:id定位


在前端,id是唯一的,只属于一个元素。

在python中,元素定位的方法如下:

    def find_element_by_id(self, id_):
        """Finds an element by id.

        :Args:
         - id\_ - The id of the element to be found. 

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

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

        :Usage:
            element = driver.find_element_by_id('foo')
        """
        return self.find_element(by=By.ID, value=id_)

我们可以看到,通过find_element_by_id()方法,传入id即可定位到元素。
参数id:元素的id属性。
返回值:如果定位到了元素,返回WebElement对象。否则抛出NoSuchElementException异常
用法举例:element = driver.find_element_by_id("foo")

该方法最终调用的是find_element(by=By.ID, value=id_),并返回WebElement对象。

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

问题:在前端,如果id是动态变化时该怎么办?


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM