selenium--更改标签的属性值
前戏
在进行web自动化的时候,我们有时需要获取元素的属性,有时需要添加,有时需要删除,这时候就要通过js来进行操作了
实战
from selenium import webdriver
import unittest def addAttribute(driver, elementobj, attributeName, value):
'''
封装向页面标签添加新属性的方法
调用JS给页面标签添加新属性,arguments[0]~arguments[2]分别
会用后面的element,attributeName和value参数进行替换
添加新属性的JS代码语法为:element.attributeName=value
比如input.name='test'
'''
driver.execute_script("arguments[0].%s=arguments[1]" % attributeName, elementobj, value) def setAttribute(driver, elementobj, attributeName, value):
'''
封装设置页面对象的属性值的方法
调用JS代码修改页面元素的属性值,arguments[0]~arguments[1]分别
会用后面的element,attributeName和value参数进行替换
'''
driver.execute_script("arguments[0].setAttribute(arguments[1],arguments[2])", elementobj, attributeName, value) def getAttribute(elementobj, attributeName):
# 封装获取页面对象的属性值方法
return elementobj.get_attribute(attributeName) def removeAttribute(driver, elementobj, attributeName):
'''
封装删除页面属性的方法
调用JS代码删除页面元素的指定的属性,arguments[0]~arguments[1]分别
会用后面的element,attributeName参数进行替换
'''
driver.execute_script("arguments[0].removeAttribute(arguments[1])",
elementobj, attributeName) class TestDemo(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome() def test_dataPicker(self):
url = "D:\PycharmProjects\zouzou\dom.html"
self.driver.get(url)
element = self.driver.find_element_by_xpath('//input') # 向页面文本框input标签中添加新属性name='search'
addAttribute(self.driver, element, 'name', 'search')
# 添加新属性后,查看一下新属性值
print('添加的新属性值%s="%s"' % ("name", getAttribute(element, "name"))) print('更改文本框中内容前的value的值:', getAttribute(element, 'value'))
# 更改value的属性值为“这是更改后的值”
setAttribute(self.driver, element, 'value', '这是更改后的值')
print('更改后value的值为:', getAttribute(element, 'value')) # 查看更改前input页面元素中size属性值
print('更改前size的属性值为:', getAttribute(element, 'size'))
# 更改input的属性值为20
setAttribute(self.driver, element, 'size', 20)
print('更改后size的属性值为:', getAttribute(element, 'size')) # 查看删除input页面元素value属性前的值
print('删除前文本框value的值:', getAttribute(element, 'value'))
# 删除属性值
removeAttribute(self.driver, element, 'value')
print('删除后文本框value的值:', getAttribute(element, 'value')) if __name__ == '__main__':
unittest.main()
selenium--更改标签的属性值的更多相关文章
- <mate>标签中属性/值的各个意思
<mate>标签中属性/值的各个意思 HTML 4 name 属性 1.<mate name="author" content="" /> ...
- 正则表达式,提取html标签的属性值
/** * 提取HTML标签的属性值 * @param source HTML标签内容 * "<a title=中国体育报 href=''>aaa</a><a ...
- 正值表达式匹配html标签的属性值
今天由于工作的需求,需要获取html标签的属性值,立即想到了正则表达式,标签如下: <circle id="ap_test" cx="200" cy=&q ...
- 使用script创建标签添加属性值和添加样式
<mark>使用script创建标签和给标签属性值以及样式的方法</mark><script> window.onload=function(){ var btn= ...
- 在tomcat启动时解析xml文件,获取特定标签的属性值,并将属性值设置到静态变量里
这里以解析hibernate.cfg.xml数据库配置信息为例,运用dom4j的解析方式来解析xml文件. 1.在javaWeb工程里新建一个java类,命名为GetXmlValue.java,为xm ...
- a 标签 rel 属性值 opener 的作用
<a> 元素,原英文单词为 anchor 的缩写,所以又称之为锚点元素.锚点元素的 href 属性用来创建通向其他网页.文件.同一页面内的位置.电子邮件地址或任何其他 URL 的超链接. ...
- JSP动作标签flush属性值
在JSP动作标签<jsp:include flush="true"/>,flush属性可以为true或false.flush默认值为false,当把flush属性赋值为 ...
- 怎样使用 v-bind 绑定 html 标签的属性值?
1. 在 Vue 中可是使用 v-bind 对 html 中的 属性 进行绑定, 如下所示, 我们想给这个 a 标签绑定一个 title 值: <!DOCTYPE html> <ht ...
- html标签默认属性值之margin;padding值
一.h1~h6标签:有默认margin(top,bottom且相同)值,没有默认padding值. 在chrome中:16,15,14,16,17,19; 在firefox中:16,15,14,16, ...
随机推荐
- # Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python.java) Given two binary strings, return their sum (also a binar ...
- First Step in luogu.
2019-11-21 21:58:32 在洛谷正式迈出第一步!!
- 管道的原子性 linux写操作原子性
从本质上说,管道也是一种文件,但他又和一般的文件有所不同,管道可以克服使用文件进行通信的两个问题 限制管道的大小.实际上,管道是一个固定大小的缓冲区.在Linux中该换冲区的大小为一页,4k 使得他的 ...
- ElasticSearch如何更新集群的状态
ElasticSearch如何更新集群的状态 最近发生了很多事情,甚至对自己的技术能力和学习方式产生了怀疑,所以有一段时间没更新文章了,估计以后更新的频率会越来越少,希望有更多的沉淀而不是简单地分享. ...
- 干货最新版 Spring Boot2.1.5 教程+案例合集
最近发了一系列的 Spring Boot 教程,但是发的时候没有顺序,有小伙伴反映不知道该从哪篇文章开始看起,刚好最近工作告一个小小段落,松哥就把这些资料按照学习顺序重新整理了一遍,给大家做一个索引, ...
- [5]Hexo静态博客绑定域名及域名解析
示例: http://zsy.xyz/ 前提预设:[4]Hexo静态博客背景及界面显示优化配置 [3]hexo+github搭建个人博客的主题配置 [2]hexo+github搭建个人博客的简单使用 ...
- HDU 2007-11 Programming Contest
Can you find it? Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/10000 K (Java/Others ...
- Mysql系列(十)—— 性能分析工具profiling
转载自:http://www.ywnds.com/?p=8677 explain是从mysql怎样解析执行sql的角度分析sql优劣.profiling是从sql执行时资源使用情况的角度来分析sql. ...
- C++ 中 string和char* 的区别
C++ 中 string和char* 的区别 1.定义: string:string是STL当中的一个容器,对其进行了封装,所以操作起来非常方便. char*:char *是一个指针,可以指向一个字符 ...
- 基于roslyn的动态编译库Natasha
人老了,玩不转博客园的编辑器,详细信息转到:https://mp.weixin.qq.com/s/1r6YKBkyovQSMUgfm_VxBg 关键字:Github, NCC, Natasha,Ros ...