WebDriver API------定位元素篇

  通俗点讲,自动化要做的就是模拟鼠标和键盘来操作一系列web元素,要么单击,要么双击,要么输入,亦或者鼠标悬停。要想操作这些元素,我们就必须找到他们。机器不像人那样可以用眼睛分辨页面的元素,并判断他们是做什么用的,所以机器如何找到他们呢?我们需要从代码层面定位元素。

       通过前端工具,我们可以看到,页面上的元素都是由一行行代码组成,他们之间有层级地组织起来,每个元素有不同地标签名和属性值。WebDriver就是通过这些信息来找到不同元素的。

   WebDriver 提供了八种元素定位方法,在Python语言中,所对应的方法如下:

  id                                                    ->                             name

  class name                                     ->                             tag name

  link text                                           ->                             partial link text

  xpath                                               ->                             css selector

  find_element_by_id()                      ->                             find_element_by_name()

  find_element_by_class_name()      ->                            find_element_by_tag_name()

  find_element_by_link_text()            ->                            find_element_by_partial_link_text()

  find_element_by_xpath()                ->                            find_element_by_css_selector()

  

  

猜你喜欢

转载自www.cnblogs.com/cindy-zx/p/11352292.html
今日推荐