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 ...
随机推荐
- struts2的method="{1}"
这里的{1}表示接收前面action里通过通配符传来的值,例如你配置的是 ,然后调用***/editCrud.action,则method里获得的值是edit,将会调用这个action里面的 edit ...
- Primer回顾 数组和指针
数组和指针类似于vector和迭代器. 区别在于:数组的长度是固定的.数组一经创建,就不允许添加新的元素.指针则可以像迭代器一样用于遍历和检查数组中的元素. 设计良好的程序只有在强调速度时才在类实现的 ...
- .Net HttpWebRequest 爬虫核心爬取
1 爬虫,爬虫攻防 2 下载html 3 xpath解析html,获取数据和深度抓取(和正则匹配) 4 多线程抓取 熟悉http协议 提供两个方法Post和Get public static stri ...
- Vue拖拽组件列表实现动态页面配置
需求描述 最近在做一个后台系统,有一个功能产品需求是页面分为左右两部分,通过右边的组件列表来动态配置左边的页面视图,并且左边由组件拼装起来的视图,可以实现上下拖拽改变顺序,也可以删除. 根据这个需求我 ...
- es6常用方法
一.let 和 constlet 声明变量,只在所在的块区有效,不存在变量提升:var 存在变 量提升const 声明常量,只在所在块区有效 二.变量的解构赋值1.数组的解构赋值let [a, b, ...
- 2014-8-5 NOIP(雾)模拟赛
皇帝的烦恼(二分答案) Description 经过多年的杀戮,秦皇终于统一了中国.为了抵御外来的侵略,他准备在国土边境安置n名将军.不幸的是这n名将军羽翼渐丰,开始展露他们的狼子野心了.他们拒绝述职 ...
- Node.js 内置模块crypto加密模块(2) AES
AES:高级加密标准 ( Advanced Encryption Standard ) AES是一种对称加密算法:加密需要密钥,且加密密钥和解密密钥相同 下面是AES加密的Node实现: " ...
- 截取HTML中的JSON数据并利用GSON进行解析(Android)
截取HTML中的JSON数据并利用GSON进行解析(Android) 前言 最近在做的一个Android项目,需要自行搭建服务器,队友选择买了阿里云的服务器ESC产品,在数据获取上,我们采用了Andr ...
- Nginx 最新版源码编译安装 包含常用模块作用及所需依赖
第一部分 Nginx最新版源码编译安装 1. 使用的模块 模块1:http_rewrite_module 基于正则匹配来实现重定向.依赖PCRE库,见依赖1 模块2:http_gzip_module ...
- Java - 一道关于Arrays.asList的题目
题目 有这样一道有趣的题目: final int[] test = new int[]{1,2,3,4}; final Integer[] test2 = new Integer[]{1,2,3,4} ...