1. #!/usr/bin/env python
    # -*- coding:utf-8 -*-
  2.  
  3. from selenium import webdriver
  4.  
  5. import time,os
  6.  
  7. # about:addons 火狐浏览器安装组件,访问的地址
  8.  
  9. # <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
    #id
    keys="selenium自动化"
    # url="https://www.baidu.com/"
    # url="file:///D:/ideaSpace/autoProject/python_autotest/nicetime/webdriver/select01.html"
    url="file:///D:/ideaSpace/autoProject/python_autotest/nicetime/webdriver/a03.html"
    driver=webdriver.Firefox()
  10.  
  11. driver.get(url)
  12.  
  13. #id name class 属性定位
    driver.find_element_by_xpath("//*[@id='kw']").send_keys(keys)
    driver.find_element_by_xpath("//*[@name='wd']").send_keys(keys)
    driver.find_element_by_xpath("//*[@class='s_ipt']").send_keys(keys)
  14.  
  15. #其他属性定位
    driver.find_element_by_xpath("//*[@autocomplete='off']").send_keys(keys)
  16.  
  17. #指定标签名称 写标签名称,不指定则写 *,如搜索框 input
    driver.find_element_by_xpath("//input[@class='s_ipt']").send_keys(keys)
  18.  
  19. #通过多级层级来定位 ,上一个层级 上上层级
    # <form name="f" id="form" action="/s" class="fm" onsubmit="javascript:F.call('ps/sug','pssubmit');">
    # <span id="s_kw_wrap" class="bg s_ipt_wr quickdelete-wrap">
    # <span class="soutu-btn"></span><input type="text" class="s_ipt" name="wd" id="kw" maxlength="100" autocomplete="off">
  20.  
  21. driver.find_element_by_xpath("//form[@id='form']/span/input").send_keys(keys)
  22.  
  23. #select01.html文件
    # 下拉框选择形式
    # <select id="status" class="form-control valid" onchange="" name="status">
    # <option value=""></option>
    # <option value="0">未审核</option>
    # <option value="1">初审通过</option>
    # <option value="2">复审通过</option>
    # <option value="3">审核不通过</option>
    # </select>
  24.  
  25. driver.find_element_by_xpath("//option[@value='2']").click()
  26.  
  27. # <a href="https://www.hao123.com" target="_blank" class="mnav">hao123</a>
    # 模糊匹配
    driver.find_element_by_xpath("//*[contains(text(),'hao123')]").click()
  28.  
  29. # <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
    #模糊匹配 包含属性
    driver.find_element_by_xpath("//*[contains(@id,'kw')]").send_keys(keys)
  30.  
  31. #模糊匹配 已什么开头
    driver.find_element_by_xpath("//input[starts-with(@class,'s_')]").send_keys(keys)
  32.  
  33. # find_element方式 单个数据
    driver.find_element(by='id',value='kw').send_keys(keys)
  34.  
  35. # a03.html文件
    # <div id="u_sp" class="s-isindex-wrap s-sp-menu">
    # <a href="http://news.baidu.com" target="_blank" class="mnav">新闻</a>
    # <a href="https://www.hao123.com" target="_blank" class="mnav">hao123</a>
    # <a href="http://map.baidu.com" target="_blank" class="mnav">地图</a>
    # <a href="http://v.baidu.com" target="_blank" class="mnav">视频</a>
    # <a href="http://tieba.baidu.com" target="_blank" class="mnav">贴吧</a>
    # <a href="http://xueshu.baidu.com" target="_blank" class="mnav">学术</a>
    # <a id="s_username_top" class="s-user-name-top" data-tid="2004" href="http://i.baidu.com/" target="_blank"></a></div>
  36.  
  37. #find_elements方式 多个数据
    len1=driver.find_elements_by_xpath("//a[@class='mnav']")
    len2=driver.find_elements(by='class name',value='mnav')
    print(len(len1))
    print(len(len2))
  38.  
  39. time.sleep(3)
    driver.quit()
  40.  

Selenium3+webdriver学习笔记3(xpath方式元素定位)的更多相关文章

  1. Selenium3+webdriver学习笔记2(常用元素定位方式,定位单个元素共8种,总共有23种)

    #!/usr/bin/env python# -*- coding:utf-8 -*- from selenium import webdriver import time,os # about:ad ...

  2. Python3+Selenium3+webdriver学习笔记10(元素属性、页面源码)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记10(元素属性.页面源码)'''from selenium i ...

  3. Python3+Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )

    !/usr/bin/env python -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )'''from selenium im ...

  4. Python3+Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)'''from sel ...

  5. Python3+Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)'''from ...

  6. Python3+Selenium3+webdriver学习笔记9(发送富文本信息及上传文件处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记9(发送富文本信息及上传文件处理)'''from seleni ...

  7. Python3+Selenium3+webdriver学习笔记11(cookie处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记11(cookie处理)'''from selenium im ...

  8. Python3+Selenium3+webdriver学习笔记8(单选、复选框、弹窗处理)

    #!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记8(单选.复选框.弹窗处理)''' from selenium ...

  9. Selenium3+webdriver学习笔记4(css方式元素定位)

    #!/usr/bin/env python# -*- coding:utf-8 -*- from selenium import webdriver import time,os # about:ad ...

随机推荐

  1. Logstash-安装logstash-filter-multiline插件(解决logstash匹配多行日志)

    ELK-logstash在搬运日志的时候会出现多行日志,普通的搬运会造成保存到ES中日志一条一条的保存,很丑,而且不方便读取,logstash-filter-multiline可以解决该问题. 接下来 ...

  2. MFC获取数据的方式

    假设输入框ID是:ID_NUMBER1,ID_NUMBER2,ID_NUMBER3. 获取数据的方式是: int number1,number2,number3; number1 = GetDlgIt ...

  3. CF-839B

    B. Game of the Rows time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. POJ-2718

    Smallest Difference Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12158   Accepted: 3 ...

  5. Hibernate&nbsp;Session的delete()方法

    本文介绍Hibernate Session的delete()方法.delete()方法用于从数据库中删除与Java对象对应的记录.对应游离对象和持久化对象,delete语句会做出不同的反应. AD:  ...

  6. uva 12452 Plants vs. Zombies HD SP (树DP)

    Problem I: Plants vs. Zombies HD Super Pro Plants versus Zombies HD Super Pro is a game played not a ...

  7. [poj]1050 To the Max dp

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  8. android基础复习

    http://www.cnblogs.com/aimeng/archive/2012/03/13/2393244.html android体系介绍 http://blog.chinaunix.net/ ...

  9. DNS(域名系统)服务器

    DNS(Domain Name System),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的Ip数串.通过主机名,最终得到该主机 ...

  10. 取消eclipse英文单词拼写验证

    依次点击Preferences->General->Editors->TexEditors->Spelling->Enable Spell Checkingt , 如下图 ...