当前位置: 首页 > news >正文

软件测试|selenium执行js脚本

JavaScript是运行在客户端(浏览器)和服务器端的脚本语言,允许将静态网页转换为交互式网页。可以通过 Python Selenium WebDriver 执行 JavaScript 语句,在Web页面中进行js交互。那么js能做的事,Selenium应该大部分也能做。WebDriver是模拟终端用户的交互,所以就不能点击不可见的元素,有时可见元素也不能点击。在这些情况下,我们就可以通过WebDriver 执行JavaScript来点击或者执行页面元素。本文将介绍如何使用WebDriver执行JavaScript语句。

Web元素定位及操作
使用execute_script() 执行 JavaScript 代码,有两种方法实现元素操作

方法1:文档级别操作
直接使用JavaScript实现元素定位和动作执行,主要方法有:

document.getElementById
document.getElementsByClassName
document.getElementsByName
document.getElementsByTagName
document.getElementsByTagNameNS

测试示例:

打开百度一下
输入框输入”test“
点击百度一下
python代码:

def test_baidu(self):self.driver.get("http://www.baidu.com")self.driver.execute_script('document.getElementById("kw").value = "test"')time.sleep(2)self.driver.execute_script('document.getElementById("su").click()')time.sleep(2)

在执行过程中,WebDriver 将 JavaScript 语句注入到浏览器中,然后脚本将执行。这个注入 JavaScript 有自己的名称空间,不会干扰实际网页中的 JavaScript运行。

方法2:元素级别操作
可以先使用WebDriver获取想要操作的元素,然后使用JavaScript执行操作。

input_ele = driver.find_element_by_id("su") 
driver.execute_script("arguments[0].click();", input_ele)
python代码:def test_baidu2(self):self.driver.get("http://www.baidu.com")input_ele = self.driver.find_element_by_id("kw")self.driver.execute_script("arguments[0].value = 'test';", input_ele)time.sleep(2)baidu_ele = self.driver.find_element_by_id("su")self.driver.execute_script("arguments[0].click();", baidu_ele)time.sleep(2)

可以在语句中使用多个 JavaScript动作:

username = driver.find_element_by_xpath("//*[@id='username']")
password = driver.find_element_by_xpath("//*[@id='password']")
driver.execute_script("arguments[0].value = 'admin';arguments[1].value = 'admin';", username, password)

获取返回值
可以返回JavaScript的执行结果:

driver.execute_script("return document.getElementById('kw').value")
driver.execute_script("return document.title;")  # 返回网页标题
滑动
在Web自动化测试 | ActionChains、TouchAction 中介绍了TouchAction类中scroll_from_element()也可以滑动页面。滑动到浏览器底部
document.documentElement.scrollTop=10000
window.scrollTo(0, document.body.scrollHeight)
滑动到浏览器顶部
document.documentElement.scrollTop=0
window.scrollTo(document.body.scrollHeight,0)

更改元素属性
大部分时间控件都是 readonly属性,需要手动去选择对应的时间。自动化测试中,可以使用JavaScript代码取消readonly属性。

测试页面:https://www.12306.cn/index/

在这里插入图片描述

测试步骤:

打开测试页面
修改出发日期
断言日期是否修改成功
python测试代码:

def test_datettime(self):self.driver.get("https://www.12306.cn/index/")# 取消readonly属性self.driver.execute_script("dat=document.getElementById('train_date'); dat.removeAttribute('readonly')")   self.driver.execute_script("document.getElementById('train_date').value='2020-10-01'")time.sleep(3)now_time = self.driver.execute_script("return document.getElementById('train_date').value")assert '2020-10-01' == now_time

总结
Selenium WebDriver 执行 JavaScript代码是一个非常强大的功能,可以实现WebElement 接口所有功能,甚至更多的功能。比如在web性能测试中可以调用Web API接口window.performance来测试Web性能。

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你! 

http://www.lryc.cn/news/228256.html

相关文章:

  • 【源码复现】图神经网络之PPNP/APPNH
  • 【算法与数据结构】131、LeetCode分割回文串
  • 网络编程学习笔记
  • 腾讯待办停运后怎么办呢?导出的ics文件怎么打开查看
  • 家长群如何发成绩?
  • 数组区域检索的优化 --- 分块,线段树,树状数组
  • 若依侧边栏添加计数标记效果
  • WebSocket技术解析:实现Web实时双向通信的利器
  • 深圳联强优创手持PDA身份证阅读器 身份证核验手持机
  • 力扣labuladong——一刷day31
  • 里氏代换原则
  • Illumination Adaptive Transformer
  • 【教3妹学编程-算法题】给小朋友们分糖果 II
  • 应急响应练习2
  • JS算法练习 11.13
  • js升序排序
  • 2023亚太杯数学建模C题思路
  • 【ArcGIS Pro微课1000例】0030:ArcGIS Pro中自带晕渲地貌工具的妙用
  • 【原创】java+swing+mysql办公用品管理系统设计与实现
  • sqlalchemy查询数据为空,查询范围对应的数据在数据库真实存在
  • Code Former安装及使用
  • SpringMVC--@RequestMapping注解
  • ARM寄存器及功能介绍/R0-R15寄存器
  • js删除json数据中指定元素
  • 广州华锐互动:VR刑侦现场执法实训助力警察全面提升警务能力
  • 多线程 浏览器渲染引擎 图形用户界面(GUI,Graphical User Interface)应用程序
  • echarts饼图label显示不全原因?
  • 暖手宝上架亚马逊美国站UL499报告测试标准要求
  • 2023数据结构期中测验-2023秋-计算机+未来网络专业
  • 解锁内存之谜:从C到Python、Java和Go的内存管理对比