首页 > 编程语言 > 详细

selenium+python 处理只读日期控件的2种方法

时间:2020-05-23 22:58:12      阅读:118      评论:0      收藏:0      [点我收藏+]

前言
有时候测试过程中会遇到日期控件场景,这时候需要特殊处理,下文以12306网站为例
技术分享图片

处理方式
通常是通过js去除只读属性(2种方法),然后通过send_keys重新写值

from time import sleep

driver = webdriver.Chrome()
driver.get("https://www.12306.cn/index/")
sleep(2)

date_txt = driver.find_element_by_xpath("//*[@id=‘train_date‘]")

# 方法1:通过js的getElementById去掉只读属性
driver.execute_script("document.getElementById(‘train_date‘).removeAttribute(‘readonly‘);")

# 方法2:通过js的document.arguments[0]去掉只读属性
driver.execute_script("arguments[0].removeAttribute(‘readonly‘);",date_txt)

# 通过send_keys操作,重新写值
date_txt.clear()   # 先清除原来的日期值
date_txt.send_keys(‘2020-06-21‘)```

selenium+python 处理只读日期控件的2种方法

原文:https://www.cnblogs.com/ritaliu/p/12944826.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!