Selenium3+webdriver学习笔记3(xpath方式元素定位)
#!/usr/bin/env python
# -*- coding:utf-8 -*- from selenium import webdriver import time,os # about:addons 火狐浏览器安装组件,访问的地址 # <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() driver.get(url) #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) #其他属性定位
driver.find_element_by_xpath("//*[@autocomplete='off']").send_keys(keys) #指定标签名称 写标签名称,不指定则写 *,如搜索框 input
driver.find_element_by_xpath("//input[@class='s_ipt']").send_keys(keys) #通过多级层级来定位 ,上一个层级 上上层级
# <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"> driver.find_element_by_xpath("//form[@id='form']/span/input").send_keys(keys) #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> driver.find_element_by_xpath("//option[@value='2']").click() # <a href="https://www.hao123.com" target="_blank" class="mnav">hao123</a>
# 模糊匹配
driver.find_element_by_xpath("//*[contains(text(),'hao123')]").click() # <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
#模糊匹配 包含属性
driver.find_element_by_xpath("//*[contains(@id,'kw')]").send_keys(keys) #模糊匹配 已什么开头
driver.find_element_by_xpath("//input[starts-with(@class,'s_')]").send_keys(keys) # find_element方式 单个数据
driver.find_element(by='id',value='kw').send_keys(keys) # 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> #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)) time.sleep(3)
driver.quit()
Selenium3+webdriver学习笔记3(xpath方式元素定位)的更多相关文章
- Selenium3+webdriver学习笔记2(常用元素定位方式,定位单个元素共8种,总共有23种)
#!/usr/bin/env python# -*- coding:utf-8 -*- from selenium import webdriver import time,os # about:ad ...
- Python3+Selenium3+webdriver学习笔记10(元素属性、页面源码)
#!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记10(元素属性.页面源码)'''from selenium i ...
- Python3+Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )
!/usr/bin/env python -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记14(等待判断 鼠标事件 )'''from selenium im ...
- Python3+Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)
#!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记13(js操作应用:弹出框无效如何处理)'''from sel ...
- Python3+Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)
#!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记12(js操作应用:滚动条 日历 内嵌div)'''from ...
- Python3+Selenium3+webdriver学习笔记9(发送富文本信息及上传文件处理)
#!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记9(发送富文本信息及上传文件处理)'''from seleni ...
- Python3+Selenium3+webdriver学习笔记11(cookie处理)
#!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记11(cookie处理)'''from selenium im ...
- Python3+Selenium3+webdriver学习笔记8(单选、复选框、弹窗处理)
#!/usr/bin/env python# -*- coding:utf-8 -*-'''Selenium3+webdriver学习笔记8(单选.复选框.弹窗处理)''' from selenium ...
- Selenium3+webdriver学习笔记4(css方式元素定位)
#!/usr/bin/env python# -*- coding:utf-8 -*- from selenium import webdriver import time,os # about:ad ...
随机推荐
- [原创]Devexpress XtraReports 系列 1 创建静态报表
在各种管理系统中,报表是必不可少的.报表是数据最直观体现的方式之一 以前,屌丝我是微软的忠实粉丝,所以报表工具就是微软自带的RDLC... 不可否认的是RDLC的功能是非常强大的.能够满足绝大多数的要 ...
- ubuntu 14.04 部署Django项目
一.购买服务器 推荐 vultr的服务器,还可以_ _ _,链接:传送门 操作系统建议选 ubuntu 14.04 64位 二.购买域名 链接:传送门 三.安装相关软件 # 创建一个叫mu的用户 ro ...
- windows64下安装MySQLdb连接数据库
MySQLdb是Python连接MySQL的模块,下面介绍一下源码方式安装MySQLdb: 1.首先要下载:http://www.codegood.com/downloads,由于我的python是2 ...
- php+redis实现高并发模拟下单、秒杀、抢购操作
对于高并发下的场景,一般都是采用redis缓存机制来处理. 当然也不是只有redis可以处理.还有利用mysql事务操作锁住操作的行.文件锁. 不过这些方式都没有redis缓存高效.可靠. 模拟的过程 ...
- python 学习笔记8 (模块)
Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句. 模块让你能够有逻辑地组织你的 Python 代码段. 把相关的代码 ...
- python3登陆接口测试
Python3和Python2有很大的语法区别,在实际的项目中,要注意格式.今天用Python3做一个接口测试,由于没有经验,用Python2的语法,调了半天没有搞定,后来一个大神指点了一下,终于拨开 ...
- OVN学习(一)
参考文档 OVN学习系列参考博文 部署OVN实验环境 网络拓扑 ### Central节点 # cat ifcfg-ens3 TYPE=Ethernet BOOTPROTO=static DEFROU ...
- linux命令之env和export
1.查看环境变量: env查看所有的环境变量: 使用echo命令查看单个环境变量.例如: echo $PATH 使用set查看所有本地定义的环境变量. unset可以删除指定的环境变量. 2.常用环境 ...
- 《深入理解Java虚拟机》笔记03 -- 垃圾收集器
收集器可以大致分为:单线程收集器, 并发收集器和并行收集器. 并行(Parallel):指多条垃圾收集线程并行工作,但此时用户线程仍然处于等待状态. 并发(Concurrent):指用户线程与垃圾收集 ...
- [Xcode 实际操作]六、媒体与动画-(17)使用MediaPlayer框架播放视频
目录:[Swift]Xcode实际操作 本文将演示视频的播放功能. 在项目名称上点击鼠标右键,弹出右键菜单, 选择[Add Files to "DemoApp"],往项目中导入文件 ...