码迷,mamicode.com
首页 > 其他好文 > 详细

3. 定位单个对象

时间:2014-07-19 00:06:04      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   strong   os   

以下方法都可以用来定位某个对象,优先选择id,name.

  • find_element_by_id
  • find_element_by_name
  • find_element_by_xpath
  • find_element_by_link_text
  • find_element_by_partial_link_text
  • find_element_by_tag_name
  • find_element_by_class_name
  • find_element_by_css_selector

  1. Location by ID
    login_form = driver.find_element_by_id(‘loginForm‘)
    bubuko.com,布布扣
  2. Location by Name
    username = driver.find_element_by_name(‘username‘)
    password = driver.find_element_by_name(‘password‘)

    bubuko.com,布布扣
  3. Location by XPATH
      绝对路径(如何html有轻微的调整就会失效)
    login_form = driver.find_element_by_xpath("/html/body/form[1]")
    相对路径元素位置 login_form = driver.find_element_by_xpath("//form[1]")
    相对路径元素属性 login_form = driver.find_element_by_xpath("//form[@id=‘loginForm‘]")

    相对路径子元素的name属性
    username = driver.find_element_by_xpath("//form[input/@name=‘username‘]")
    相对路径id熟悉后index username = driver.find_element_by_xpath("//form[@id=‘loginForm‘]/input[1]")
    相对路径name属性 username = driver.find_element_by_xpath("//input[@name=‘username‘]")

    相对路径中的双属性name和type
    clear_button = driver.find_element_by_xpath("//input[@name=‘continue‘][@type=‘button‘]")
    相对路径中前id属性后index clear_button = driver.find_element_by_xpath("//form[@id=‘loginForm‘]/input[4]")
    bubuko.com,布布扣
     

    There are also a couple of very useful Add-ons that can assist in discovering the XPath of an element:

    • XPath Checker - suggests XPath and can be used to test XPath results.
    • Firebug - XPath suggestions are just one of the many powerful features of this very useful add-on.
    • XPath Helper - for Google Chrome

  4. Location Hyperlink by Link Text
    continue_link = driver.find_element_by_link_text(‘Continue‘)
    continue_link = driver.find_element_by_partial_link_text(‘Conti‘)
    bubuko.com,布布扣
  5. Location by Tag Name
    heading1 = driver.find_element_by_tag_name(‘h1‘)
    bubuko.com,布布扣
  6. Location by Class Name
    content = driver.find_element_by_class_name(‘content‘)
    bubuko.com,布布扣
    
    
  7. Location by CSS Selector
    content = driver.find_element_by_css_selector(‘p.content‘)
    bubuko.com,布布扣
    Sauce Labs has good documentation on CSS selectors.

3. 定位单个对象,布布扣,bubuko.com

3. 定位单个对象

标签:des   style   http   color   strong   os   

原文地址:http://www.cnblogs.com/hugh007/p/3853509.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!