3. 定位单个对象

以下方法都可以用来定位某个对象,优先选择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')
  2. Location by Name
    username = driver.find_element_by_name('username') password = driver.find_element_by_name('password')

  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]")

     

    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')
  5. Location by Tag Name
    heading1 = driver.find_element_by_tag_name('h1')
  6. Location by Class Name
    content = driver.find_element_by_class_name('content')
     
  7. Location by CSS Selector
    content = driver.find_element_by_css_selector('p.content')

    Sauce Labs has good documentation on CSS selectors.

转载于:https://www.cnblogs.com/hugh007/p/3853509.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值