web自动化(四)日期控件

执行js脚本有两种方式一种是原始js 一种jQuery 推荐使用jQuery 根据简洁 方便

selenuim

bash 复制代码
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import  WebDriverWait
from  selenium.webdriver.support import  expected_conditions as EC
web=webdriver.Firefox()
web.get("https://www.12306.cn/index")
el=(By.ID,"train_date")
WebDriverWait(web,10,2).until(EC.presence_of_element_located(el))
el_se=web.find_element(*el)
#执行js 方式
# js="arguments[0].setAttribute('readonly',true)"
# web.execute_script(js,el_se)
# time.sleep(5)
# #控件如果是只读,需要删除readonly 属性
# #arguments 是参数集合
# js="var el=arguments[0]; el.removeAttribute('readonly')"
# web.execute_script(js,el_se)

#jQuery 方式 推荐
js="$('#train_date').attr('readonly',true)"
time.sleep(5)
web.execute_script(js)
time.sleep(5)
js="$('#train_date').removeAttr('readonly')"
web.execute_script(js)


time.sleep(5)
el_se.clear()
time.sleep(2)
el_se.send_keys("2024-07-15")
time.sleep(5)
web.quit()

drissionpage

bash 复制代码
import time
from DrissionPage import  WebPage
from DrissionPage.common import By as DBy
web_page=WebPage("d")
web_page.get("https://www.12306.cn/index")
el=(DBy.ID,"train_date")

web_page.wait.eles_loaded(el,timeout=5)
js="$('#train_date').attr('readonly',true)"
web_page.run_js(js)
time.sleep(10)

js="$('#train_date').removeAttr('readonly')"
web_page.run_js(js)

time.sleep(5)

js="$('#train_date').val('2024-7-18')"
web_page.run_js(js)

time.sleep(5)
web_page.quit()
相关推荐
测试老哥2 天前
Selenium 使用指南
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
九章云极AladdinEdu2 天前
超参数自动化调优指南:Optuna vs. Ray Tune 对比评测
运维·人工智能·深度学习·ai·自动化·gpu算力
学生信的大叔2 天前
【Python自动化】Ubuntu24.04配置Selenium并测试
python·selenium·自动化
GAOJ_K2 天前
弧形导轨如何提升新能源汽车的能效和续航里程?
科技·自动化·汽车·制造
全栈工程师修炼指南2 天前
告别手动构建!Jenkins 与 Gitlab 完美协作,根据参数自动化触发CI/CD流水线实践
运维·ci/cd·自动化·gitlab·jenkins
Source.Liu2 天前
【Pywinauto库】12.2 pywinauto.element_info 后端内部实施模块
windows·python·自动化
Source.Liu2 天前
【Pywinauto库】12.1 pywinauto.backend 后端内部实施模块
开发语言·windows·python·自动化
宇钶宇夕2 天前
西门子 S7-200 SMART PLC 核心指令详解:从移位、上升沿和比较指令到流水灯控制程序实战
运维·算法·自动化
大视码垛机2 天前
速度与安全双突破:大视码垛机重构工业自动化新范式
大数据·数据库·人工智能·机器人·自动化·制造
疯狂的维修3 天前
关于Gateway configration studio软件配置网关
网络协议·c#·自动化·gateway