前戏

在进行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--更改标签的属性值的更多相关文章

  1. <mate>标签中属性/值的各个意思

    <mate>标签中属性/值的各个意思 HTML 4 name 属性 1.<mate name="author" content="" /> ...

  2. 正则表达式,提取html标签的属性值

    /** * 提取HTML标签的属性值 * @param source HTML标签内容 * "<a title=中国体育报 href=''>aaa</a><a ...

  3. 正值表达式匹配html标签的属性值

    今天由于工作的需求,需要获取html标签的属性值,立即想到了正则表达式,标签如下: <circle id="ap_test" cx="200" cy=&q ...

  4. 使用script创建标签添加属性值和添加样式

    <mark>使用script创建标签和给标签属性值以及样式的方法</mark><script> window.onload=function(){ var btn= ...

  5. 在tomcat启动时解析xml文件,获取特定标签的属性值,并将属性值设置到静态变量里

    这里以解析hibernate.cfg.xml数据库配置信息为例,运用dom4j的解析方式来解析xml文件. 1.在javaWeb工程里新建一个java类,命名为GetXmlValue.java,为xm ...

  6. a 标签 rel 属性值 opener 的作用

    <a> 元素,原英文单词为 anchor 的缩写,所以又称之为锚点元素.锚点元素的 href 属性用来创建通向其他网页.文件.同一页面内的位置.电子邮件地址或任何其他 URL 的超链接. ...

  7. JSP动作标签flush属性值

    在JSP动作标签<jsp:include flush="true"/>,flush属性可以为true或false.flush默认值为false,当把flush属性赋值为 ...

  8. 怎样使用 v-bind 绑定 html 标签的属性值?

    1. 在 Vue 中可是使用 v-bind 对 html 中的 属性 进行绑定, 如下所示, 我们想给这个 a 标签绑定一个 title 值: <!DOCTYPE html> <ht ...

  9. html标签默认属性值之margin;padding值

    一.h1~h6标签:有默认margin(top,bottom且相同)值,没有默认padding值. 在chrome中:16,15,14,16,17,19; 在firefox中:16,15,14,16, ...

随机推荐

  1. 调试MATLAB代码

    1.在子函数设置的断点,在运行时,不起作用: 因为在主函数开始时,使用了clear all,在运行时,会把断点给删除.

  2. sort和sorted

    sort 与 sorted 区别: sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作. list 的 sort 方法返回的是对已经存在的列表进行操作,而内建函数 ...

  3. Isilon Gen6的换盘步骤

    When a drive is faulted and ready for replacement, OneFS will illuminate the Front Panel Fault LED a ...

  4. Pandas操作excel

    读取excel:Pandas库read_excel()参数详解 pandas.read_excel(io,sheet_name = 0,header = 0,names = None,index_co ...

  5. 《 .NET并发编程实战》阅读指南 - 第7章

    先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.

  6. ******可用 SpringBoot 项目打包分开lib,配置和资源文件

    spring-boot多模块打包后,无法找到其他模块中的类https://blog.csdn.net/Can96/article/details/96172172 关于SpringBoot项目打包没有 ...

  7. windows通过秘钥使用ssh和scp

    windows10内置支持了ssh和scp等仿linux指令,可以让我们方便的使用类似linux的工作流把日常的代码资源部署维护工作写成脚本(批处理). ssh和scp都有-i参数可以通过指定一个私钥 ...

  8. 转 Sqlserver_left join 、right join、 inner join 用法

    https://www.cnblogs.com/ingstyle/p/4368064.html   left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right joi ...

  9. Python——全国瓜子二手车数据分析

    继瓜子二手车数据爬去之后,在此进行处理分析 Pycharm代码 # coding:utf8 # author:Jery # datetime:2019/5/3 17:35 # software:PyC ...

  10. 在IE中点击转跳,并打开chorme浏览器继续浏览指定页面,IE自定义ocx控件开发

    因项目需要,需要开发一个功能:在IE中点击转跳,并打开chorme浏览器继续浏览指定页面. 分析需求后,参考了: https://www.cnblogs.com/ffjiang/p/7908025.h ...