selenium中的find_element方法

selenium是一款十分强大的Web应用自动化框架,我们可以通过它来自动操控浏览器。操控浏览器的实质是操控浏览器的界面元素,因此定位元素是使用selenium的关键,selenium中通过find_element方法来完成定位。

1.find_element的三种用法

(1)通过webdriver对象的find_element(“属性名”,”属性值”)

        如1:我们要定位一个属性id,值为”wang”的元素

        

selenium中的find_element方法

        如2:我们要定位一个属性class,值为”plant”的元素

        

selenium中的find_element方法

(2)通过webdriver对象的find_element_by_xx(” “)(在selenium的4.0版本中此种用法已经抛弃,不推荐使用)

        如:定位id为username,class_name为password,tag_name为input的元素

        

selenium中的find_element方法

 (3)通过webdriver模块中的By,以指定方式定位元素

        导入模块:from selenium.webdriver.common.by import By

         如:定位id为username,class_name为password,tag_name为input的元素

        

selenium中的find_element方法

2.find_element()和find_elements()的区别

        (1)find_element()的返回结果是一个WebElement对象,如果符合条件的有多个,默认返回找到的第一个,如果没有找到则抛出NoSuchElementException异常。

        (2)find_elements()的返回结果是一个包含所有符合条件的WebElement对象的列表,如果未找到,则返回一个空列表。

来源:心藏炬火

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年11月27日
下一篇 2021年11月27日

相关推荐