简单对象的定位

简单对象的定位

webdriver 提供了一系列的元素定位方法,常用的有以下几种

  • id
  • name
  • class name
  • tag name
  • link text
  • partial link text
  • xpath

css selector 分别对应 python webdriver 中的方法为:

  • 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()

1. id和name定位
简单对象的定位
例:

  • id=” kw”
    find_element_by_id(" kw")
  • name=”wd”
    find_element_by_name(“wd”)

2. tag name 和 class name 定位

  • 通过 tag 标签名对对元素进行定位:
    div
    find_element_by_tag_name(“div”)

  • 通过元素中带的 class 属性对元素进行定位:

input type=“text” class=“s_ipt” name=“wd” id=“kw” maxlength=“100”
autocomplete=“off”

例:

  • class=” s_ipt”
    find_element_by_class_name(" s_ipt")

3. link text 与 与 partial link text 定位
简单对象的定位

  • 通过 link text 定位元素:
    find_element_by_link_text(“百度推广”)
  • 通过 partial link text 定位元素:
    find_element_by_partial_link_text(“百度”)

4. XPath 定位

  • 绝对路径定位
    find_element_by_xpath(“/html/body/div[2]/div[2]/div[5]/div/div/div/img/form/span/input”)
  • 相对路径定位
    find_element_by_xpath(“//*[@id=“kw”]”)—通过自身的id属性定位

4.CSS 定位
find_element_by_css_selector()
简单对象的定位简单对象的定位简单对象的定位